feat(cli): add initial validate-config action

This commit is contained in:
Remi Gelinas
2024-07-17 12:27:12 -04:00
parent 00b1851191
commit 431c99313c
2 changed files with 64 additions and 0 deletions

View File

@ -9,6 +9,7 @@ const list_keybinds = @import("list_keybinds.zig");
const list_themes = @import("list_themes.zig");
const list_colors = @import("list_colors.zig");
const show_config = @import("show_config.zig");
const validate_config = @import("validate_config.zig");
/// Special commands that can be invoked via CLI flags. These are all
/// invoked by using `+<action>` as a CLI flag. The only exception is
@ -35,6 +36,9 @@ pub const Action = enum {
/// Dump the config to stdout
@"show-config",
// Validate passed config file
@"validate-config",
pub const Error = error{
/// Multiple actions were detected. You can specify at most one
/// action on the CLI otherwise the behavior desired is ambiguous.
@ -124,6 +128,7 @@ pub const Action = enum {
.@"list-themes" => try list_themes.run(alloc),
.@"list-colors" => try list_colors.run(alloc),
.@"show-config" => try show_config.run(alloc),
.@"validate-config" => try validate_config.run(alloc),
};
}

View File

@ -0,0 +1,59 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const args = @import("args.zig");
const Action = @import("action.zig").Action;
const Config = @import("../config.zig").Config;
const cli = @import("../cli.zig");
pub const Options = struct {
/// The path of the config file to validate
@"config-file": ?[:0]const u8 = null,
pub fn deinit(self: Options) void {
_ = self;
}
/// Enables "-h" and "--help" to work.
pub fn help(self: Options) !void {
_ = self;
return Action.help_error;
}
};
/// The `validate-config` command is used to validate a Ghostty config
pub fn run(alloc: std.mem.Allocator) !u8 {
var opts: Options = .{};
defer opts.deinit();
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
}
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);
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});
}
} else {
_ = try Config.load(alloc);
}
return 0;
}