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 @@...@@ -1,6 +1,9 @@
1const std = @import("std");1const std = @import("std");
2const gpa = std.heap.c_allocator;2const gpa = std.heap.c_allocator;
33
4const zfetch = @import("zfetch");
5const json = @import("json");
6
4const u = @import("./../util/index.zig");7const u = @import("./../util/index.zig");
58
6//9//
...@@ -21,6 +24,8 @@ pub fn execute(args: [][]u8) !void {...@@ -21,6 +24,8 @@ pub fn execute(args: [][]u8) !void {
21 \\24 \\
22 \\The subcommands available are:25 \\The subcommands available are:
23 \\ - add Append this package to your dependencies26 \\ - 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
24 });29 });
25 return;30 return;
26 }31 }
...@@ -34,3 +39,24 @@ pub fn execute(args: [][]u8) !void {...@@ -34,3 +39,24 @@ pub fn execute(args: [][]u8) !void {
34 }39 }
35 std.debug.panic("error: unknown command \"{s}\" for \"zigmod aq\"", .{args[0]});40 std.debug.panic("error: unknown command \"{s}\" for \"zigmod aq\"", .{args[0]});
36}41}
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 @@...@@ -1,9 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const gpa = std.heap.c_allocator;2const gpa = std.heap.c_allocator;
33
4const zfetch = @import("zfetch");
5const json = @import("json");
6
7const u = @import("./../../util/index.zig");4const u = @import("./../../util/index.zig");
8const aq = @import("./../aq.zig");5const aq = @import("./../aq.zig");
96
...@@ -14,24 +11,7 @@ pub fn execute(args: [][]u8) !void {...@@ -14,24 +11,7 @@ pub fn execute(args: [][]u8) !void {
14 const pkg_id = args[0];11 const pkg_id = args[0];
1512
16 const url = try std.mem.join(gpa, "/", &.{ aq.server_root, pkg_id });13 const url = try std.mem.join(gpa, "/", &.{ aq.server_root, pkg_id });
1714 const val = try aq.server_fetch(url);
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 }
3515
36 const name = val.get(.{ "pkg", "name" }).?.String;16 const name = val.get(.{ "pkg", "name" }).?.String;
3717