2026-10-04 12:37:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
|
2026-11-17 22:03:08 +00:00
|
|
|
legacyconfigkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
|
|
|
"go.maximumdirect.net/configkit"
|
2026-10-04 12:37:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type frameworkConfigurationConfig struct {
|
|
|
|
|
mode, configPath, configOverlay, schemaDir string
|
2026-10-04 12:46:12 +00:00
|
|
|
path string
|
2026-10-04 12:37:02 +00:00
|
|
|
configExplicit bool
|
2026-10-04 12:46:12 +00:00
|
|
|
pathExplicit bool
|
2026-10-04 12:37:02 +00:00
|
|
|
overrides []configkit.Override
|
|
|
|
|
diagnostics []configkit.Diagnostic
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type frameworkConfigurationSource struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Label string `json:"label"`
|
2026-11-17 22:03:08 +00:00
|
|
|
Layer legacyconfigkit.Layer `json:"layer"`
|
|
|
|
|
Kind legacyconfigkit.SourceKind `json:"kind"`
|
2026-10-04 12:37:02 +00:00
|
|
|
UnderlayIndex int `json:"underlay_index"`
|
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
|
ImportedFrom *frameworkConfigurationLocation `json:"imported_from,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type frameworkConfigurationLocation struct {
|
|
|
|
|
SourceID string `json:"source_id"`
|
|
|
|
|
Line int `json:"line"`
|
|
|
|
|
Column int `json:"column"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func inspectFrameworkConfigurationCommand(args []string, stdout, stderr io.Writer) int {
|
|
|
|
|
cfg, err := parseFrameworkConfigurationArgs(args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeApplicationError(stderr, "inspect configuration parse", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
2026-11-17 22:03:08 +00:00
|
|
|
var explanationPath legacyconfigkit.Path
|
2026-10-04 12:46:12 +00:00
|
|
|
if cfg.mode == "explain" {
|
2026-11-17 22:03:08 +00:00
|
|
|
explanationPath, err = legacyconfigkit.ParsePath(cfg.path)
|
2026-10-04 12:46:12 +00:00
|
|
|
if err != nil {
|
|
|
|
|
writeApplicationError(stderr, "inspect configuration path", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-04 12:37:02 +00:00
|
|
|
settings, err := resolveAppSettingsForFrameworkConfiguration(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeApplicationError(stderr, "inspect configuration", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
if err := writeApplicationWarnings(stderr, cfg.diagnostics); err != nil {
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
engine, err := newEngine(settings)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeApplicationError(stderr, "engine", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
view, err := engine.InspectConfiguration()
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeApplicationError(stderr, "inspect configuration", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
2026-11-17 22:03:08 +00:00
|
|
|
if err := legacyconfigkit.WriteDiagnostics(stderr, view.Diagnostics()); err != nil {
|
2026-10-04 12:37:02 +00:00
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
if cfg.mode == "validate" {
|
|
|
|
|
return ExitOK
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 12:46:12 +00:00
|
|
|
if cfg.mode == "explain" {
|
|
|
|
|
if _, ok := view.Explain(explanationPath); !ok {
|
|
|
|
|
writeApplicationError(stderr, "inspect configuration path", fmt.Errorf("unknown path %q", cfg.path))
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
if err := view.WriteExplanation(stdout, explanationPath); err != nil {
|
|
|
|
|
fmt.Fprintf(stderr, "inspect configuration output error: %v\n", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
return ExitOK
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 12:37:02 +00:00
|
|
|
switch cfg.mode {
|
|
|
|
|
case "show":
|
|
|
|
|
err = view.WriteEffective(stdout)
|
|
|
|
|
case "sources":
|
|
|
|
|
sources := frameworkConfigurationSources(view.Sources())
|
|
|
|
|
err = json.NewEncoder(stdout).Encode(sources)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(stderr, "inspect configuration output error: %v\n", err)
|
|
|
|
|
return ExitRuntimeError
|
|
|
|
|
}
|
|
|
|
|
return ExitOK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseFrameworkConfigurationArgs(args []string) (*frameworkConfigurationConfig, error) {
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return nil, errors.New("configuration operation is required")
|
|
|
|
|
}
|
|
|
|
|
cfg := &frameworkConfigurationConfig{mode: args[0]}
|
|
|
|
|
switch cfg.mode {
|
2026-10-04 12:46:12 +00:00
|
|
|
case "validate", "show", "sources", "explain":
|
2026-10-04 12:37:02 +00:00
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("unknown configuration operation %q", cfg.mode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs := flag.NewFlagSet("inspect configuration "+cfg.mode, flag.ContinueOnError)
|
|
|
|
|
fs.SetOutput(io.Discard)
|
|
|
|
|
registerConfigSelectionFlags(fs, &cfg.configPath, &cfg.configOverlay)
|
|
|
|
|
var promptkitSources promptKitSourceFlagValue
|
|
|
|
|
fs.Var(&promptkitSources, "promptkit-source", "selected PromptKit source path (repeatable)")
|
|
|
|
|
fs.StringVar(&cfg.schemaDir, "schema-dir", "", "base directory for validation schemas")
|
2026-10-04 12:46:12 +00:00
|
|
|
fs.StringVar(&cfg.path, "path", "", "JSON Pointer to explain (required for explain)")
|
2026-10-04 12:37:02 +00:00
|
|
|
if err := fs.Parse(args[1:]); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if fs.NArg() != 0 {
|
|
|
|
|
return nil, fmt.Errorf("unexpected positional args: %v", fs.Args())
|
|
|
|
|
}
|
|
|
|
|
cfg.configExplicit = flagWasSet(fs, "config")
|
2026-10-04 12:46:12 +00:00
|
|
|
cfg.pathExplicit = flagWasSet(fs, "path")
|
|
|
|
|
if cfg.mode == "explain" && !cfg.pathExplicit {
|
|
|
|
|
return nil, errors.New("--path is required for explain")
|
|
|
|
|
}
|
2026-10-04 12:37:02 +00:00
|
|
|
cfg.overrides = applicationOverrides(fs)
|
|
|
|
|
return cfg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveAppSettingsForFrameworkConfiguration(cfg *frameworkConfigurationConfig) (engineSettings, error) {
|
|
|
|
|
settings, diagnostics, err := loadApplicationSettings(cfg.configPath, cfg.configOverlay, cfg.configExplicit, cfg.overrides)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return engineSettings{}, err
|
|
|
|
|
}
|
|
|
|
|
cfg.diagnostics = diagnostics
|
|
|
|
|
return engineSettings{
|
|
|
|
|
promptkitSources: settings.PromptKitSources,
|
|
|
|
|
schemaDir: settings.SchemaDir,
|
|
|
|
|
transientRetries: settings.TransientRetries,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 22:03:08 +00:00
|
|
|
func frameworkConfigurationSources(sources []legacyconfigkit.SourceInfo) []frameworkConfigurationSource {
|
2026-10-04 12:37:02 +00:00
|
|
|
result := make([]frameworkConfigurationSource, 0, len(sources))
|
|
|
|
|
for _, source := range sources {
|
|
|
|
|
item := frameworkConfigurationSource{
|
|
|
|
|
ID: source.ID, Label: source.Label, Layer: source.Layer,
|
|
|
|
|
Kind: source.Kind, UnderlayIndex: source.UnderlayIndex, Path: source.Path,
|
|
|
|
|
}
|
|
|
|
|
if source.ImportedFrom != nil {
|
|
|
|
|
item.ImportedFrom = &frameworkConfigurationLocation{
|
|
|
|
|
SourceID: source.ImportedFrom.SourceID,
|
|
|
|
|
Line: source.ImportedFrom.Line,
|
|
|
|
|
Column: source.ImportedFrom.Column,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result = append(result, item)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|