authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-02 18:15:17 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-02 18:15:17 -07:00
loga9298362417cf023f783440343374ed212ada707
tree7835705f2bd05f067c61112808484885fade1e9b
parent543e3137f4d4f9841089923264ff370c54136822

add aquila support with `aq add` command


3 files changed, 100 insertions(+), 0 deletions(-)

src/cmd/aq.zig created+34
...@@ -0,0 +1,34 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const u = @import("./../util/index.zig");
5
6//
7//
8
9pub const commands = struct {
10 pub const add = @import("./aquila/add.zig");
11};
12
13pub fn execute(args: [][]u8) !void {
14 if (args.len == 0) {
15 std.debug.warn("{s}\n", .{
16 \\This is a subcommand for use with https://github.com/nektro/aquila instances but has no default behavior on its own aside from showing you this nice help text.
17 \\
18 \\The default remote is https://aquila.red.
19 \\
20 \\The subcommands available are:
21 \\ - add Append this package to your dependencies
22 });
23 return;
24 }
25
26 inline for (std.meta.declarations(commands)) |decl| {
27 if (std.mem.eql(u8, args[0], decl.name)) {
28 const cmd = @field(commands, decl.name);
29 try cmd.execute(args[1..]);
30 return;
31 }
32 }
33 std.debug.panic("error: unknown command \"{s}\" for \"zigmod aq\"", .{args[0]});
34}
src/cmd/aquila/add.zig created+65
...@@ -0,0 +1,65 @@
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");
8
9//
10//
11
12pub fn execute(args: [][]u8) !void {
13 const pkg_id = args[0];
14
15 const url = try std.mem.join(gpa, "/", &.{ "https://aquila.red", pkg_id });
16
17 const req = try zfetch.Request.init(gpa, url, null);
18 defer req.deinit();
19
20 var headers = zfetch.Headers.init(gpa);
21 defer headers.deinit();
22 try headers.set("accept", "application/json");
23
24 try req.do(.GET, headers, null);
25
26 const r = req.reader();
27 const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize));
28 const val = try json.parse(gpa, body_content);
29
30 if (val.get("message")) |msg| {
31 std.log.err("server: {s}", .{msg.String});
32 return;
33 }
34
35 const name = val.get(.{ "pkg", "name" }).?.String;
36
37 const versions = val.get("versions").?.Array;
38 const to_add = versions[versions.len - 1];
39
40 const self_module = try u.ModFile.init(gpa, "zig.mod");
41 for (self_module.deps) |dep| {
42 if (std.mem.eql(u8, dep.name, name)) {
43 std.log.warn("dependency with name '{s}' already exists in your dependencies", .{name});
44 }
45 }
46 for (self_module.devdeps) |dep| {
47 if (std.mem.eql(u8, dep.name, name)) {
48 std.log.warn("dependency with name '{s}' already exists in your dependencies", .{name});
49 }
50 }
51
52 const file = try std.fs.cwd().openFile("zig.mod", .{ .read = true, .write = true });
53 try file.seekTo(try file.getEndPos());
54
55 const v_hash = to_add.get("tar_hash").?.String;
56 const v_maj = to_add.get("real_major").?.Number;
57 const v_min = to_add.get("real_minor").?.Number;
58
59 const file_w = file.writer();
60 try file_w.print("\n", .{});
61 try file_w.print(" - src: http https://aquila.red/{s}/v{d}.{d}.tar.gz _ {d} {d}\n", .{ pkg_id, v_maj, v_min, v_maj, v_min });
62 try file_w.print(" version: {s}\n", .{v_hash});
63
64 std.log.info("Successfully added package {s}", .{pkg_id});
65}
src/lib.zig+1
...@@ -8,4 +8,5 @@ pub const commands = struct {...@@ -8,4 +8,5 @@ pub const commands = struct {
8 pub const sum = @import("./cmd/sum.zig");8 pub const sum = @import("./cmd/sum.zig");
9 pub const zpm = @import("./cmd/zpm.zig");9 pub const zpm = @import("./cmd/zpm.zig");
10 pub const license = @import("./cmd/license.zig");10 pub const license = @import("./cmd/license.zig");
11 pub const aq = @import("./cmd/aq.zig");
11};12};