Files
commandkit/integrations/ckconfig/show.go

78 lines
2.6 KiB
Go
Raw 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-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))
}
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 {
return configAcquisitionFailure(reporter, "show", selected.scope.scope.Name, acquired, acquireErr)
2026-10-07 13:55:43 +00:00
}
queueConfigDiagnostics(reporter, acquired.Diagnostics)
if err := invocation.Context.Err(); err != nil {
return configFailure("Configuration show was canceled.", acquired.Explanation, err)
}
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()
}