refactor: move config file loading into loadFile

This commit is contained in:
Remi Gelinas
2024-07-17 15:02:03 -04:00
parent 431c99313c
commit 3350e3c848
2 changed files with 25 additions and 27 deletions

View File

@@ -33,26 +33,19 @@ pub fn run(alloc: std.mem.Allocator) !u8 {
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
// If a config path is passed, validate it, otherwise validate usual config options
if (opts.@"config-file") |config_path| {
const cwd = std.fs.cwd();
if (cwd.openFile(config_path, .{})) |file| {
defer file.close();
var cfg = try Config.default(alloc); var cfg = try Config.default(alloc);
defer cfg.deinit(); defer cfg.deinit();
var buf_reader = std.io.bufferedReader(file.reader()); // If a config path is passed, validate it, otherwise validate default configs
var iter = cli.args.lineIterator(buf_reader.reader()); if (opts.@"config-file") |config_path| {
try cfg.loadIter(alloc, &iter); try cfg.loadFile(alloc, config_path);
try cfg.loadRecursiveFiles(alloc);
try cfg.finalize(); if (!cfg._errors.empty()) {
} else |err| { try stdout.print("Config is not valid path={s}", .{config_path});
try stdout.print("{any}", .{err}); return 1;
} }
} else { } else {
_ = try Config.load(alloc); try cfg.loadDefaultFiles(alloc);
} }
return 0; return 0;

View File

@@ -1686,22 +1686,27 @@ pub fn loadIter(
try cli.args.parse(Config, alloc, self, iter); 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 /// Load the configuration from the default configuration file. The default
/// configuration file is at `$XDG_CONFIG_HOME/ghostty/config`. /// configuration file is at `$XDG_CONFIG_HOME/ghostty/config`.
pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void { pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void {
const config_path = try internal_os.xdg.config(alloc, .{ .subdir = "ghostty/config" }); const config_path = try internal_os.xdg.config(alloc, .{ .subdir = "ghostty/config" });
defer alloc.free(config_path); defer alloc.free(config_path);
const cwd = std.fs.cwd(); self.loadFile(alloc, config_path) catch |err| switch (err) {
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) {
error.FileNotFound => std.log.info( error.FileNotFound => std.log.info(
"homedir config not found, not loading path={s}", "homedir config not found, not loading path={s}",
.{config_path}, .{config_path},
@@ -1711,7 +1716,7 @@ pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void {
"error reading config file, not loading err={} path={s}", "error reading config file, not loading err={} path={s}",
.{ err, config_path }, .{ err, config_path },
), ),
} };
} }
/// Load and parse the CLI args. /// Load and parse the CLI args.