Delegate configuration discovery to ConfigKit
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user