summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Installation.zig18
-rw-r--r--src/http.zig23
-rw-r--r--src/install.zig4
-rw-r--r--src/main.zig5
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 @@
1const curl = @import("curl");
2const std = @import("std"); 1const std = @import("std");
3const xdg = @import("xdg"); 2const xdg = @import("xdg");
3const zelda = @import("zelda");
4const zup = @import("zup"); 4const zup = @import("zup");
5 5
6const Allocator = std.mem.Allocator; 6const Allocator = std.mem.Allocator;
@@ -135,13 +135,23 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool {
135pub fn getAvailableList(config: Config) !Installations { 135pub 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 @@
1const std = @import("std");
2const zelda = @import("zelda");
3
4const Allocator = std.mem.Allocator;
5const 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
9pub 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 @@
1const libarchive = @import("libarchive"); 1const libarchive = @import("libarchive");
2const curl = @import("curl");
3const std = @import("std"); 2const std = @import("std");
4const xdg = @import("xdg"); 3const xdg = @import("xdg");
4const zelda = @import("zelda");
5const zup = @import("zup"); 5const zup = @import("zup");
6 6
7const Allocator = std.mem.Allocator; 7const 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");
2pub const Installation = @import("Installation.zig"); 2pub const Installation = @import("Installation.zig");
3pub const Installations = Installation.Installations; 3pub const Installations = Installation.Installations;
4pub const SubCommand = @import("subcommand.zig").SubCommand; 4pub const SubCommand = @import("subcommand.zig").SubCommand;
5pub const http = @import("http.zig");
5 6
6pub const help = SubCommand(Help); 7pub const help = SubCommand(Help);
7pub const install = SubCommand(@import("install.zig")); 8pub const install = SubCommand(@import("install.zig"));
@@ -12,6 +13,7 @@ pub const update = SubCommand(@import("update.zig"));
12pub const version = SubCommand(Version); 13pub const version = SubCommand(Version);
13 14
14const std = @import("std"); 15const std = @import("std");
16const zelda = @import("zelda");
15const zup_config = @import("zup-config"); 17const zup_config = @import("zup-config");
16 18
17const ArgIterator = std.process.ArgIterator; 19const 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