From 9ddb831241a150e83fa18d1f681c264118c91fbc Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 27 Jan 2026 20:31:01 +0900 Subject: implement minimal cli --- justfile | 3 +++ src/main.zig | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index 5e38639..96c3831 100644 --- a/justfile +++ b/justfile @@ -4,6 +4,9 @@ help: build: fmt @zig build +run QUERY: + @zig build run -- {{QUERY}} + test: fmt @zig build test 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 \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"); + } } -- cgit v1.3-1-g0d28