48 lines
1.2 KiB
Zig
48 lines
1.2 KiB
Zig
|
const std = @import("std");
|
||
|
|
||
|
const Config = @This();
|
||
|
|
||
|
elevation_mask: f64 = 19,
|
||
|
rotctld: AddressPort = .{ .address = "127.0.0.1", .port = 4533 },
|
||
|
listen: AddressPort = .{ .address = "127.0.0.1", .port = 42069 },
|
||
|
|
||
|
const confdir = "rotint";
|
||
|
const conffile = "config.json";
|
||
|
|
||
|
pub fn default() Config {
|
||
|
return .{};
|
||
|
}
|
||
|
|
||
|
pub fn load(allocator: std.mem.Allocator, env: *const std.process.EnvMap) !Config {
|
||
|
if (env.get("XDG_CONFIG_HOME")) |cfg|
|
||
|
if (cfg.len > 0) {
|
||
|
return try loadPath(allocator, try std.fs.path.join(
|
||
|
allocator,
|
||
|
&.{ cfg, confdir, conffile },
|
||
|
));
|
||
|
};
|
||
|
|
||
|
if (env.get("HOME")) |home|
|
||
|
if (home.len > 0) {
|
||
|
return try loadPath(allocator, try std.fs.path.join(
|
||
|
allocator,
|
||
|
&.{ home, ".config", confdir, conffile },
|
||
|
));
|
||
|
};
|
||
|
|
||
|
return error.ConfigHomeMissing;
|
||
|
}
|
||
|
|
||
|
fn loadPath(allocator: std.mem.Allocator, path: []const u8) !Config {
|
||
|
defer allocator.free(path);
|
||
|
const data = try std.fs.cwd().readFileAlloc(allocator, path, 1024);
|
||
|
defer allocator.free(data);
|
||
|
|
||
|
return try std.json.parseFromSliceLeaky(Config, allocator, data, .{});
|
||
|
}
|
||
|
|
||
|
const AddressPort = struct {
|
||
|
address: []const u8,
|
||
|
port: u16,
|
||
|
};
|