Share diagnostic adaptation across integrations
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
@@ -511,61 +510,10 @@ func appendPromptKitAcquisition(left, right promptKitAcquisition) promptKitAcqui
|
||||
}
|
||||
|
||||
func promptKitDiagnostics(scope, side string, sources []configkit.SourceInfo, records []configkit.Diagnostic) promptKitAcquisition {
|
||||
if len(records) == 0 {
|
||||
return promptKitAcquisition{}
|
||||
}
|
||||
// Snapshot provider-owned slices before deferring their report to invocation.
|
||||
sources = append([]configkit.SourceInfo(nil), sources...)
|
||||
records = append([]configkit.Diagnostic(nil), records...)
|
||||
for index := range records {
|
||||
if records[index].Primary != nil {
|
||||
primary := *records[index].Primary
|
||||
records[index].Primary = &primary
|
||||
}
|
||||
records[index].Related = append([]configkit.Location(nil), records[index].Related...)
|
||||
}
|
||||
var warnings, failures []configkit.Diagnostic
|
||||
for _, record := range records {
|
||||
if record.Severity == configkit.SeverityError {
|
||||
failures = append(failures, record)
|
||||
} else {
|
||||
warnings = append(warnings, record)
|
||||
}
|
||||
}
|
||||
messages := promptKitAcquisition{}
|
||||
if len(warnings) > 0 {
|
||||
messages.diagnostics = []commandkit.Diagnostic{{
|
||||
Severity: commandkit.SeverityWarning,
|
||||
Write: func(writer io.Writer) error {
|
||||
return writePromptKitDiagnostics(writer, scope, side, sources, warnings)
|
||||
},
|
||||
}}
|
||||
}
|
||||
if len(failures) > 0 {
|
||||
messages.explanation = commandkit.Explanation{Details: []commandkit.Detail{func(writer io.Writer) error {
|
||||
return writePromptKitDiagnostics(writer, scope, side, sources, failures)
|
||||
}}}
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
func writePromptKitDiagnostics(writer io.Writer, scope, side string, sources []configkit.SourceInfo, records []configkit.Diagnostic) error {
|
||||
heading := "Configuration scope " + strconv.Quote(scope)
|
||||
if side != "" {
|
||||
heading += " (" + side + ")"
|
||||
}
|
||||
if _, err := fmt.Fprintf(writer, "%s:\n", heading); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(sources) > 0 {
|
||||
if _, err := io.WriteString(writer, "Source IDs:\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, source := range sources {
|
||||
if _, err := fmt.Fprintf(writer, " %s: label=%s, path=%s\n", strconv.Quote(source.ID), strconv.Quote(source.Label), strconv.Quote(source.Path)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return configkit.WriteDiagnostics(writer, records)
|
||||
diagnostics, explanation := ckconfig.AdaptDiagnostics(heading+":\n", sources, records)
|
||||
return promptKitAcquisition{diagnostics: diagnostics, explanation: explanation}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user