diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Installation.zig | 18 | ||||
| -rw-r--r-- | src/http.zig | 23 | ||||
| -rw-r--r-- | src/install.zig | 4 | ||||
| -rw-r--r-- | src/main.zig | 5 |
4 files changed, 44 insertions, 6 deletions
diff --git a/src/Installation.zig b/src/Installation.zig index 4f7a48d..3c056b9 100644 --- a/src/Installation.zig +++ b/src/Installation.zig | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | const curl = @import("curl"); | ||
| 2 | const std = @import("std"); | 1 | const std = @import("std"); |
| 3 | const xdg = @import("xdg"); | 2 | 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,13 +135,23 @@ 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 json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); | 138 | var response = try zelda.get(allocator, "https://ziglang.org/download/index.json"); |
| 139 | defer allocator.free(json_str); | 139 | defer response.deinit(); |
| 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 | } | ||
| 140 | 150 | ||
| 141 | var parser = std.json.Parser.init(allocator, false); | 151 | var parser = std.json.Parser.init(allocator, false); |
| 142 | defer parser.deinit(); | 152 | defer parser.deinit(); |
| 143 | 153 | ||
| 144 | var vt = try parser.parse(json_str); | 154 | var vt = try parser.parse(response.body.?); |
| 145 | defer vt.deinit(); | 155 | defer vt.deinit(); |
| 146 | 156 | ||
| 147 | if (vt.root != .Object) { | 157 | if (vt.root != .Object) { |
diff --git a/src/http.zig b/src/http.zig new file mode 100644 index 0000000..bb3b3ac --- /dev/null +++ b/src/http.zig | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const zelda = @import("zelda"); | ||
| 3 | |||
| 4 | const Allocator = std.mem.Allocator; | ||
| 5 | const File = std.fs.File; | ||
| 6 | |||
| 7 | // TODO: zelda should be able to do this internally so I wouldn't have to allocate memory for the | ||
| 8 | // file | ||
| 9 | pub fn downloadToFile(allocator: Allocator, file: *File, url: []const u8) !void { | ||
| 10 | var response = try zelda.get(allocator, url); | ||
| 11 | defer response.deinit(); | ||
| 12 | |||
| 13 | if (response.status_code.group() != .success) { | ||
| 14 | std.log.err("HTTP Error: {}", .{response.status_code}); | ||
| 15 | return error.HttpError; | ||
| 16 | } | ||
| 17 | |||
| 18 | if (response.body) |body| { | ||
| 19 | try file.writer().writeAll(body); | ||
| 20 | } else { | ||
| 21 | std.log.warn("Downloaded nothing from {s}...", .{url}); | ||
| 22 | } | ||
| 23 | } | ||
diff --git a/src/install.zig b/src/install.zig index 27db4bb..47b804c 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"); | ||
| 3 | const std = @import("std"); | 2 | const std = @import("std"); |
| 4 | const xdg = @import("xdg"); | 3 | 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; |
| @@ -70,7 +70,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I | |||
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | std.log.info("Downloading to /tmp/{s}...", .{filename}); | 72 | std.log.info("Downloading to /tmp/{s}...", .{filename}); |
| 73 | try curl.easyDownloadToFile(&tmpfile, installation.url.?); | 73 | try zup.http.downloadToFile(allocator, &tmpfile, installation.url.?); |
| 74 | 74 | ||
| 75 | std.log.info("Extracting...", .{}); | 75 | std.log.info("Extracting...", .{}); |
| 76 | var archive = try ArchiveRead.init(); | 76 | var archive = try ArchiveRead.init(); |
diff --git a/src/main.zig b/src/main.zig index 197a224..56b1b77 100644 --- a/src/main.zig +++ b/src/main.zig | |||
| @@ -2,6 +2,7 @@ pub const Config = @import("Config.zig"); | |||
| 2 | pub const Installation = @import("Installation.zig"); | 2 | pub const Installation = @import("Installation.zig"); |
| 3 | pub const Installations = Installation.Installations; | 3 | pub const Installations = Installation.Installations; |
| 4 | pub const SubCommand = @import("subcommand.zig").SubCommand; | 4 | pub const SubCommand = @import("subcommand.zig").SubCommand; |
| 5 | pub const http = @import("http.zig"); | ||
| 5 | 6 | ||
| 6 | pub const help = SubCommand(Help); | 7 | pub const help = SubCommand(Help); |
| 7 | pub const install = SubCommand(@import("install.zig")); | 8 | pub const install = SubCommand(@import("install.zig")); |
| @@ -12,6 +13,7 @@ pub const update = SubCommand(@import("update.zig")); | |||
| 12 | pub const version = SubCommand(Version); | 13 | pub const version = SubCommand(Version); |
| 13 | 14 | ||
| 14 | const std = @import("std"); | 15 | const std = @import("std"); |
| 16 | const zelda = @import("zelda"); | ||
| 15 | const zup_config = @import("zup-config"); | 17 | const zup_config = @import("zup-config"); |
| 16 | 18 | ||
| 17 | const ArgIterator = std.process.ArgIterator; | 19 | const ArgIterator = std.process.ArgIterator; |
| @@ -24,6 +26,9 @@ pub fn main() !void { | |||
| 24 | const allocator = gpa.allocator(); | 26 | const allocator = gpa.allocator(); |
| 25 | defer _ = gpa.deinit(); | 27 | defer _ = gpa.deinit(); |
| 26 | 28 | ||
| 29 | // TODO: Make an issue in zelda mentioning this | ||
| 30 | defer zelda.client.global_connection_cache.deinit(); | ||
| 31 | |||
| 27 | var config = try Config.init(allocator); | 32 | var config = try Config.init(allocator); |
| 28 | defer config.deinit(); | 33 | defer config.deinit(); |
| 29 | 34 | ||