config: dedicated load func so we can reload

This commit is contained in:
Mitchell Hashimoto
2023-03-12 22:03:20 -07:00
parent 11e4215f9f
commit 3ce7baf30e
2 changed files with 29 additions and 15 deletions

View File

@ -202,6 +202,34 @@ pub const Config = struct {
self.* = undefined; 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 { pub fn default(alloc_gpa: Allocator) Allocator.Error!Config {
// Build up our basic config // Build up our basic config
var result: Config = .{ var result: Config = .{

View File

@ -30,22 +30,8 @@ pub fn main() !void {
const alloc = state.alloc; const alloc = state.alloc;
// Try reading our config // Try reading our config
var config = try Config.default(alloc); var config = try Config.load(alloc);
defer config.deinit(); 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}); //std.log.debug("config={}", .{config});
// Create our app state // Create our app state