From 22f3fea98f24a1fc6402d4d1e399f2ec087d82c0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 11 Sep 2023 09:46:27 -0700 Subject: [PATCH] config: turn invalid config-file values into errors in the list --- src/config/Config.zig | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index c2d2f4f8d..5ca6b27cd 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -764,16 +764,25 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void { /// Load and parse the config files that were added in the "config-file" key. pub fn loadRecursiveFiles(self: *Config, alloc: Allocator) !void { - // TODO(mitchellh): we should parse the files form the homedir first // TODO(mitchellh): support nesting (config-file in a config file) // TODO(mitchellh): detect cycles when nesting if (self.@"config-file".list.items.len == 0) return; + const arena_alloc = self._arena.?.allocator(); const cwd = std.fs.cwd(); const len = self.@"config-file".list.items.len; for (self.@"config-file".list.items) |path| { - var file = try cwd.openFile(path, .{}); + var file = cwd.openFile(path, .{}) catch |err| { + try self._errors.add(arena_alloc, .{ + .message = try std.fmt.allocPrintZ( + arena_alloc, + "error opening config-file {s}: {}", + .{ path, err }, + ), + }); + continue; + }; defer file.close(); var buf_reader = std.io.bufferedReader(file.reader()); @@ -783,8 +792,15 @@ pub fn loadRecursiveFiles(self: *Config, alloc: Allocator) !void { // We don't currently support adding more config files to load // from within a loaded config file. This can be supported // later. - if (self.@"config-file".list.items.len > len) - return error.ConfigFileInConfigFile; + if (self.@"config-file".list.items.len > len) { + try self._errors.add(arena_alloc, .{ + .message = try std.fmt.allocPrintZ( + arena_alloc, + "config-file cannot be used in a config-file. Found in {s}", + .{path}, + ), + }); + } } }