authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-02 23:10:22 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-02 23:10:22 -07:00
logbcdb68005ebf31399be9acf6b6897180d4efcaaf
treeba7a8e54a858b87655ddd51f2806fc4f8f42e6a4
parent2a1a595dec91552d05b2f0c6c666f14dbb301e70

move server fetch into aq command


2 files changed, 27 insertions(+), 21 deletions(-)

src/cmd/aq.zig+26
......@@ -1,6 +1,9 @@
11const std = @import("std");
22const gpa = std.heap.c_allocator;
33
4const zfetch = @import("zfetch");
5const json = @import("json");
6
47const u = @import("./../util/index.zig");
58
69//
......@@ -21,6 +24,8 @@ pub fn execute(args: [][]u8) !void {
2124 \\
2225 \\The subcommands available are:
2326 \\ - add Append this package to your dependencies
27 \\ - update Check your zig.mod dependencies for new versions
28 \\ - modile Print the zig.mod text for a new version
2429 });
2530 return;
2631 }
......@@ -34,3 +39,24 @@ pub fn execute(args: [][]u8) !void {
3439 }
3540 std.debug.panic("error: unknown command \"{s}\" for \"zigmod aq\"", .{args[0]});
3641}
42
43pub fn server_fetch(url: []const u8) !json.Value {
44 const req = try zfetch.Request.init(gpa, url, null);
45 defer req.deinit();
46
47 var headers = zfetch.Headers.init(gpa);
48 defer headers.deinit();
49 try headers.set("accept", "application/json");
50
51 try req.do(.GET, headers, null);
52
53 const r = req.reader();
54 const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize));
55 const val = try json.parse(gpa, body_content);
56
57 if (val.get("message")) |msg| {
58 std.log.err("server: {s}", .{msg.String});
59 return error.AquilaBadResponse;
60 }
61 return val;
62}
src/cmd/aquila/add.zig+1-21
......@@ -1,9 +1,6 @@
11const std = @import("std");
22const gpa = std.heap.c_allocator;
33
4const zfetch = @import("zfetch");
5const json = @import("json");
6
74const u = @import("./../../util/index.zig");
85const aq = @import("./../aq.zig");
96
......@@ -14,24 +11,7 @@ pub fn execute(args: [][]u8) !void {
1411 const pkg_id = args[0];
1512
1613 const url = try std.mem.join(gpa, "/", &.{ aq.server_root, pkg_id });
17
18 const req = try zfetch.Request.init(gpa, url, null);
19 defer req.deinit();
20
21 var headers = zfetch.Headers.init(gpa);
22 defer headers.deinit();
23 try headers.set("accept", "application/json");
24
25 try req.do(.GET, headers, null);
26
27 const r = req.reader();
28 const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize));
29 const val = try json.parse(gpa, body_content);
30
31 if (val.get("message")) |msg| {
32 std.log.err("server: {s}", .{msg.String});
33 return;
34 }
14 const val = try aq.server_fetch(url);
3515
3616 const name = val.get(.{ "pkg", "name" }).?.String;
3717