From 3ce7baf30e4540a8bfbc71b593638858ce9a797c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 12 Mar 2023 22:03:20 -0700 Subject: [PATCH] config: dedicated load func so we can reload --- src/config.zig | 28 ++++++++++++++++++++++++++++ src/main.zig | 16 +--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/config.zig b/src/config.zig index 06626b3e7..3789fe1a8 100644 --- a/src/config.zig +++ b/src/config.zig @@ -202,6 +202,34 @@ pub const Config = struct { self.* = undefined; } + /// Load the configuration according to the default rules: + /// + /// 1. Defaults + /// 2. XDG Config File + /// 3. CLI flags + /// 4. Recursively defined configuration files + /// + pub fn load(alloc_gpa: Allocator) !Config { + var result = try default(alloc_gpa); + errdefer result.deinit(); + + // If we have a configuration file in our home directory, parse that first. + try result.loadDefaultFiles(alloc_gpa); + + // Parse the config from the CLI args + { + var iter = try std.process.argsWithAllocator(alloc_gpa); + defer iter.deinit(); + try cli_args.parse(Config, alloc_gpa, &result, &iter); + } + + // Parse the config files that were added from our file and CLI args. + try result.loadRecursiveFiles(alloc_gpa); + try result.finalize(); + + return result; + } + pub fn default(alloc_gpa: Allocator) Allocator.Error!Config { // Build up our basic config var result: Config = .{ diff --git a/src/main.zig b/src/main.zig index abd9dfdef..899f0494a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,22 +30,8 @@ pub fn main() !void { const alloc = state.alloc; // Try reading our config - var config = try Config.default(alloc); + var config = try Config.load(alloc); defer config.deinit(); - - // If we have a configuration file in our home directory, parse that first. - try config.loadDefaultFiles(alloc); - - // Parse the config from the CLI args - { - var iter = try std.process.argsWithAllocator(alloc); - defer iter.deinit(); - try cli_args.parse(Config, alloc, &config, &iter); - } - - // Parse the config files that were added from our file and CLI args. - try config.loadRecursiveFiles(alloc); - try config.finalize(); //std.log.debug("config={}", .{config}); // Create our app state