Delegate configuration discovery to ConfigKit
This commit is contained in:
@@ -297,6 +297,11 @@ Prompt size: mechanical fixture migration; avoid unrelated production refactorin
|
|||||||
|
|
||||||
## Stage 6 — Delegate CLI Discovery And Publish The Actual Selected File
|
## 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.
|
Goal: replace filesystem probing with the settled native source selection.
|
||||||
|
|
||||||
Inspect loadConfigKit, discoverConfigPath/requireConfigFile, command dispatch,
|
Inspect loadConfigKit, discoverConfigPath/requireConfigFile, command dispatch,
|
||||||
|
|||||||
@@ -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) {
|
func TestConfigLoadingFailuresReturnOneWithPathContext(t *testing.T) {
|
||||||
missing := filepath.Join(t.TempDir(), "missing.yml")
|
missing := filepath.Join(t.TempDir(), "missing.yml")
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
|
|||||||
@@ -5,21 +5,28 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
||||||
configkit "go.maximumdirect.net/configkit"
|
configkit "go.maximumdirect.net/configkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfigKit(configPath, overlay string, overrides []configkit.Override, opts Options) (*config.LoadedConfig, string, error) {
|
func loadConfigKit(configPath, overlay string, overrides []configkit.Override, opts Options) (*config.LoadedConfig, string, error) {
|
||||||
path, err := discoverConfigPath(configPath, opts)
|
var source configkit.Source
|
||||||
if err != nil {
|
if path := strings.TrimSpace(configPath); path != "" {
|
||||||
return nil, "", err
|
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()
|
workingDirectory, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", fmt.Errorf("determine invocation directory: %w", err)
|
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 {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import (
|
|||||||
configkit "go.maximumdirect.net/configkit"
|
configkit "go.maximumdirect.net/configkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultConfigPath = "/usr/local/etc/notarius/config.yml"
|
|
||||||
const usage = `Usage:
|
const usage = `Usage:
|
||||||
notarius help
|
notarius help
|
||||||
notarius --version
|
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) {
|
func parseOnly(raw string) ([]string, error) {
|
||||||
if strings.TrimSpace(raw) == "" {
|
if strings.TrimSpace(raw) == "" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user