aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
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");
+ }
}