103 lines
3.7 KiB
Go
103 lines
3.7 KiB
Go
package ckconfig
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
// ClassifyConfigurationError selects a CLI outcome from ConfigKit loading
|
|
// diagnostics. A nil error is success. A loading error is usage only when it
|
|
// contains at least one error diagnostic and all error diagnostics identify
|
|
// invalid configuration input; warnings are ignored. Unknown evidence and
|
|
// other errors are operational. Wrappers and joined errors are inspected, and
|
|
// any operational branch, cancellation, or deadline takes precedence.
|
|
// The caller remains responsible for constructing an outcome and its cause.
|
|
func ClassifyConfigurationError(err error) commandkit.OutcomeKind {
|
|
if err == nil {
|
|
return commandkit.OutcomeSuccess
|
|
}
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
return classifyConfigurationTree(err)
|
|
}
|
|
|
|
func classifyConfigurationTree(err error) commandkit.OutcomeKind {
|
|
if load, ok := err.(*configkit.LoadError); ok {
|
|
if load == nil {
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
return classifyConfigurationDiagnostics(load.Diagnostics())
|
|
}
|
|
switch wrapped := err.(type) {
|
|
case interface{ Unwrap() []error }:
|
|
found := false
|
|
for _, child := range wrapped.Unwrap() {
|
|
if child == nil {
|
|
continue
|
|
}
|
|
found = true
|
|
if classifyConfigurationTree(child) != commandkit.OutcomeUsage {
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
}
|
|
if found {
|
|
return commandkit.OutcomeUsage
|
|
}
|
|
case interface{ Unwrap() error }:
|
|
if child := wrapped.Unwrap(); child != nil {
|
|
return classifyConfigurationTree(child)
|
|
}
|
|
}
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
|
|
func classifyConfigurationDiagnostics(diagnostics []configkit.Diagnostic) commandkit.OutcomeKind {
|
|
found := false
|
|
for _, diagnostic := range diagnostics {
|
|
switch diagnostic.Severity {
|
|
case configkit.SeverityWarning:
|
|
continue
|
|
case configkit.SeverityError:
|
|
found = true
|
|
if !configurationInputCode(diagnostic.Code) {
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
default:
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
}
|
|
if found {
|
|
return commandkit.OutcomeUsage
|
|
}
|
|
return commandkit.OutcomeOperational
|
|
}
|
|
|
|
func configurationInputCode(code configkit.Code) bool {
|
|
switch code {
|
|
case configkit.CodeYAMLInvalid, configkit.CodeYAMLEmpty, configkit.CodeYAMLMultipleDocuments,
|
|
configkit.CodeYAMLRootType, configkit.CodeYAMLDuplicateKey, configkit.CodeYAMLTag,
|
|
configkit.CodeYAMLAnchor, configkit.CodeYAMLAlias, configkit.CodeYAMLMergeKey,
|
|
configkit.CodeYAMLScalar, configkit.CodeYAMLDepthLimit, configkit.CodeYAMLNodeLimit,
|
|
configkit.CodeDecodeType, configkit.CodeDecodeNull, configkit.CodeDecodeRange,
|
|
configkit.CodeDecodeDuration, configkit.CodeDecodeText, configkit.CodeDecodeUnknownField,
|
|
configkit.CodeDecodeArrayLength, configkit.CodeSourceLimitExceeded,
|
|
configkit.CodeImportDisabled, configkit.CodeImportCycle, configkit.CodeImportRepeated,
|
|
configkit.CodeImportVersion, configkit.CodeSequenceInvalid, configkit.CodeSequenceConflict,
|
|
configkit.CodeComposeInvalid, configkit.CodeComposeConflict, configkit.CodeUnderlayVersion,
|
|
configkit.CodeOverlayVersion, configkit.CodeOverlayRepeated, configkit.CodeOverlayDirective,
|
|
configkit.CodeVersionType, configkit.CodeVersionError, configkit.CodeResolveError,
|
|
configkit.CodeValidateError, configkit.CodeInheritanceInvalid, configkit.CodeInheritanceMissing,
|
|
configkit.CodeInheritanceCycle, configkit.CodeInheritanceVersion, configkit.CodeOverrideInvalidPath,
|
|
configkit.CodeOverrideDuplicate, configkit.CodeOverrideOverlap, configkit.CodeOverrideDirective,
|
|
configkit.CodeOverrideVersion, configkit.CodeOverrideLimitExceeded, configkit.CodeSecretRequired,
|
|
configkit.CodeSecretInvalidName:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|