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 }; var client = std.http.Client{ .allocator = allocator };
defer client.deinit(); defer client.deinit();
var request = client.request(.GET, versions_json_uri, headers, .{}) catch { var request = client.request(.GET, versions_json_uri, headers, .{}) catch |err| {
std.debug.print("Error creating request\n", .{}); 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; return ZimError.NetworkError;
}
std.debug.print("Error creating request\n{any}\n", .{err});
return ZimError.Unexpected;
}; };
defer request.deinit(); defer request.deinit();
@ -239,7 +243,7 @@ fn shellName(s: ?ShellType) string {
} }
fn printZimPathHelp(shell_tag: ?ShellType, zim_path: string) void { fn printZimPathHelp(shell_tag: ?ShellType, zim_path: string) void {
if (shell_tag == undefined) { if (shell_tag == null) {
std.debug.print("Unrecognized shell\n", .{}); std.debug.print("Unrecognized shell\n", .{});
return; return;
} }
@ -311,7 +315,7 @@ fn downloadAndExtractTarball(allocator: std.mem.Allocator, zv: ZigVersion) ZimEr
const tar_buf_len = 38; const tar_buf_len = 38;
var tar_arr: [tar_buf_len]u8 = undefined; var tar_arr: [tar_buf_len]u8 = undefined;
var tar_buf = tar_arr[0..]; 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; return ZimError.Memory;
}; };
@ -319,7 +323,7 @@ fn downloadAndExtractTarball(allocator: std.mem.Allocator, zv: ZigVersion) ZimEr
const full_tar_buf_len = buf_len + tar_buf_len + 10; const full_tar_buf_len = buf_len + tar_buf_len + 10;
var full_tar_arr: [full_tar_buf_len]u8 = undefined; var full_tar_arr: [full_tar_buf_len]u8 = undefined;
var full_tar_buf = full_tar_arr[0..]; 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; return ZimError.Memory;
}; };
@ -348,7 +352,7 @@ fn downloadAndExtractTarball(allocator: std.mem.Allocator, zv: ZigVersion) ZimEr
// Remove tarball file // Remove tarball file
zim_dir.deleteFile(full_path_tarball_file_name) catch |err| { zim_dir.deleteFile(full_path_tarball_file_name) catch |err| {
std.debug.print("Could not remove {s}: {any}\n", .{tarball_file_name, err}); std.debug.print("Could not remove {s}: {any}\n", .{ tarball_file_name, err });
return ZimError.BadIO; return ZimError.BadIO;
}; };
@ -472,6 +476,9 @@ pub fn list(allocator: std.mem.Allocator, param: string) !void {
std.debug.print("\n [{d}]\tZig Version {s}", .{ i + 1, version_str }); std.debug.print("\n [{d}]\tZig Version {s}", .{ i + 1, version_str });
i += 1; i += 1;
} }
if (i == 0) {
std.debug.print("\nNo local versions installed\nTry `zim install help`\n", .{});
} else {
std.debug.print("\n", .{}); std.debug.print("\n", .{});
std.debug.print( std.debug.print(
\\ \\
@ -479,6 +486,7 @@ pub fn list(allocator: std.mem.Allocator, param: string) !void {
\\ You can also just specify the index of the version you would like to use. \\ You can also just specify the index of the version you would like to use.
\\ \\
, .{}); , .{});
}
return; return;
}, },