Files

104 lines
3.9 KiB
Go
Raw Permalink Normal View History

package ckconfig
2026-10-07 13:55:43 +00:00
import (
"bytes"
"fmt"
2026-10-11 01:39:23 +00:00
"io"
2026-11-07 04:47:23 +00:00
"strings"
2026-10-07 13:55:43 +00:00
commandkit "go.maximumdirect.net/commandkit"
2026-10-11 01:39:23 +00:00
)
2026-10-07 13:55:43 +00:00
type effectiveDocument struct {
name string
2026-10-11 01:39:23 +00:00
view View
2026-10-07 13:55:43 +00:00
}
2026-10-11 01:39:23 +00:00
func runConfigShow[I, S any](invocation commandkit.Invocation, plan *configOperationPlan[I, S], state S) commandkit.Outcome {
documents := make([]effectiveDocument, 0, len(plan.scopes))
reporter := invocation.Reporter()
2026-10-11 01:39:23 +00:00
for _, selected := range plan.scopes {
if err := invocation.Context.Err(); err != nil {
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, err)
2026-10-11 01:39:23 +00:00
}
if selected.scope.scope.Acquire == nil {
return commandkit.Failure(fmt.Sprintf("Configuration scope %q has no acquisition callback.", selected.scope.scope.Name))
}
2026-11-07 04:39:34 +00:00
acquired, acquireErr := selected.scope.scope.Acquire(invocation.Context, Request{Operation: Show, Selection: selected.selection}, selected.input, state)
2026-10-07 13:55:43 +00:00
if acquireErr != nil {
return configAcquisitionFailure(reporter, "show", selected.scope.scope.Name, acquired, acquireErr)
2026-10-07 13:55:43 +00:00
}
queueConfigAcquisition(reporter, acquired)
if err := invocation.Context.Err(); err != nil {
return configFailure("Configuration show was canceled.", acquired.Explanation, err)
}
2026-11-07 04:47:23 +00:00
if plan.pathSet {
if acquired.Value.Explanation == nil {
return configFailure("Configuration scope did not provide explanation data.", acquired.Explanation, fmt.Errorf("configuration scope %q declared path show without an explanation reader", selected.scope.scope.Name))
}
explanation, found := acquired.Value.Explanation.Explain(plan.path)
if !found || !explanation.Effective.Present {
return configFailure("The requested effective configuration path is not available.", acquired.Explanation, fmt.Errorf("configuration path %q is not effective in scope %q", plan.path.String(), selected.scope.scope.Name))
}
if err := invocation.Context.Err(); err != nil {
return configFailure("Configuration show was canceled.", acquired.Explanation, err)
}
var value any
err := reporter.RenderOutput(func(io.Writer) error {
var err error
value, err = commandkit.DecodeYAMLReport(strings.NewReader(explanation.Effective.YAML))
return err
})
if err == nil {
if canceled := invocation.Context.Err(); canceled != nil {
return configFailure("Configuration show was canceled.", acquired.Explanation, canceled)
}
_ = reporter.Report(commandkit.Report{Value: value})
}
return commandkit.Success()
}
2026-10-07 13:55:43 +00:00
if acquired.Value.Effective == nil {
return commandkit.Failure(fmt.Sprintf("Configuration scope %q did not provide an effective configuration writer.", selected.scope.scope.Name))
2026-10-07 13:55:43 +00:00
}
2026-10-11 01:39:23 +00:00
documents = append(documents, effectiveDocument{name: selected.scope.scope.Name, view: acquired.Value})
2026-10-07 13:55:43 +00:00
}
if err := invocation.Context.Err(); err != nil {
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, err)
}
var value any
err := reporter.RenderOutput(func(io.Writer) error {
values := make(map[string]any, len(documents))
for _, document := range documents {
if err := invocation.Context.Err(); err != nil {
return err
}
var native bytes.Buffer
if err := document.view.Effective.WriteEffective(&native); err != nil {
return err
}
decoded, err := commandkit.DecodeYAMLReport(&native)
if err != nil {
return err
2026-10-07 13:55:43 +00:00
}
values[document.name] = decoded
2026-10-07 13:55:43 +00:00
}
if plan.scopeName == "all" {
value = values
2026-10-09 01:30:17 +00:00
} else {
value = values[documents[0].name]
2026-10-07 13:55:43 +00:00
}
return nil
})
if err != nil {
if canceled := invocation.Context.Err(); canceled != nil {
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, canceled)
2026-10-07 13:55:43 +00:00
}
return commandkit.Success()
2026-10-07 13:55:43 +00:00
}
if err := invocation.Context.Err(); err != nil {
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, err)
2026-10-07 13:55:43 +00:00
}
_ = reporter.Report(commandkit.Report{Value: value})
return commandkit.Success()
}