2026-10-11 01:06:21 +00:00
|
|
|
package ckconfig
|
2026-10-07 13:55:43 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
2026-10-11 01:39:23 +00:00
|
|
|
"io"
|
2026-10-07 13:55:43 +00:00
|
|
|
|
2026-11-04 03:52:46 +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))
|
2026-10-26 03:16:02 +00:00
|
|
|
reporter := invocation.Reporter()
|
2026-10-11 01:39:23 +00:00
|
|
|
for _, selected := range plan.scopes {
|
2026-10-07 16:47:28 +00:00
|
|
|
if err := invocation.Context.Err(); err != nil {
|
2026-10-26 03:16:02 +00:00
|
|
|
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, err)
|
2026-10-11 01:39:23 +00:00
|
|
|
}
|
|
|
|
|
if selected.scope.scope.Acquire == nil {
|
2026-11-07 03:57:10 +00:00
|
|
|
return commandkit.Failure(fmt.Sprintf("Configuration scope %q has no acquisition callback.", selected.scope.scope.Name))
|
2026-10-07 16:47:28 +00:00
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
acquired, acquireErr := selected.scope.scope.Acquire(invocation.Context, Request{Operation: Show, Selection: plan.selection}, selected.input, state)
|
2026-10-07 13:55:43 +00:00
|
|
|
if acquireErr != nil {
|
2026-10-26 03:16:02 +00:00
|
|
|
return configAcquisitionFailure(reporter, "show", selected.scope.scope.Name, acquired, acquireErr)
|
2026-10-07 13:55:43 +00:00
|
|
|
}
|
2026-11-07 04:32:24 +00:00
|
|
|
queueConfigAcquisition(reporter, acquired)
|
2026-10-07 16:47:28 +00:00
|
|
|
if err := invocation.Context.Err(); err != nil {
|
2026-10-26 03:16:02 +00:00
|
|
|
return configFailure("Configuration show was canceled.", acquired.Explanation, err)
|
2026-10-07 16:47:28 +00:00
|
|
|
}
|
2026-10-07 13:55:43 +00:00
|
|
|
if acquired.Value.Effective == nil {
|
2026-11-07 03:57:10 +00:00
|
|
|
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
|
|
|
}
|
2026-10-07 16:47:28 +00:00
|
|
|
if err := invocation.Context.Err(); err != nil {
|
2026-10-26 03:16:02 +00:00
|
|
|
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, err)
|
2026-10-07 16:47:28 +00:00
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
var value any
|
|
|
|
|
err := reporter.RenderOutput(func(io.Writer) error {
|
|
|
|
|
values := make(map[string]any, len(documents))
|
|
|
|
|
for _, document := range documents {
|
2026-10-26 03:16:02 +00:00
|
|
|
if err := invocation.Context.Err(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
var native bytes.Buffer
|
|
|
|
|
if err := document.view.Effective.WriteEffective(&native); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
decoded, err := commandkit.DecodeYAMLReport(&native)
|
|
|
|
|
if err != nil {
|
2026-10-26 03:16:02 +00:00
|
|
|
return err
|
2026-10-07 13:55:43 +00:00
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
values[document.name] = decoded
|
2026-10-07 13:55:43 +00:00
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
if plan.scopeName == "all" {
|
|
|
|
|
value = values
|
2026-10-09 01:30:17 +00:00
|
|
|
} else {
|
2026-11-07 03:57:10 +00:00
|
|
|
value = values[documents[0].name]
|
2026-10-07 13:55:43 +00:00
|
|
|
}
|
2026-11-07 03:57:10 +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
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
return commandkit.Success()
|
2026-10-07 13:55:43 +00:00
|
|
|
}
|
2026-11-07 03:57:10 +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
|
|
|
}
|
2026-11-07 03:57:10 +00:00
|
|
|
_ = reporter.Report(commandkit.Report{Value: value})
|
|
|
|
|
return commandkit.Success()
|
2026-10-07 16:45:10 +00:00
|
|
|
}
|