Add focused configuration reports
This commit is contained in:
@@ -2,6 +2,7 @@ package ckconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
@@ -69,13 +70,15 @@ func runConfigSources[I, S any](invocation commandkit.Invocation, plan *configOp
|
||||
Selection: selected.selection,
|
||||
}, selected.input, state)
|
||||
queueConfigAcquisition(reporter, acquired)
|
||||
if acquireErr != nil {
|
||||
return configFailure(fmt.Sprintf("Unable to list sources for configuration scope %q.", name), acquired.Explanation, acquireErr)
|
||||
}
|
||||
if err := invocation.Context.Err(); err != nil {
|
||||
if acquireErr == nil && invocation.Context.Err() != nil {
|
||||
err := invocation.Context.Err()
|
||||
return configFailure("Configuration source listing was canceled.", acquired.Explanation, err)
|
||||
}
|
||||
if acquired.Value.Sources == nil {
|
||||
if acquireErr != nil {
|
||||
explanation := sourceSelectionExplanation(acquired.Explanation, "Source inventory is unavailable.")
|
||||
return configFailure(fmt.Sprintf("Unable to list sources for configuration scope %q.", name), explanation, acquireErr)
|
||||
}
|
||||
cause := fmt.Errorf("configuration scope %q did not provide a source reader", name)
|
||||
return configFailure("Configuration scope did not provide source information.", acquired.Explanation, cause)
|
||||
}
|
||||
@@ -83,6 +86,9 @@ func runConfigSources[I, S any](invocation commandkit.Invocation, plan *configOp
|
||||
sources := acquired.Value.Sources.Sources()
|
||||
document := sourceReport{Scope: name, Sources: make([]sourceDTO, 0, len(sources))}
|
||||
for _, source := range sources {
|
||||
if plan.sourceID.Set && source.ID != plan.sourceID.Value {
|
||||
continue
|
||||
}
|
||||
placement := "mapping"
|
||||
if source.PlacementMode == configkit.PlacementModeSequenceItem {
|
||||
placement = "sequence_item"
|
||||
@@ -121,14 +127,36 @@ func runConfigSources[I, S any](invocation commandkit.Invocation, plan *configOp
|
||||
item.EmptyImportSelections = append(item.EmptyImportSelections, emptyImportSelectionDTO{Path: selection.Path.String(), Location: locationDTO{SourceID: selection.Location.SourceID, Line: selection.Location.Line, Column: selection.Location.Column}, Selector: string(selection.Selector)})
|
||||
}
|
||||
document.Sources = append(document.Sources, item)
|
||||
if plan.sourceID.Set {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err := invocation.Context.Err(); err != nil {
|
||||
if plan.sourceID.Set && len(document.Sources) == 0 {
|
||||
message := fmt.Sprintf("Source ID %q was not found in configuration scope %q.", plan.sourceID.Value, name)
|
||||
if acquireErr != nil {
|
||||
return configFailure(fmt.Sprintf("Unable to list sources for configuration scope %q.", name), sourceSelectionExplanation(acquired.Explanation, message), acquireErr)
|
||||
}
|
||||
return configFailure(message, acquired.Explanation, fmt.Errorf("source ID %q was not found", plan.sourceID.Value))
|
||||
}
|
||||
if acquireErr == nil && invocation.Context.Err() != nil {
|
||||
err := invocation.Context.Err()
|
||||
return configFailure("Configuration source listing was canceled.", acquired.Explanation, err)
|
||||
}
|
||||
_ = reporter.Report(commandkit.Report{Value: document, TextTemplate: sourceTextTemplate})
|
||||
if acquireErr != nil {
|
||||
return configFailure(fmt.Sprintf("Unable to list sources for configuration scope %q.", name), acquired.Explanation, acquireErr)
|
||||
}
|
||||
return commandkit.Success()
|
||||
}
|
||||
|
||||
func sourceSelectionExplanation(explanation commandkit.Explanation, message string) commandkit.Explanation {
|
||||
explanation.Details = append(append([]commandkit.Detail(nil), explanation.Details...), func(writer io.Writer) error {
|
||||
_, err := fmt.Fprintln(writer, message)
|
||||
return err
|
||||
})
|
||||
return explanation
|
||||
}
|
||||
|
||||
var sourceTextTemplate = template.Must(commandkit.ParseTextTemplate("sources", `Scope: {{printf "%q" .Scope}}
|
||||
{{if .Sources}}Sources:
|
||||
{{range .Sources}} - id: {{printf "%q" .ID}}
|
||||
|
||||
Reference in New Issue
Block a user