From 3b3baf375be63acfd293b1b47a5f49aab4b06a70 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 15 Nov 2022 02:37:38 +0200 Subject: Replace my zig-curl with zelda --- .gitmodules | 6 +++--- README.org | 2 +- build.zig | 12 ++++++++---- libs/curl | 1 - libs/zelda | 1 + src/Installation.zig | 18 ++++++++++++++---- src/http.zig | 23 +++++++++++++++++++++++ src/install.zig | 4 ++-- src/main.zig | 5 +++++ 9 files changed, 57 insertions(+), 15 deletions(-) delete mode 160000 libs/curl create mode 160000 libs/zelda create mode 100644 src/http.zig diff --git a/.gitmodules b/.gitmodules index a9df803..cfc92e7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,9 +4,9 @@ [submodule "libs/xdg"] path = libs/xdg url = https://git.sr.ht/~ukko/zig-xdg -[submodule "libs/curl"] - path = libs/curl - url = https://git.sr.ht/~ukko/zig-curl [submodule "libs/libarchive"] path = libs/libarchive url = https://git.sr.ht/~ukko/zig-libarchive +[submodule "libs/zelda"] + path = libs/zelda + url = https://github.com/haze/zelda.git diff --git a/README.org b/README.org index 427631b..f922886 100644 --- a/README.org +++ b/README.org @@ -9,7 +9,7 @@ ** System libraries - libarchive -- libcurl +- libressl ** Zig version diff --git a/build.zig b/build.zig index fe092db..c99ce32 100644 --- a/build.zig +++ b/build.zig @@ -1,10 +1,11 @@ const builtin = @import("builtin"); const std = @import("std"); +const zelda = @import("libs/zelda/build.zig"); const Builder = std.build.Builder; const SemanticVersion = std.SemanticVersion; -pub fn build(b: *Builder) void { +pub fn build(b: *Builder) !void { const target = b.standardTargetOptions(.{}); const mode = b.standardReleaseOptions(); @@ -15,14 +16,17 @@ pub fn build(b: *Builder) void { exe.setTarget(target); exe.setBuildMode(mode); exe.addOptions("zup-config", config); + // TODO[https://github.com/ziglang/zig/issues/13551]: Remove this + exe.linkSystemLibraryPkgConfigOnly("libcrypto"); + try zelda.link(b, exe, target, mode, true); exe.addPackagePath("clap", "libs/clap/clap.zig"); - exe.addPackagePath("curl", "libs/curl/curl.zig"); exe.addPackagePath("libarchive", "libs/libarchive/libarchive.zig"); exe.addPackagePath("xdg", "libs/xdg/xdg.zig"); exe.addPackagePath("zup", "src/main.zig"); exe.linkLibC(); - exe.linkSystemLibrary("libarchive"); - exe.linkSystemLibrary("libcurl"); + // TODO[https://github.com/ziglang/zig/issues/13551]: + // Replace this with linkSystemLibrary("archive") + exe.linkSystemLibraryPkgConfigOnly("libarchive"); exe.install(); const run_cmd = exe.run(); diff --git a/libs/curl b/libs/curl deleted file mode 160000 index 7f920ec..0000000 --- a/libs/curl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7f920ec5be3e4b336303e93bb164c33f1e440228 diff --git a/libs/zelda b/libs/zelda new file mode 160000 index 0000000..349513e --- /dev/null +++ b/libs/zelda @@ -0,0 +1 @@ +Subproject commit 349513eedb83a69989f5ca0616e69f841cc516a8 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 @@ -const curl = @import("curl"); const std = @import("std"); const xdg = @import("xdg"); +const zelda = @import("zelda"); const zup = @import("zup"); const Allocator = std.mem.Allocator; @@ -135,13 +135,23 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { pub fn getAvailableList(config: Config) !Installations { const allocator = config.allocator; - var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); - defer allocator.free(json_str); + var response = try zelda.get(allocator, "https://ziglang.org/download/index.json"); + defer response.deinit(); + + if (response.status_code.group() != .success) { + std.log.err("HTTP Error while getting Zig download index.json: {}", .{response.status_code}); + return error.HttpError; + } + + if (response.body == null) { + std.log.err("No body response while getting Zig download index.json", .{}); + return error.HttpError; + } var parser = std.json.Parser.init(allocator, false); defer parser.deinit(); - var vt = try parser.parse(json_str); + var vt = try parser.parse(response.body.?); defer vt.deinit(); 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 @@ +const std = @import("std"); +const zelda = @import("zelda"); + +const Allocator = std.mem.Allocator; +const File = std.fs.File; + +// TODO: zelda should be able to do this internally so I wouldn't have to allocate memory for the +// file +pub fn downloadToFile(allocator: Allocator, file: *File, url: []const u8) !void { + var response = try zelda.get(allocator, url); + defer response.deinit(); + + if (response.status_code.group() != .success) { + std.log.err("HTTP Error: {}", .{response.status_code}); + return error.HttpError; + } + + if (response.body) |body| { + try file.writer().writeAll(body); + } else { + std.log.warn("Downloaded nothing from {s}...", .{url}); + } +} 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 @@ const libarchive = @import("libarchive"); -const curl = @import("curl"); const std = @import("std"); const xdg = @import("xdg"); +const zelda = @import("zelda"); const zup = @import("zup"); const Allocator = std.mem.Allocator; @@ -70,7 +70,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I } std.log.info("Downloading to /tmp/{s}...", .{filename}); - try curl.easyDownloadToFile(&tmpfile, installation.url.?); + try zup.http.downloadToFile(allocator, &tmpfile, installation.url.?); std.log.info("Extracting...", .{}); 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"); pub const Installation = @import("Installation.zig"); pub const Installations = Installation.Installations; pub const SubCommand = @import("subcommand.zig").SubCommand; +pub const http = @import("http.zig"); pub const help = SubCommand(Help); pub const install = SubCommand(@import("install.zig")); @@ -12,6 +13,7 @@ pub const update = SubCommand(@import("update.zig")); pub const version = SubCommand(Version); const std = @import("std"); +const zelda = @import("zelda"); const zup_config = @import("zup-config"); const ArgIterator = std.process.ArgIterator; @@ -24,6 +26,9 @@ pub fn main() !void { const allocator = gpa.allocator(); defer _ = gpa.deinit(); + // TODO: Make an issue in zelda mentioning this + defer zelda.client.global_connection_cache.deinit(); + var config = try Config.init(allocator); defer config.deinit(); -- cgit v1.2.3