Fix for no internet and no local versions installed

This commit is contained in:
Tom Cat 2023-10-23 10:54:57 -06:00
parent 6ea3710e4f
commit ecb19ad647

View File

@ -156,9 +156,13 @@ fn getRemoteVersionsSlice(allocator: std.mem.Allocator) ZimError![]const ZigVers
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
var request = client.request(.GET, versions_json_uri, headers, .{}) catch {
std.debug.print("Error creating request\n", .{});
return ZimError.NetworkError;
var request = client.request(.GET, versions_json_uri, headers, .{}) catch |err| {
if (err == error.TemporaryNameServerFailureError) {
std.debug.print("Name Server for {s} could not be resolved. Check your internet connection.", .{versions_json_url});
return ZimError.NetworkError;
}
std.debug.print("Error creating request\n{any}\n", .{err});
return ZimError.Unexpected;
};
defer request.deinit();
@ -239,7 +243,7 @@ fn shellName(s: ?ShellType) string {
}
fn printZimPathHelp(shell_tag: ?ShellType, zim_path: string) void {
if (shell_tag == undefined) {
if (shell_tag == null) {
std.debug.print("Unrecognized shell\n", .{});
return;
}
@ -311,15 +315,15 @@ fn downloadAndExtractTarball(allocator: std.mem.Allocator, zv: ZigVersion) ZimEr
const tar_buf_len = 38;
var tar_arr: [tar_buf_len]u8 = undefined;
var tar_buf = tar_arr[0..];
const tarball_file_name = std.fmt.bufPrint(tar_buf, "zig-{s}.tar", .{ zim_path, zv.version_string }) catch {
const tarball_file_name = std.fmt.bufPrint(tar_buf, "zig-{s}.tar", .{zv.version_string}) catch {
return ZimError.Memory;
};
// Buffer lenght for `<zim_path>/versions/<tarbll_file_name>`
// Buffer lenght for `<zim_path>/versions/<tarbll_file_name>`
const full_tar_buf_len = buf_len + tar_buf_len + 10;
var full_tar_arr: [full_tar_buf_len]u8 = undefined;
var full_tar_buf = full_tar_arr[0..];
const full_path_tarball_file_name = std.fmt.bufPrint(full_tar_buf, "{s}/versions/{s}", .{ zim_path, tarball_file_name}) catch {
const full_path_tarball_file_name = std.fmt.bufPrint(full_tar_buf, "{s}/versions/{s}", .{ zim_path, tarball_file_name }) catch {
return ZimError.Memory;
};
@ -347,10 +351,10 @@ fn downloadAndExtractTarball(allocator: std.mem.Allocator, zv: ZigVersion) ZimEr
};
// Remove tarball file
zim_dir.deleteFile(full_path_tarball_file_name) catch |err| {
std.debug.print("Could not remove {s}: {any}\n", .{tarball_file_name, err});
return ZimError.BadIO;
};
zim_dir.deleteFile(full_path_tarball_file_name) catch |err| {
std.debug.print("Could not remove {s}: {any}\n", .{ tarball_file_name, err });
return ZimError.BadIO;
};
std.debug.print("{s} is installed and now locally available!\n", .{zv.version_string});
}
@ -472,13 +476,17 @@ pub fn list(allocator: std.mem.Allocator, param: string) !void {
std.debug.print("\n [{d}]\tZig Version {s}", .{ i + 1, version_str });
i += 1;
}
std.debug.print("\n", .{});
std.debug.print(
\\
\\ Run `zim use` to select which version you want active in your environment.
\\ You can also just specify the index of the version you would like to use.
\\
, .{});
if (i == 0) {
std.debug.print("\nNo local versions installed\nTry `zim install help`\n", .{});
} else {
std.debug.print("\n", .{});
std.debug.print(
\\
\\ Run `zim use` to select which version you want active in your environment.
\\ You can also just specify the index of the version you would like to use.
\\
, .{});
}
return;
},