2026-10-11 01:06:21 +00:00
|
|
|
package ckconfig
|
2026-10-07 13:50:48 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-10-11 01:39:23 +00:00
|
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
2026-10-07 13:50:48 +00:00
|
|
|
"go.yaml.in/yaml/v4"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-11 01:39:23 +00:00
|
|
|
func runConfigValidation[I, S any](invocation commandkit.Invocation, plan *configOperationPlan[I, S], state S) commandkit.Outcome {
|
|
|
|
|
diagnostics := make([]commandkit.Diagnostic, 0)
|
2026-10-07 13:50:48 +00:00
|
|
|
writtenRecord := false
|
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-11 01:39:23 +00:00
|
|
|
return configOperationFailure(err, diagnostics, writtenRecord)
|
2026-10-07 16:47:28 +00:00
|
|
|
}
|
2026-10-11 01:39:23 +00:00
|
|
|
if selected.scope.scope.Acquire == nil {
|
|
|
|
|
return configOperationFailure(fmt.Errorf("configuration scope %q has no acquisition callback", selected.scope.scope.Name), diagnostics, writtenRecord)
|
|
|
|
|
}
|
|
|
|
|
acquired, acquireErr := selected.scope.scope.Acquire(invocation.Context, Request{
|
|
|
|
|
Operation: Validate,
|
|
|
|
|
Selection: plan.selection,
|
|
|
|
|
}, selected.input, state)
|
2026-10-07 13:50:48 +00:00
|
|
|
diagnostics = append(diagnostics, acquired.Diagnostics...)
|
|
|
|
|
if acquireErr != nil {
|
2026-10-11 01:39:23 +00:00
|
|
|
return configOperationFailure(acquireErr, diagnostics, writtenRecord)
|
2026-10-07 13:50:48 +00:00
|
|
|
}
|
2026-10-07 16:47:28 +00:00
|
|
|
if err := invocation.Context.Err(); err != nil {
|
2026-10-11 01:39:23 +00:00
|
|
|
return configOperationFailure(err, diagnostics, writtenRecord)
|
2026-10-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-11 01:39:23 +00:00
|
|
|
record, err := validationSuccessRecord(selected.scope.scope.Name, plan.format)
|
2026-10-07 13:50:48 +00:00
|
|
|
if err != nil {
|
2026-10-11 01:39:23 +00:00
|
|
|
return configOperationFailure(err, diagnostics, writtenRecord)
|
2026-10-07 13:50:48 +00:00
|
|
|
}
|
2026-10-11 01:39:23 +00:00
|
|
|
if plan.format == "yaml" && !writtenRecord {
|
|
|
|
|
if err := writeConfigReport(invocation.Streams.Out, []byte("scopes:\n")); err != nil {
|
|
|
|
|
return configDeliveryFailure(err, diagnostics, true)
|
2026-10-07 13:50:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-11 01:39:23 +00:00
|
|
|
if err := writeConfigReport(invocation.Streams.Out, record); err != nil {
|
|
|
|
|
return configDeliveryFailure(err, diagnostics, true)
|
2026-10-07 13:50:48 +00:00
|
|
|
}
|
|
|
|
|
writtenRecord = true
|
|
|
|
|
}
|
2026-10-11 01:39:23 +00:00
|
|
|
outcome := commandkit.Success()
|
2026-10-07 13:50:48 +00:00
|
|
|
outcome.Diagnostics = diagnostics
|
|
|
|
|
return outcome
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validationSuccessRecord(scope, format string) ([]byte, error) {
|
|
|
|
|
if format == "text" {
|
|
|
|
|
return []byte(scope + ": valid\n"), nil
|
|
|
|
|
}
|
|
|
|
|
encoded, err := yaml.Marshal(struct {
|
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
Valid bool `yaml:"valid"`
|
|
|
|
|
}{Name: scope, Valid: true})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
lines := strings.Split(strings.TrimSuffix(string(encoded), "\n"), "\n")
|
|
|
|
|
var record bytes.Buffer
|
|
|
|
|
for index, line := range lines {
|
|
|
|
|
if index == 0 {
|
|
|
|
|
record.WriteString(" - ")
|
|
|
|
|
} else {
|
|
|
|
|
record.WriteString(" ")
|
|
|
|
|
}
|
|
|
|
|
record.WriteString(line)
|
|
|
|
|
record.WriteByte('\n')
|
|
|
|
|
}
|
|
|
|
|
return record.Bytes(), nil
|
|
|
|
|
}
|