2026-09-13 16:50:40 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
2026-11-17 20:51:06 +00:00
|
|
|
"strings"
|
2026-09-13 16:50:40 +00:00
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
2026-11-17 20:42:25 +00:00
|
|
|
configkit "go.maximumdirect.net/configkit"
|
2026-09-13 16:50:40 +00:00
|
|
|
)
|
|
|
|
|
|
2026-09-25 21:46:35 +00:00
|
|
|
func loadConfigKit(configPath, overlay string, overrides []configkit.Override, opts Options) (*config.LoadedConfig, string, error) {
|
2026-11-17 20:51:06 +00:00
|
|
|
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})
|
2026-09-13 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
workingDirectory, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", fmt.Errorf("determine invocation directory: %w", err)
|
|
|
|
|
}
|
2026-11-17 20:51:06 +00:00
|
|
|
loaded, err := config.LoadSource(source, config.LoadOptions{Overlay: overlay, Overrides: overrides, InvocationDirectory: workingDirectory})
|
2026-09-13 16:50:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
2026-11-17 20:42:25 +00:00
|
|
|
return loaded, loaded.MainSourcePath(), nil
|
2026-09-13 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeConfigLoadError(output io.Writer, err error) {
|
|
|
|
|
var loadError *configkit.LoadError
|
|
|
|
|
if errors.As(err, &loadError) {
|
|
|
|
|
if writeErr := configkit.WriteDiagnostics(output, loadError.Diagnostics()); writeErr == nil {
|
2026-09-14 00:41:41 +00:00
|
|
|
// ConfigKit's locations contain source IDs. Keep its renderer and
|
2026-09-25 21:46:35 +00:00
|
|
|
// supply the acquired-source legend so imported/overlay errors can
|
2026-09-14 00:41:41 +00:00
|
|
|
// be traced to a file. Quoting also escapes terminal controls.
|
|
|
|
|
for _, source := range loadError.Sources() {
|
|
|
|
|
label := source.Path
|
|
|
|
|
if label == "" {
|
|
|
|
|
label = source.Label
|
|
|
|
|
}
|
|
|
|
|
if _, err := fmt.Fprintf(output, "source %q: %q\n", source.ID, label); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 16:50:40 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(output, "notarius: %v\n", err)
|
|
|
|
|
}
|