aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-01-27 20:31:01 +0900
committernsfisis <nsfisis@gmail.com>2026-01-27 20:31:01 +0900
commit9ddb831241a150e83fa18d1f681c264118c91fbc (patch)
tree096a787112c8c5df37ecd4fd5cac2430fcd2478f /src
parent225cb99c09ec01fac5cfecf858040f53ee5cc32b (diff)
downloadzgjq-9ddb831241a150e83fa18d1f681c264118c91fbc.tar.gz
zgjq-9ddb831241a150e83fa18d1f681c264118c91fbc.tar.zst
zgjq-9ddb831241a150e83fa18d1f681c264118c91fbc.zip
implement minimal cli
Diffstat (limited to 'src')
-rw-r--r--src/main.zig34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/main.zig b/src/main.zig
index 1d4f8bf..73d7886 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,6 +1,36 @@
const std = @import("std");
-const zgjq = @import("zgjq");
+const jq = @import("zgjq").jq;
+const jv = @import("zgjq").jv;
pub fn main() !void {
- // TODO
+ const allocator = std.heap.smp_allocator;
+
+ const args = try std.process.argsAlloc(allocator);
+ defer std.process.argsFree(allocator, args);
+
+ if (args.len < 2) {
+ try std.fs.File.stderr().writeAll("usage: zgjq <query>\n");
+ std.process.exit(1);
+ }
+ const query = args[1];
+
+ const input = try std.fs.File.stdin().readToEndAlloc(allocator, std.math.maxInt(usize));
+ defer allocator.free(input);
+
+ const parsed = try jv.parse(allocator, input);
+ defer parsed.deinit();
+ const json = parsed.value;
+
+ var runtime = try jq.Runtime.init(allocator);
+ defer runtime.deinit();
+ try runtime.compileFromSlice(query);
+ try runtime.start(json);
+
+ const stdout = std.fs.File.stdout();
+ while (try runtime.next()) |result| {
+ const output = try jv.stringify(allocator, result);
+ defer allocator.free(output);
+ try stdout.writeAll(output);
+ try stdout.writeAll("\n");
+ }
}