Refactor configuration handling and integrate command-line argument parsing with args.zig
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -12,4 +12,4 @@ docgen_tmp/
|
|||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
zocket/*.md
|
config/*.md
|
||||||
|
|||||||
24
build.zig
24
build.zig
@@ -5,6 +5,11 @@ pub fn build(b: *std.Build) void
|
|||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const version = getGitVersion(b);
|
||||||
|
|
||||||
|
const options = b.addOptions();
|
||||||
|
options.addOption([]const u8, "version", version);
|
||||||
|
|
||||||
// deps
|
// deps
|
||||||
|
|
||||||
const toml_dep = b.dependency("toml", .{
|
const toml_dep = b.dependency("toml", .{
|
||||||
@@ -12,6 +17,11 @@ pub fn build(b: *std.Build) void
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const args_dep = b.dependency("args", .{
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
const mod = b.addModule("zocket", .{
|
const mod = b.addModule("zocket", .{
|
||||||
@@ -20,6 +30,7 @@ pub fn build(b: *std.Build) void
|
|||||||
});
|
});
|
||||||
|
|
||||||
mod.addImport("toml", toml_dep.module("toml"));
|
mod.addImport("toml", toml_dep.module("toml"));
|
||||||
|
mod.addImport("args", args_dep.module("args"));
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "zocket",
|
.name = "zocket",
|
||||||
@@ -33,6 +44,8 @@ pub fn build(b: *std.Build) void
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
exe.root_module.addOptions("build_options", options);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
@@ -63,3 +76,14 @@ pub fn build(b: *std.Build) void
|
|||||||
test_step.dependOn(&run_mod_tests.step);
|
test_step.dependOn(&run_mod_tests.step);
|
||||||
test_step.dependOn(&run_exe_tests.step);
|
test_step.dependOn(&run_exe_tests.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn getGitVersion(b: *std.Build) []const u8
|
||||||
|
{
|
||||||
|
const result = b.runAllowFail(
|
||||||
|
&.{ "git", "describe", "--tags", "--always", "--dirty" },
|
||||||
|
undefined,
|
||||||
|
.inherit,
|
||||||
|
) catch return "0.0.0-unknown";
|
||||||
|
|
||||||
|
return b.dupe(std.mem.trim(u8, result, " \n\r\t"));
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,10 @@
|
|||||||
.url = "git+https://github.com/sam701/zig-toml?ref=zig-0.16#8685923e32e8b8a795eb2715684236975a70faed",
|
.url = "git+https://github.com/sam701/zig-toml?ref=zig-0.16#8685923e32e8b8a795eb2715684236975a70faed",
|
||||||
.hash = "toml-0.3.0-bV14BRmKAQAWR0FT0KUKFwVJTImaFhWjSe6HfSMtNZOH",
|
.hash = "toml-0.3.0-bV14BRmKAQAWR0FT0KUKFwVJTImaFhWjSe6HfSMtNZOH",
|
||||||
},
|
},
|
||||||
|
.args = .{
|
||||||
|
.url = "https://github.com/muhammad-fiaz/args.zig/archive/refs/tags/0.0.8.tar.gz",
|
||||||
|
.hash = "args-0.0.8-8hzgbsBECgD3C0uPaFNcK9UFTZ210rWU4XIhopR8snoA",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
|
|||||||
39
src/main.zig
39
src/main.zig
@@ -2,6 +2,10 @@ const zocket = @import("zocket");
|
|||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
|
const options = @import("build_options");
|
||||||
|
|
||||||
|
|
||||||
|
const args = zocket.deps.args;
|
||||||
|
|
||||||
pub fn main(init: std.process.Init) !void
|
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;
|
var arena: std.heap.DebugAllocator(.{}) = .init;
|
||||||
defer _ = arena.deinit();
|
defer _ = arena.deinit();
|
||||||
|
|
||||||
try run(io, arena.allocator(), "zocket/config.toml");
|
try run(io, arena.allocator(), "config/config.toml");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
|
|
||||||
const allocator = arena.allocator();
|
const allocator = arena.allocator();
|
||||||
|
|
||||||
var tmpArena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
var tmpArena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
||||||
const tmpAllocator = tmpArena.allocator();
|
const tmpAllocator = tmpArena.allocator();
|
||||||
|
|
||||||
const exe = try std.process.executableDirPathAlloc(io, tmpAllocator);
|
var parser = try args.ArgumentParser.init(tmpAllocator, .{
|
||||||
const path = try std.fs.path.join(allocator, &.{ exe, "zocket/config.toml" });
|
.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();
|
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
|
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();
|
defer parsed.deinit();
|
||||||
|
|
||||||
const configuration = parsed.value;
|
const configuration = parsed.value;
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
const std = @import("std");
|
pub const deps = struct {
|
||||||
|
pub const args = @import("args");
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
pub const config = @import("config/config.zig");
|
pub const config = @import("config/config.zig");
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
|||||||
Reference in New Issue
Block a user