diff options
| author | 2022-11-15 02:37:38 +0200 | |
|---|---|---|
| committer | 2022-11-15 02:37:38 +0200 | |
| commit | 3b3baf375be63acfd293b1b47a5f49aab4b06a70 (patch) | |
| tree | 102e254f6160a9a1f28dd9a51f74e93747567505 | |
| parent | Update clap (diff) | |
| download | zup-3b3baf375be63acfd293b1b47a5f49aab4b06a70.tar.gz zup-3b3baf375be63acfd293b1b47a5f49aab4b06a70.tar.xz zup-3b3baf375be63acfd293b1b47a5f49aab4b06a70.zip | |
Replace my zig-curl with zelda
| -rw-r--r-- | .gitmodules | 6 | ||||
| -rw-r--r-- | README.org | 2 | ||||
| -rw-r--r-- | build.zig | 12 | ||||
| m--------- | libs/curl | 0 | ||||
| m--------- | libs/zelda | 0 | ||||
| -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 |
9 files changed, 56 insertions, 14 deletions
diff --git a/.gitmodules b/.gitmodules index a9df803..cfc92e7 100644 --- a/.gitmodules +++ b/.gitmodules | |||
| @@ -4,9 +4,9 @@ | |||
| 4 | [submodule "libs/xdg"] | 4 | [submodule "libs/xdg"] |
| 5 | path = libs/xdg | 5 | path = libs/xdg |
| 6 | url = https://git.sr.ht/~ukko/zig-xdg | 6 | url = https://git.sr.ht/~ukko/zig-xdg |
| 7 | [submodule "libs/curl"] | ||
| 8 | path = libs/curl | ||
| 9 | url = https://git.sr.ht/~ukko/zig-curl | ||
| 10 | [submodule "libs/libarchive"] | 7 | [submodule "libs/libarchive"] |
| 11 | path = libs/libarchive | 8 | path = libs/libarchive |
| 12 | url = https://git.sr.ht/~ukko/zig-libarchive | 9 | url = https://git.sr.ht/~ukko/zig-libarchive |
| 10 | [submodule "libs/zelda"] | ||
| 11 | path = libs/zelda | ||
| 12 | url = https://github.com/haze/zelda.git | ||
| @@ -9,7 +9,7 @@ | |||
| 9 | ** System libraries | 9 | ** System libraries |
| 10 | 10 | ||
| 11 | - libarchive | 11 | - libarchive |
| 12 | - libcurl | 12 | - libressl |
| 13 | 13 | ||
| 14 | ** Zig version | 14 | ** Zig version |
| 15 | 15 | ||
| @@ -1,10 +1,11 @@ | |||
| 1 | const builtin = @import("builtin"); | 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | const zelda = @import("libs/zelda/build.zig"); | ||
| 3 | 4 | ||
| 4 | const Builder = std.build.Builder; | 5 | const Builder = std.build.Builder; |
| 5 | const SemanticVersion = std.SemanticVersion; | 6 | const SemanticVersion = std.SemanticVersion; |
| 6 | 7 | ||
| 7 | pub fn build(b: *Builder) void { | 8 | pub fn build(b: *Builder) !void { |
| 8 | const target = b.standardTargetOptions(.{}); | 9 | const target = b.standardTargetOptions(.{}); |
| 9 | const mode = b.standardReleaseOptions(); | 10 | const mode = b.standardReleaseOptions(); |
| 10 | 11 | ||
| @@ -15,14 +16,17 @@ pub fn build(b: *Builder) void { | |||
| 15 | exe.setTarget(target); | 16 | exe.setTarget(target); |
| 16 | exe.setBuildMode(mode); | 17 | exe.setBuildMode(mode); |
| 17 | exe.addOptions("zup-config", config); | 18 | exe.addOptions("zup-config", config); |
| 19 | // TODO[https://github.com/ziglang/zig/issues/13551]: Remove this | ||
| 20 | exe.linkSystemLibraryPkgConfigOnly("libcrypto"); | ||
| 21 | try zelda.link(b, exe, target, mode, true); | ||
| 18 | exe.addPackagePath("clap", "libs/clap/clap.zig"); | 22 | exe.addPackagePath("clap", "libs/clap/clap.zig"); |
| 19 | exe.addPackagePath("curl", "libs/curl/curl.zig"); | ||
| 20 | exe.addPackagePath("libarchive", "libs/libarchive/libarchive.zig"); | 23 | exe.addPackagePath("libarchive", "libs/libarchive/libarchive.zig"); |
| 21 | exe.addPackagePath("xdg", "libs/xdg/xdg.zig"); | 24 | exe.addPackagePath("xdg", "libs/xdg/xdg.zig"); |
| 22 | exe.addPackagePath("zup", "src/main.zig"); | 25 | exe.addPackagePath("zup", "src/main.zig"); |
| 23 | exe.linkLibC(); | 26 | exe.linkLibC(); |
| 24 | exe.linkSystemLibrary("libarchive"); | 27 | // TODO[https://github.com/ziglang/zig/issues/13551]: |
| 25 | exe.linkSystemLibrary("libcurl"); | 28 | // Replace this with linkSystemLibrary("archive") |
| 29 | exe.linkSystemLibraryPkgConfigOnly("libarchive"); | ||
| 26 | exe.install(); | 30 | exe.install(); |
| 27 | 31 | ||
| 28 | const run_cmd = exe.run(); | 32 | const run_cmd = exe.run(); |
diff --git a/libs/curl b/libs/curl deleted file mode 160000 | |||
| Subproject 7f920ec5be3e4b336303e93bb164c33f1e44022 | |||
diff --git a/libs/zelda b/libs/zelda new file mode 160000 | |||
| Subproject 349513eedb83a69989f5ca0616e69f841cc516a | |||
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 | ||