summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-11-13 17:29:08 -0500
committerRobby Zambito <contact@robbyzambito.me>2025-11-18 13:25:01 -0500
commit6cfaba958ebd552307b93a5fd377bf9522a893e3 (patch)
treef0d38edcd90b00d0bb53c04ad5cd463dc5bfa26a /src/main.zig
parent432b06fc6a09a5a8540335db5b9e5471b8e11f85 (diff)
Print some basic server info to stderr
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig121
1 files changed, 103 insertions, 18 deletions
diff --git a/src/main.zig b/src/main.zig
index c1bfb21..2b983e1 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,27 +1,112 @@
const std = @import("std");
const zits = @import("zits");
+const clap = @import("clap");
+
+const SubCommands = enum {
+ help,
+ serve,
+ @"pub",
+};
+
+const main_parsers = .{
+ .command = clap.parsers.enumeration(SubCommands),
+};
+
+// The parameters for `main`. Parameters for the subcommands are specified further down.
+const main_params = clap.parseParamsComptime(
+ \\-h, --help Display this help and exit.
+ \\<command>
+ \\
+);
+
+// To pass around arguments returned by clap, `clap.Result` and `clap.ResultEx` can be used to
+// get the return type of `clap.parse` and `clap.parseEx`.
+const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers);
pub fn main() !void {
- // Prints to stderr, ignoring potential errors.
- std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
- try zits.bufferedPrint();
-}
+ var dba = std.heap.DebugAllocator(.{}){};
+ defer _ = dba.deinit();
+ const gpa = dba.allocator();
+
+ var iter = try std.process.ArgIterator.initWithAllocator(gpa);
+ defer iter.deinit();
+
+ _ = iter.next();
+
+ var diag = clap.Diagnostic{};
+ var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{
+ .diagnostic = &diag,
+ .allocator = gpa,
-test "simple test" {
- const gpa = std.testing.allocator;
- var list: std.ArrayList(i32) = .empty;
- defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
- try list.append(gpa, 42);
- try std.testing.expectEqual(@as(i32, 42), list.pop());
+ // Terminate the parsing of arguments after parsing the first positional (0 is passed
+ // here because parsed positionals are, like slices and arrays, indexed starting at 0).
+ //
+ // This will terminate the parsing after parsing the subcommand enum and leave `iter`
+ // not fully consumed. It can then be reused to parse the arguments for subcommands.
+ .terminating_positional = 0,
+ }) catch |err| {
+ try diag.reportToFile(.stderr(), err);
+ return err;
+ };
+ defer res.deinit();
+
+ if (res.args.help != 0)
+ return clap.helpToFile(.stderr(), clap.Help, &main_params, .{});
+
+ const command = res.positionals[0] orelse return error.MissingCommand;
+ switch (command) {
+ .help => return clap.helpToFile(.stderr(), clap.Help, &main_params, .{}),
+ .serve => try serverMain(gpa, &iter, res),
+ .@"pub" => unreachable,
+ }
}
-test "fuzz example" {
- const Context = struct {
- fn testOne(context: @This(), input: []const u8) anyerror!void {
- _ = context;
- // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
- try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
- }
+const ServerInfo = struct {
+ /// The unique identifier of the NATS server.
+ server_id: []const u8,
+ /// The name of the NATS server.
+ server_name: []const u8,
+ /// The version of NATS.
+ version: []const u8,
+ /// The version of golang the NATS server was built with.
+ go: []const u8 = "0.0.0",
+ /// The IP address used to start the NATS server,
+ /// by default this will be 0.0.0.0 and can be
+ /// configured with -client_advertise host:port.
+ host: []const u8 = "0.0.0.0",
+ /// The port number the NATS server is configured
+ /// to listen on.
+ port: u16 = 6868,
+ /// Whether the server supports headers.
+ headers: bool = false,
+ /// Maximum payload size, in bytes, that the server
+ /// will accept from the client.
+ max_payload: u64,
+ /// An integer indicating the protocol version of
+ /// the server. The server version 1.2.0 sets this
+ /// to 1 to indicate that it supports the "Echo"
+ /// feature.
+ proto: u32 = 1,
+};
+
+fn serverMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
+ _ = gpa;
+ _ = iter;
+ _ = main_args;
+
+ const info: ServerInfo = .{
+ .server_id = "foo",
+ .server_name = "bar",
+ .version = "6.9.0",
+ .max_payload = 6969,
};
- try std.testing.fuzz(Context{}, Context.testOne, .{});
+
+ var out_fs = std.fs.File.stderr().writer(&.{});
+ const out = &out_fs.interface;
+
+ try std.json.Stringify.value(info, .{
+ .whitespace = .indent_2,
+ }, out);
+
+ std.debug.print("in serverMain\n", .{});
}