44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package ckconfig
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
|
)
|
|
|
|
func runConfigDiff[I, S any](invocation commandkit.Invocation, plan *configOperationPlan[I, S], state S) commandkit.Outcome {
|
|
if plan.comparison == nil || plan.comparison.scope == nil {
|
|
return configOperationFailure(errors.New("configuration comparison was not prepared"), nil, false)
|
|
}
|
|
selected := plan.comparison
|
|
if err := invocation.Context.Err(); err != nil {
|
|
return configOperationFailure(err, nil, false)
|
|
}
|
|
compare := selected.scope.scope.Compare
|
|
if compare == nil {
|
|
return configOperationFailure(fmt.Errorf("configuration scope %q has no comparison callback", selected.scope.scope.Name), nil, false)
|
|
}
|
|
acquired, err := compare(invocation.Context, selected.request, state)
|
|
diagnostics := append([]commandkit.Diagnostic(nil), acquired.Diagnostics...)
|
|
if err != nil {
|
|
return configOperationFailure(err, diagnostics, false)
|
|
}
|
|
if err := invocation.Context.Err(); err != nil {
|
|
return configOperationFailure(err, diagnostics, false)
|
|
}
|
|
report, err := renderDiffReport(acquired.Value)
|
|
if err != nil {
|
|
return configOperationFailure(err, diagnostics, false)
|
|
}
|
|
if err := invocation.Context.Err(); err != nil {
|
|
return configOperationFailure(err, diagnostics, false)
|
|
}
|
|
if err := writeConfigReport(invocation.Streams.Out, report); err != nil {
|
|
return configDeliveryFailure(err, diagnostics, true)
|
|
}
|
|
outcome := commandkit.Success()
|
|
outcome.Diagnostics = diagnostics
|
|
return outcome
|
|
}
|