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 --- src/Installation.zig | 18 ++++++++++++++---- src/http.zig | 23 +++++++++++++++++++++++ src/install.zig | 4 ++-- src/main.zig | 5 +++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/http.zig (limited to 'src') 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