Introduce the version three configuration loader
This commit is contained in:
@@ -5,17 +5,14 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
ck "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
var systemConfigPaths = []string{"/usr/local/etc/audita/config.yml", "/etc/audita/config.yml"}
|
||||
|
||||
type configInput struct {
|
||||
ConfigPath string
|
||||
ConfigPathSet bool
|
||||
@@ -162,39 +159,33 @@ func parseConfigInput(command string, args []string, output io.Writer) (configIn
|
||||
return input, false, nil
|
||||
}
|
||||
|
||||
func selectConfigPath(explicit string, set bool, stat func(string) (os.FileInfo, error), candidates []string) (string, string, error) {
|
||||
if set {
|
||||
if strings.TrimSpace(explicit) == "" {
|
||||
return "", "", fmt.Errorf("--config requires a nonempty path")
|
||||
}
|
||||
if _, err := stat(explicit); err != nil {
|
||||
return "", "", fmt.Errorf("config file %q: %w", explicit, err)
|
||||
}
|
||||
return explicit, "flag", nil
|
||||
}
|
||||
for _, path := range candidates {
|
||||
if _, err := stat(path); err == nil {
|
||||
return path, "default", nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return "", "", fmt.Errorf("config file %q: %w", path, err)
|
||||
}
|
||||
}
|
||||
return "", "", fmt.Errorf("no configuration file found; select a config with backend and profile settings")
|
||||
}
|
||||
|
||||
func loadConfigInput(input configInput, command string) (selectedConfig, error) {
|
||||
if command == "validate" && (!input.ConfigPathSet || strings.TrimSpace(input.ConfigPath) == "") {
|
||||
return selectedConfig{}, fmt.Errorf("--config is required")
|
||||
var source ck.Source
|
||||
if input.ConfigPathSet {
|
||||
if strings.TrimSpace(input.ConfigPath) == "" {
|
||||
return selectedConfig{}, fmt.Errorf("--config requires a nonempty path")
|
||||
}
|
||||
source = ck.File(input.ConfigPath)
|
||||
} else {
|
||||
source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories()))
|
||||
}
|
||||
path, source, err := selectConfigPath(input.ConfigPath, input.ConfigPathSet, os.Stat, systemConfigPaths)
|
||||
loaded, err := config.Load(ck.Input{Source: source}, input.Overlay, input.Overrides...)
|
||||
if err != nil {
|
||||
return selectedConfig{}, err
|
||||
}
|
||||
loaded, err := config.Load(ck.File(path), input.Overlay, input.Overrides...)
|
||||
if err != nil {
|
||||
return selectedConfig{}, err
|
||||
selected := selectedConfig{Result: loaded}
|
||||
for _, info := range loaded.Sources() {
|
||||
if info.Layer == ck.LayerMain && info.ImportedFrom == nil {
|
||||
selected.Path = info.Path
|
||||
if input.ConfigPathSet {
|
||||
selected.Source = "flag"
|
||||
} else {
|
||||
selected.Source = "default"
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return selectedConfig{Path: path, Source: source, Result: loaded}, nil
|
||||
return selected, nil
|
||||
}
|
||||
|
||||
func configInputError(err error) string {
|
||||
|
||||
Reference in New Issue
Block a user