From 8873187921c55d94be56364bf0b9d6b2d12127c2 Mon Sep 17 00:00:00 2001 From: Claudomator Agent Date: Tue, 10 Mar 2026 04:36:19 +0000 Subject: cli: implement --config flag to load TOML config file The --config flag was registered but silently ignored. Now: - config.LoadFile loads a TOML file on top of defaults - PersistentPreRunE applies the file when --config is set - Explicit CLI flags (--data-dir, --claude-bin) take precedence over the file Tests: TestLoadFile_OverridesDefaults, TestLoadFile_MissingFile_ReturnsError, TestRootCmd_ConfigFile_Loaded, TestRootCmd_ConfigFile_CLIFlagOverrides, TestRootCmd_ConfigFile_Missing_ReturnsError Co-Authored-By: Claude Sonnet 4.6 --- internal/config/config.go | 15 +++++++++++++++ internal/config/config_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) (limited to 'internal/config') diff --git a/internal/config/config.go b/internal/config/config.go index daf42fe..8c5aebf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "path/filepath" + + "github.com/BurntSushi/toml" ) type Config struct { @@ -42,6 +44,19 @@ func Default() (*Config, error) { }, nil } +// LoadFile loads a TOML config file on top of the defaults. +// Fields not present in the file retain their default values. +func LoadFile(path string) (*Config, error) { + cfg, err := Default() + if err != nil { + return nil, err + } + if _, err := toml.DecodeFile(path, cfg); err != nil { + return nil, fmt.Errorf("loading config file %q: %w", path, err) + } + return cfg, nil +} + // EnsureDirs creates the data directory structure. func (c *Config) EnsureDirs() error { for _, dir := range []string{c.DataDir, c.LogDir} { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 766b856..2bba2c4 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,6 +1,8 @@ package config import ( + "os" + "path/filepath" "testing" ) @@ -22,3 +24,32 @@ func TestDefault_ValidHome_ReturnsConfig(t *testing.T) { t.Errorf("DataDir = %q, want /tmp/testhome/.claudomator", cfg.DataDir) } } + +func TestLoadFile_OverridesDefaults(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.toml") + if err := os.WriteFile(path, []byte("server_addr = \":9191\"\n"), 0600); err != nil { + t.Fatal(err) + } + t.Setenv("HOME", dir) + + cfg, err := LoadFile(path) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if cfg.ServerAddr != ":9191" { + t.Errorf("ServerAddr = %q, want :9191", cfg.ServerAddr) + } + // Unset fields retain defaults. + if cfg.MaxConcurrent != 3 { + t.Errorf("MaxConcurrent = %d, want 3", cfg.MaxConcurrent) + } +} + +func TestLoadFile_MissingFile_ReturnsError(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + _, err := LoadFile("/nonexistent/config.toml") + if err == nil { + t.Fatal("expected error for missing file, got nil") + } +} -- cgit v1.2.3