summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2022-11-15 13:55:07 +0200
committerGravatar Uko Kokņevičs2022-11-15 13:55:07 +0200
commit33226c20c41e3fe2de3330b6692c90bc4818b119 (patch)
tree4e66df2da513eb7270c1d0ebc23aef3e264099f8 /src
parentReplace my zig-curl with zelda (diff)
downloadzup-33226c20c41e3fe2de3330b6692c90bc4818b119.tar.gz
zup-33226c20c41e3fe2de3330b6692c90bc4818b119.tar.xz
zup-33226c20c41e3fe2de3330b6692c90bc4818b119.zip
Don't download the archive to a file
Diffstat (limited to 'src')
-rw-r--r--src/http.zig23
-rw-r--r--src/install.zig32
-rw-r--r--src/main.zig1
3 files changed, 13 insertions, 43 deletions
diff --git a/src/http.zig b/src/http.zig
deleted file mode 100644
index bb3b3ac..0000000
--- a/src/http.zig
+++ /dev/null
@@ -1,23 +0,0 @@
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 47b804c..5eb5a29 100644
--- a/src/install.zig
+++ b/src/install.zig
@@ -51,26 +51,20 @@ 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 const filename = std.fs.path.basename(installation.url.?); 54
55 55 std.log.info("Downloading from {s}...", .{installation.url.?});
56 // TODO: Platform-agnostic tempfile creation 56 var response = try zelda.get(allocator, installation.url.?);
57 var tmpname = try std.fmt.allocPrintZ(allocator, "/tmp/{s}", .{filename}); 57 defer response.deinit();
58 defer allocator.free(tmpname); 58
59 59 if (response.status_code.group() != .success) {
60 var tmpdir = try std.fs.openDirAbsolute(std.fs.path.dirname(tmpname).?, .{}); 60 std.log.err("HTTP Error: {}", .{response.status_code});
61 defer tmpdir.close(); 61 return error.HttpError;
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 };
70 } 62 }
71 63
72 std.log.info("Downloading to /tmp/{s}...", .{filename}); 64 if (response.body == null) {
73 try zup.http.downloadToFile(allocator, &tmpfile, installation.url.?); 65 std.log.err("HTTP Error, no body received", .{});
66 return error.HttpError;
67 }
74 68
75 std.log.info("Extracting...", .{}); 69 std.log.info("Extracting...", .{});
76 var archive = try ArchiveRead.init(); 70 var archive = try ArchiveRead.init();
@@ -78,7 +72,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I
78 72
79 try archive.supportFilter(.all); 73 try archive.supportFilter(.all);
80 try archive.supportFormat(.all); 74 try archive.supportFormat(.all);
81 try archive.openFilename(tmpname, 10240); 75 try archive.openMemory(response.body.?);
82 76
83 while (try archive.nextHeader()) |entry_c| { 77 while (try archive.nextHeader()) |entry_c| {
84 var entry = entry_c; 78 var entry = entry_c;
diff --git a/src/main.zig b/src/main.zig
index 56b1b77..11aae8f 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2,7 +2,6 @@ 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");
6 5
7pub const help = SubCommand(Help); 6pub const help = SubCommand(Help);
8pub const install = SubCommand(@import("install.zig")); 7pub const install = SubCommand(@import("install.zig"));