diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Installation.zig | 18 | ||||
| -rw-r--r-- | src/install.zig | 34 | ||||
| -rw-r--r-- | src/main.zig | 4 |
3 files changed, 24 insertions, 32 deletions
diff --git a/src/Installation.zig b/src/Installation.zig index 3c056b9..4f7a48d 100644 --- a/src/Installation.zig +++ b/src/Installation.zig | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | const curl = @import("curl"); | ||
| 1 | const std = @import("std"); | 2 | const std = @import("std"); |
| 2 | const xdg = @import("xdg"); | 3 | const xdg = @import("xdg"); |
| 3 | const zelda = @import("zelda"); | ||
| 4 | const zup = @import("zup"); | 4 | const zup = @import("zup"); |
| 5 | 5 | ||
| 6 | const Allocator = std.mem.Allocator; | 6 | const Allocator = std.mem.Allocator; |
| @@ -135,23 +135,13 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { | |||
| 135 | pub fn getAvailableList(config: Config) !Installations { | 135 | pub fn getAvailableList(config: Config) !Installations { |
| 136 | const allocator = config.allocator; | 136 | const allocator = config.allocator; |
| 137 | 137 | ||
| 138 | var response = try zelda.get(allocator, "https://ziglang.org/download/index.json"); | 138 | var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); |
| 139 | defer response.deinit(); | 139 | defer allocator.free(json_str); |
| 140 | |||
| 141 | if (response.status_code.group() != .success) { | ||
| 142 | std.log.err("HTTP Error while getting Zig download index.json: {}", .{response.status_code}); | ||
| 143 | return error.HttpError; | ||
| 144 | } | ||
| 145 | |||
| 146 | if (response.body == null) { | ||
| 147 | std.log.err("No body response while getting Zig download index.json", .{}); | ||
| 148 | return error.HttpError; | ||
| 149 | } | ||
| 150 | 140 | ||
| 151 | var parser = std.json.Parser.init(allocator, false); | 141 | var parser = std.json.Parser.init(allocator, false); |
| 152 | defer parser.deinit(); | 142 | defer parser.deinit(); |
| 153 | 143 | ||
| 154 | var vt = try parser.parse(response.body.?); | 144 | var vt = try parser.parse(json_str); |
| 155 | defer vt.deinit(); | 145 | defer vt.deinit(); |
| 156 | 146 | ||
| 157 | if (vt.root != .Object) { | 147 | if (vt.root != .Object) { |
diff --git a/src/install.zig b/src/install.zig index 5eb5a29..27db4bb 100644 --- a/src/install.zig +++ b/src/install.zig | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | const libarchive = @import("libarchive"); | 1 | const libarchive = @import("libarchive"); |
| 2 | const curl = @import("curl"); | ||
| 2 | const std = @import("std"); | 3 | const std = @import("std"); |
| 3 | const xdg = @import("xdg"); | 4 | const xdg = @import("xdg"); |
| 4 | const zelda = @import("zelda"); | ||
| 5 | const zup = @import("zup"); | 5 | const zup = @import("zup"); |
| 6 | 6 | ||
| 7 | const Allocator = std.mem.Allocator; | 7 | const Allocator = std.mem.Allocator; |
| @@ -51,20 +51,26 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I | |||
| 51 | defer allocator.free(installation_dir); | 51 | defer allocator.free(installation_dir); |
| 52 | 52 | ||
| 53 | std.log.info("Installing {s}, version {}", .{ name, installation.version }); | 53 | std.log.info("Installing {s}, version {}", .{ name, installation.version }); |
| 54 | 54 | const filename = std.fs.path.basename(installation.url.?); | |
| 55 | std.log.info("Downloading from {s}...", .{installation.url.?}); | 55 | |
| 56 | var response = try zelda.get(allocator, installation.url.?); | 56 | // TODO: Platform-agnostic tempfile creation |
| 57 | defer response.deinit(); | 57 | var tmpname = try std.fmt.allocPrintZ(allocator, "/tmp/{s}", .{filename}); |
| 58 | 58 | defer allocator.free(tmpname); | |
| 59 | if (response.status_code.group() != .success) { | 59 | |
| 60 | std.log.err("HTTP Error: {}", .{response.status_code}); | 60 | var tmpdir = try std.fs.openDirAbsolute(std.fs.path.dirname(tmpname).?, .{}); |
| 61 | return error.HttpError; | 61 | defer tmpdir.close(); |
| 62 | |||
| 63 | var tmpfile = try tmpdir.createFile(filename, .{}); | ||
| 64 | defer { | ||
| 65 | tmpfile.close(); | ||
| 66 | std.log.info("Deleting /tmp/{s}...", .{filename}); | ||
| 67 | tmpdir.deleteFile(filename) catch |err| { | ||
| 68 | std.log.warn("Failed to delete /tmp/{s}: {}", .{ filename, err }); | ||
| 69 | }; | ||
| 62 | } | 70 | } |
| 63 | 71 | ||
| 64 | if (response.body == null) { | 72 | std.log.info("Downloading to /tmp/{s}...", .{filename}); |
| 65 | std.log.err("HTTP Error, no body received", .{}); | 73 | try curl.easyDownloadToFile(&tmpfile, installation.url.?); |
| 66 | return error.HttpError; | ||
| 67 | } | ||
| 68 | 74 | ||
| 69 | std.log.info("Extracting...", .{}); | 75 | std.log.info("Extracting...", .{}); |
| 70 | var archive = try ArchiveRead.init(); | 76 | var archive = try ArchiveRead.init(); |
| @@ -72,7 +78,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I | |||
| 72 | 78 | ||
| 73 | try archive.supportFilter(.all); | 79 | try archive.supportFilter(.all); |
| 74 | try archive.supportFormat(.all); | 80 | try archive.supportFormat(.all); |
| 75 | try archive.openMemory(response.body.?); | 81 | try archive.openFilename(tmpname, 10240); |
| 76 | 82 | ||
| 77 | while (try archive.nextHeader()) |entry_c| { | 83 | while (try archive.nextHeader()) |entry_c| { |
| 78 | var entry = entry_c; | 84 | var entry = entry_c; |
diff --git a/src/main.zig b/src/main.zig index 11aae8f..197a224 100644 --- a/src/main.zig +++ b/src/main.zig | |||
| @@ -12,7 +12,6 @@ pub const update = SubCommand(@import("update.zig")); | |||
| 12 | pub const version = SubCommand(Version); | 12 | pub const version = SubCommand(Version); |
| 13 | 13 | ||
| 14 | const std = @import("std"); | 14 | const std = @import("std"); |
| 15 | const zelda = @import("zelda"); | ||
| 16 | const zup_config = @import("zup-config"); | 15 | const zup_config = @import("zup-config"); |
| 17 | 16 | ||
| 18 | const ArgIterator = std.process.ArgIterator; | 17 | const ArgIterator = std.process.ArgIterator; |
| @@ -25,9 +24,6 @@ pub fn main() !void { | |||
| 25 | const allocator = gpa.allocator(); | 24 | const allocator = gpa.allocator(); |
| 26 | defer _ = gpa.deinit(); | 25 | defer _ = gpa.deinit(); |
| 27 | 26 | ||
| 28 | // TODO: Make an issue in zelda mentioning this | ||
| 29 | defer zelda.client.global_connection_cache.deinit(); | ||
| 30 | |||
| 31 | var config = try Config.init(allocator); | 27 | var config = try Config.init(allocator); |
| 32 | defer config.deinit(); | 28 | defer config.deinit(); |
| 33 | 29 | ||