From 3350e3c848d95310d4b76a5ea0f2c92245e578e8 Mon Sep 17 00:00:00 2001 From: Remi Gelinas Date: Wed, 17 Jul 2024 15:02:03 -0400 Subject: [PATCH] refactor: move config file loading into loadFile --- src/cli/validate_config.zig | 25 +++++++++---------------- src/config/Config.zig | 27 ++++++++++++++++----------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/cli/validate_config.zig b/src/cli/validate_config.zig index 7bd8e8032..7f50e612b 100644 --- a/src/cli/validate_config.zig +++ b/src/cli/validate_config.zig @@ -33,26 +33,19 @@ pub fn run(alloc: std.mem.Allocator) !u8 { const stdout = std.io.getStdOut().writer(); - // If a config path is passed, validate it, otherwise validate usual config options + var cfg = try Config.default(alloc); + defer cfg.deinit(); + + // If a config path is passed, validate it, otherwise validate default configs if (opts.@"config-file") |config_path| { - const cwd = std.fs.cwd(); + try cfg.loadFile(alloc, config_path); - if (cwd.openFile(config_path, .{})) |file| { - defer file.close(); - - var cfg = try Config.default(alloc); - defer cfg.deinit(); - - var buf_reader = std.io.bufferedReader(file.reader()); - var iter = cli.args.lineIterator(buf_reader.reader()); - try cfg.loadIter(alloc, &iter); - try cfg.loadRecursiveFiles(alloc); - try cfg.finalize(); - } else |err| { - try stdout.print("{any}", .{err}); + if (!cfg._errors.empty()) { + try stdout.print("Config is not valid path={s}", .{config_path}); + return 1; } } else { - _ = try Config.load(alloc); + try cfg.loadDefaultFiles(alloc); } return 0; diff --git a/src/config/Config.zig b/src/config/Config.zig index 6f780bb33..6f27597fc 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -1686,22 +1686,27 @@ pub fn loadIter( try cli.args.parse(Config, alloc, self, iter); } +pub fn loadFile(self: *Config, alloc: Allocator, path: []const u8) !void { + const cwd = std.fs.cwd(); + + var file = try cwd.openFile(path, .{}); + defer file.close(); + + std.log.info("reading configuration file path={s}", .{path}); + + var buf_reader = std.io.bufferedReader(file.reader()); + var iter = cli.args.lineIterator(buf_reader.reader()); + try self.loadIter(alloc, &iter); + try self.expandPaths(std.fs.path.dirname(path).?); +} + /// Load the configuration from the default configuration file. The default /// configuration file is at `$XDG_CONFIG_HOME/ghostty/config`. pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void { const config_path = try internal_os.xdg.config(alloc, .{ .subdir = "ghostty/config" }); defer alloc.free(config_path); - const cwd = std.fs.cwd(); - if (cwd.openFile(config_path, .{})) |file| { - defer file.close(); - std.log.info("reading configuration file path={s}", .{config_path}); - - var buf_reader = std.io.bufferedReader(file.reader()); - var iter = cli.args.lineIterator(buf_reader.reader()); - try self.loadIter(alloc, &iter); - try self.expandPaths(std.fs.path.dirname(config_path).?); - } else |err| switch (err) { + self.loadFile(alloc, config_path) catch |err| switch (err) { error.FileNotFound => std.log.info( "homedir config not found, not loading path={s}", .{config_path}, @@ -1711,7 +1716,7 @@ pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void { "error reading config file, not loading err={} path={s}", .{ err, config_path }, ), - } + }; } /// Load and parse the CLI args.