50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
)
|
|
|
|
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
|
|
}
|
|
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})
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return loaded, path, nil
|
|
}
|
|
|
|
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 {
|
|
// ConfigKit's locations contain source IDs. Keep its renderer and
|
|
// supply the acquired-source legend so imported/overlay errors can
|
|
// 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
|
|
}
|
|
}
|
|
return
|
|
}
|
|
}
|
|
fmt.Fprintf(output, "notarius: %v\n", err)
|
|
}
|