Refactor configuration handling and integrate command-line argument parsing with args.zig
This commit is contained in:
39
src/main.zig
39
src/main.zig
@@ -2,6 +2,10 @@ const zocket = @import("zocket");
|
||||
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const options = @import("build_options");
|
||||
|
||||
|
||||
const args = zocket.deps.args;
|
||||
|
||||
pub fn main(init: std.process.Init) !void
|
||||
{
|
||||
@@ -12,20 +16,36 @@ pub fn main(init: std.process.Init) !void
|
||||
var arena: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = arena.deinit();
|
||||
|
||||
try run(io, arena.allocator(), "zocket/config.toml");
|
||||
try run(io, arena.allocator(), "config/config.toml");
|
||||
}
|
||||
else
|
||||
{
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const allocator = arena.allocator();
|
||||
|
||||
var tmpArena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
||||
const tmpAllocator = tmpArena.allocator();
|
||||
|
||||
const exe = try std.process.executableDirPathAlloc(io, tmpAllocator);
|
||||
const path = try std.fs.path.join(allocator, &.{ exe, "zocket/config.toml" });
|
||||
var parser = try args.ArgumentParser.init(tmpAllocator, .{
|
||||
.name = "zocket",
|
||||
.version = options.version,
|
||||
.description = "A lightweight Zig WebSocket server and mesh routing daemon.",
|
||||
});
|
||||
|
||||
try parser.addOption("path", .{
|
||||
.short = 'p',
|
||||
.help = "Configuration-File Path (*.toml)",
|
||||
});
|
||||
|
||||
var result = try parser.parseProcess(init);
|
||||
|
||||
const tmpPath = result.getString("path") orelse blk: {
|
||||
const exe = try std.process.executableDirPathAlloc(io, tmpAllocator);
|
||||
break :blk try std.Io.Dir.path.join(tmpAllocator, &.{ exe, "config/config.toml" });
|
||||
};
|
||||
|
||||
const path = try allocator.dupe(u8, tmpPath);
|
||||
|
||||
tmpArena.deinit();
|
||||
|
||||
@@ -35,7 +55,16 @@ pub fn main(init: std.process.Init) !void
|
||||
|
||||
fn run(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void
|
||||
{
|
||||
const parsed = try zocket.config.parse(io, allocator, path);
|
||||
const parsed = zocket.config.parse(io, allocator, path) catch |err|
|
||||
{
|
||||
const msg: []const u8 = switch (err) {
|
||||
error.FileNotFound => "File not found",
|
||||
error.AccessDenied => "Permission denied reading",
|
||||
else => "Unexpected error opening",
|
||||
};
|
||||
std.log.err("{s}: '{s}' ({any})\n", .{ msg, path, err });
|
||||
std.process.exit(1);
|
||||
};
|
||||
defer parsed.deinit();
|
||||
|
||||
const configuration = parsed.value;
|
||||
|
||||
Reference in New Issue
Block a user