From 349ace0528e659fbaa7eb96bd85f8225e69b5ac6 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Tue, 17 Nov 2026 20:51:06 +0000 Subject: [PATCH] Delegate configuration discovery to ConfigKit --- docs/roadmap/implementation.md | 5 ++++ internal/cli/command_contract_test.go | 21 ----------------- internal/cli/configkit_load.go | 15 ++++++++---- internal/cli/run.go | 34 --------------------------- 4 files changed, 16 insertions(+), 59 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index a389e29f..7b5266af 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -297,6 +297,11 @@ Prompt size: mechanical fixture migration; avoid unrelated production refactorin ## Stage 6 — Delegate CLI Discovery And Publish The Actual Selected File +**Complete.** The shared CLI loader selects one ConfigKit source and reports +the retained main path; manual probing was removed. Focused load, selection, +diagnostic, and report tests and a native temporary build pass. The expanded +discovery matrix is covered separately below. + Goal: replace filesystem probing with the settled native source selection. Inspect loadConfigKit, discoverConfigPath/requireConfigFile, command dispatch, diff --git a/internal/cli/command_contract_test.go b/internal/cli/command_contract_test.go index f3faa03a..279d13ca 100644 --- a/internal/cli/command_contract_test.go +++ b/internal/cli/command_contract_test.go @@ -104,27 +104,6 @@ func TestConfigDiscoveryPrefersExplicitPathThenEnvironment(t *testing.T) { } } -func TestConfigDiscoveryUsesCompiledDefaultOnlyWhenAvailable(t *testing.T) { - info, statErr := os.Stat(defaultConfigPath) - if statErr != nil && !os.IsNotExist(statErr) { - t.Fatalf("stat compiled default config: %v", statErr) - } - if statErr == nil && !info.Mode().IsRegular() { - t.Skipf("compiled default config has unexpected host state: %s", info.Mode()) - } - - path, err := discoverConfigPath("", commandContractOptions(t)) - if statErr == nil { - if err != nil || path != defaultConfigPath { - t.Fatalf("discoverConfigPath() = %q, %v; want compiled default", path, err) - } - return - } - if err == nil || !strings.Contains(err.Error(), "config file not found") { - t.Fatalf("discoverConfigPath() error = %v, want documented not-found context", err) - } -} - func TestConfigLoadingFailuresReturnOneWithPathContext(t *testing.T) { missing := filepath.Join(t.TempDir(), "missing.yml") var stdout, stderr bytes.Buffer diff --git a/internal/cli/configkit_load.go b/internal/cli/configkit_load.go index 6a910047..50aa10a1 100644 --- a/internal/cli/configkit_load.go +++ b/internal/cli/configkit_load.go @@ -5,21 +5,28 @@ import ( "fmt" "io" "os" + "strings" "gitea.maximumdirect.net/eric/notarius/internal/core/config" configkit "go.maximumdirect.net/configkit" ) func loadConfigKit(configPath, overlay string, overrides []configkit.Override, opts Options) (*config.LoadedConfig, string, error) { - path, err := discoverConfigPath(configPath, opts) - if err != nil { - return nil, "", err + var source configkit.Source + if path := strings.TrimSpace(configPath); path != "" { + source = configkit.File(path) + } else if path, ok := opts.LookupEnv("NOTARIUS_CONFIG"); ok && strings.TrimSpace(path) != "" { + source = configkit.File(strings.TrimSpace(path)) + } else { + source = configkit.SearchConfig("notarius", configkit.Searches( + configkit.WorkingDirectory(), configkit.UserConfigDirectory(), configkit.UnixSystemDirectories(), + ), configkit.SearchOptions{Required: true}) } workingDirectory, err := os.Getwd() if err != nil { return nil, "", fmt.Errorf("determine invocation directory: %w", err) } - loaded, err := config.Load(path, config.LoadOptions{Overlay: overlay, Overrides: overrides, InvocationDirectory: workingDirectory}) + loaded, err := config.LoadSource(source, config.LoadOptions{Overlay: overlay, Overrides: overrides, InvocationDirectory: workingDirectory}) if err != nil { return nil, "", err } diff --git a/internal/cli/run.go b/internal/cli/run.go index ffd1b448..cfb0b360 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -29,7 +29,6 @@ import ( configkit "go.maximumdirect.net/configkit" ) -const defaultConfigPath = "/usr/local/etc/notarius/config.yml" const usage = `Usage: notarius help notarius --version @@ -1085,39 +1084,6 @@ func runPipelines(args []string, stdout, stderr io.Writer, opts Options) int { } } -func discoverConfigPath(configPath string, opts Options) (string, error) { - if path := strings.TrimSpace(configPath); path != "" { - if err := requireConfigFile(path); err != nil { - return "", err - } - return path, nil - } - if path, ok := opts.LookupEnv("NOTARIUS_CONFIG"); ok && strings.TrimSpace(path) != "" { - path = strings.TrimSpace(path) - if err := requireConfigFile(path); err != nil { - return "", err - } - return path, nil - } - if _, err := os.Stat(defaultConfigPath); err == nil { - return defaultConfigPath, nil - } else if err != nil && !os.IsNotExist(err) { - return "", fmt.Errorf("check default config %q: %w", defaultConfigPath, err) - } - return "", fmt.Errorf("config file not found; pass --config or set NOTARIUS_CONFIG") -} - -func requireConfigFile(path string) error { - info, err := os.Stat(path) - if err != nil { - return fmt.Errorf("config file %q is not available: %w", path, err) - } - if info.IsDir() { - return fmt.Errorf("config file %q is a directory", path) - } - return nil -} - func parseOnly(raw string) ([]string, error) { if strings.TrimSpace(raw) == "" { return nil, nil