2026-10-11 21:51:54 +00:00
|
|
|
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 {
|
2026-10-26 03:20:17 +00:00
|
|
|
cause := errors.New("configuration comparison was not prepared")
|
|
|
|
|
return configFailure("Configuration comparison was not prepared.", commandkit.Explanation{}, cause)
|
2026-10-11 21:51:54 +00:00
|
|
|
}
|
|
|
|
|
selected := plan.comparison
|
|
|
|
|
if err := invocation.Context.Err(); err != nil {
|
2026-10-26 03:20:17 +00:00
|
|
|
return configFailure("Configuration comparison was canceled.", commandkit.Explanation{}, err)
|
2026-10-11 21:51:54 +00:00
|
|
|
}
|
|
|
|
|
compare := selected.scope.scope.Compare
|
|
|
|
|
if compare == nil {
|
2026-10-26 03:20:17 +00:00
|
|
|
cause := fmt.Errorf("configuration scope %q has no comparison callback", selected.scope.scope.Name)
|
|
|
|
|
return configFailure("Configuration scope does not support comparison.", commandkit.Explanation{}, cause)
|
2026-10-11 21:51:54 +00:00
|
|
|
}
|
|
|
|
|
acquired, err := compare(invocation.Context, selected.request, state)
|
2026-10-26 03:20:17 +00:00
|
|
|
reporter := invocation.Reporter()
|
|
|
|
|
queueConfigDiagnostics(reporter, acquired.Diagnostics)
|
2026-10-11 21:51:54 +00:00
|
|
|
if err != nil {
|
2026-10-26 03:20:17 +00:00
|
|
|
return configFailure(fmt.Sprintf("Unable to compare configuration scope %q.", selected.scope.scope.Name), acquired.Explanation, err)
|
2026-10-11 21:51:54 +00:00
|
|
|
}
|
|
|
|
|
if err := invocation.Context.Err(); err != nil {
|
2026-10-26 03:20:17 +00:00
|
|
|
return configFailure("Configuration comparison was canceled.", acquired.Explanation, err)
|
|
|
|
|
}
|
2026-10-26 14:32:03 +00:00
|
|
|
return renderPreparedConfigReport(invocation, acquired.Explanation, func() ([]byte, error) {
|
|
|
|
|
return renderDiffReport(acquired.Value)
|
2026-10-26 03:20:17 +00:00
|
|
|
})
|
2026-10-11 21:51:54 +00:00
|
|
|
}
|