authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-06 01:42:10 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-06 01:42:10 -07:00
log7ff589bc32a093732690f3187d042c16f32cf171
treeb60674b64fa70238d8f048050f6c1c1d36210434
parent9a546e72d7176dc7a2e02bb316cceb21acfd6066

cmd: add `zpm showjson` command


2 files changed, 35 insertions(+), 0 deletions(-)

src/cmd/zpm.zig+16
......@@ -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//
......@@ -8,8 +11,11 @@ const u = @import("./../util/index.zig");
811
912pub const commands = struct {
1013 pub const add = @import("./zpm_add.zig");
14 pub const showjson = @import("./zpm/showjson.zig");
1115};
1216
17pub const server_root = "https://zpm.random-projects.net/api";
18
1319pub fn execute(args: [][]u8) !void {
1420 if (args.len == 0) {
1521 std.debug.warn("{s}\n", .{
......@@ -32,3 +38,13 @@ pub fn execute(args: [][]u8) !void {
3238 }
3339 std.debug.panic("error: unknown command \"{s}\" for \"zigmod zpm\"", .{args[0]});
3440}
41
42pub fn server_fetch(url: []const u8) !json.Value {
43 const req = try zfetch.Request.init(gpa, url, null);
44 defer req.deinit();
45 try req.do(.GET, null, null);
46 const r = req.reader();
47 const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize));
48 const val = try json.parse(gpa, body_content);
49 return val;
50}
src/cmd/zpm/showjson.zig created+19
......@@ -0,0 +1,19 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const zfetch = @import("zfetch");
5const json = @import("json");
6
7const u = @import("./../util/index.zig");
8const zpm = @import("./../zpm.zig");
9
10//
11//
12
13pub fn execute(args: [][]u8) !void {
14 const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, args[0] });
15 const val = try zpm.server_fetch(url);
16
17 const out = std.io.getStdOut().writer();
18 try out.print("{}\n", .{val});
19}