config: make it possible not to leak
Using the GPA is a bit annoying sometimes. The other option would be to just use page allocator to allocate the config and bypass the GPA.
This commit is contained in:
40
src/main.zig
40
src/main.zig
@@ -53,37 +53,23 @@ pub fn main() !u8 {
|
||||
}
|
||||
|
||||
Config.loadDefault(allocator);
|
||||
defer Config.deinit();
|
||||
return writeDefaultConfig(if (args.len == 3) args[2] else null);
|
||||
} else if (std.mem.eql(u8, args[1], commands.run)) {
|
||||
if (args.len > 3) {
|
||||
printHelp(exename, .run);
|
||||
return 1;
|
||||
}
|
||||
blk: {
|
||||
const confpath = if (args.len == 3) args[2] else "yaes.json";
|
||||
const conf_file = std.fs.cwd().openFile(confpath, .{}) catch {
|
||||
log.warn("Could not load config file '{s}'. Using default config.", .{confpath});
|
||||
Config.loadDefault(allocator);
|
||||
break :blk;
|
||||
};
|
||||
defer conf_file.close();
|
||||
loadConfigOrDefault(allocator, if (args.len == 3) args[2] else null) catch
|
||||
return 1;
|
||||
|
||||
Config.load(allocator, conf_file.reader(), std.io.getStdErr().writer()) catch |err| {
|
||||
log.err("Could not parse config file '{s}': {s}.", .{ confpath, @errorName(err) });
|
||||
return 1;
|
||||
};
|
||||
}
|
||||
defer Config.destroy(allocator);
|
||||
|
||||
const ver = lj.getDriverVersion();
|
||||
std.debug.print("Driver version: {d}\n", .{ver});
|
||||
defer Config.deinit();
|
||||
|
||||
RotCtl.run(allocator) catch |err| {
|
||||
log.err("rotator controller ceased unexpectedly! {s}", .{@errorName(err)});
|
||||
return 1;
|
||||
};
|
||||
|
||||
return 0;
|
||||
} else if (std.mem.eql(u8, args[1], commands.help)) {
|
||||
if (args.len != 3) {
|
||||
printHelp(exename, .help);
|
||||
@@ -103,6 +89,24 @@ pub fn main() !u8 {
|
||||
printHelp(exename, .main);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn loadConfigOrDefault(allocator: std.mem.Allocator, path: ?[]const u8) !void {
|
||||
const confpath = path orelse "yaes.json";
|
||||
const conf_file = std.fs.cwd().openFile(confpath, .{}) catch {
|
||||
log.warn("Could not load config file '{s}'. Using default config.", .{confpath});
|
||||
Config.loadDefault(allocator);
|
||||
return;
|
||||
};
|
||||
defer conf_file.close();
|
||||
|
||||
Config.load(allocator, conf_file.reader(), std.io.getStdErr().writer()) catch |err| {
|
||||
log.err("Could not parse config file '{s}': {s}.", .{ confpath, @errorName(err) });
|
||||
return error.InvalidConfig;
|
||||
};
|
||||
log.info("Loaded config from '{s}'.", .{confpath});
|
||||
}
|
||||
|
||||
fn installUdevRules(outpath: ?[]const u8) u8 {
|
||||
|
Reference in New Issue
Block a user