summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: e65516f883cf96b89db3fca61032a8435389f10a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const std = @import("std");
const builtin = @import("builtin");
const zits = @import("zits");

const Message = zits.MessageParser.Message;
const Server = zits.Server;

const serve = @import("./subcommand/serve.zig").main;

const help =
    \\High Performance NATS compatible client and server.
    \\
    \\Commands:
    \\  serve    Serve a high performance NATS compatible server
    \\  publish  Publish a message over the NATS protocol
    \\  help     Show this output
    \\
    \\Global Flags:
    \\  -h, --help    Show context-sensitive help
    \\
;

const Subcommand = enum {
    serve,
    publish,
    help,
};

const to_subcommand: std.StaticStringMap(Subcommand) = .initComptime(.{
    .{ @tagName(.serve), .serve },
    .{ "srv", .serve },
    .{ @tagName(.publish), .publish },
    .{ "pub", .publish },
    .{ @tagName(.help), .help },
});

pub fn main(init: std.process.Init) !void {
    const io = init.io;
    const args = try init.minimal.args.toSlice(init.arena.allocator());

    if (args.len == 1) {
        try std.Io.File.stdout().writeStreamingAll(io, help);
        return;
    }

    switch (to_subcommand.get(args[1]) orelse .help) {
        .serve => {
            try serve(init.gpa, io, args[2..]);
            return;
        },
        .publish => {
            std.debug.print("Unimplemented\n", .{});
        },
        else => {
            try std.Io.File.stdout().writeStreamingAll(io, help);
            return;
        },
    }
}

pub const std_options: std.Options = .{
    // By default, in safe build modes, the standard library will attach a segfault handler to the program to
    // print a helpful stack trace if a segmentation fault occurs. Here, we can disable this, or even enable
    // it in unsafe build modes.
    .enable_segfault_handler = true,
    // This is the logging function used by `std.log`.
    .logFn = myLogFn,
};

fn myLogFn(
    comptime level: std.log.Level,
    comptime scope: @EnumLiteral(),
    comptime format: []const u8,
    args: anytype,
) void {
    if (scope == .zits) {
        std.log.defaultLog(level, std.log.default_log_scope, format, args);
    } else {
        std.log.defaultLog(level, scope, format, args);
    }
}