103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
package consumers
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
// BackendConfiguration is an application-owned configuration.
|
|
type BackendConfiguration struct {
|
|
Storage struct {
|
|
Backend string `yaml:"backend"`
|
|
} `yaml:"storage"`
|
|
}
|
|
|
|
// ConfigurationInspector is the reporting interface shared by results and errors.
|
|
type ConfigurationInspector interface {
|
|
InspectSubtree(configkit.Path) (configkit.Inspection, bool)
|
|
}
|
|
|
|
// BackendReport retains reporting evidence and diagnostics from one load.
|
|
type BackendReport struct {
|
|
Inspection configkit.Inspection
|
|
Available bool
|
|
Diagnostics []configkit.Diagnostic
|
|
storage configkit.Inspection
|
|
hasStorage bool
|
|
}
|
|
|
|
// InspectBackend loads once and preserves the original error along with any
|
|
// available inspection. Imports are enabled for the application's source.
|
|
func InspectBackend(source configkit.Source) (BackendReport, error) {
|
|
result, err := configkit.Load(source, configkit.Options[BackendConfiguration]{
|
|
Imports: true,
|
|
Definition: configkit.Definition[BackendConfiguration]{Validate: func(value *BackendConfiguration, validation *configkit.Validation) {
|
|
if value.Storage.Backend != "local" && value.Storage.Backend != "memory" {
|
|
validation.Error(configkit.Keys("storage", "backend"), "unsupported storage backend")
|
|
}
|
|
}},
|
|
})
|
|
var provider ConfigurationInspector = result
|
|
var diagnostics []configkit.Diagnostic
|
|
if err != nil {
|
|
var failure *configkit.LoadError
|
|
if !errors.As(err, &failure) {
|
|
return BackendReport{}, err
|
|
}
|
|
provider, diagnostics = failure, failure.Diagnostics()
|
|
}
|
|
view, ok := provider.InspectSubtree(configkit.Path{})
|
|
storage, hasStorage := provider.InspectSubtree(configkit.Keys("storage"))
|
|
return BackendReport{Inspection: view, Available: ok, Diagnostics: diagnostics, storage: storage, hasStorage: hasStorage}, err
|
|
}
|
|
|
|
// WriteConfigurationReport uses the same show and explain operations for a
|
|
// successful result or a failed load. Paths in explanations remain native.
|
|
func WriteConfigurationReport(output io.Writer, provider ConfigurationInspector) error {
|
|
root, ok := provider.InspectSubtree(configkit.Path{})
|
|
if !ok {
|
|
_, err := fmt.Fprintln(output, "Configuration inspection unavailable")
|
|
return err
|
|
}
|
|
if _, err := fmt.Fprintf(output, "Configuration checkpoint: %s\n", root.Stage()); err != nil {
|
|
return err
|
|
}
|
|
focus, ok := provider.InspectSubtree(configkit.Keys("storage"))
|
|
if !ok {
|
|
return errors.New("storage inspection unavailable")
|
|
}
|
|
if err := focus.WriteEffective(output); err != nil {
|
|
return err
|
|
}
|
|
return focus.WriteExplanation(output, configkit.Keys("storage", "backend"))
|
|
}
|
|
|
|
// WriteBackendReport renders retained evidence and any original diagnostics.
|
|
func WriteBackendReport(output io.Writer, report BackendReport) error {
|
|
if err := configkit.WriteDiagnostics(output, report.Diagnostics); err != nil {
|
|
return err
|
|
}
|
|
return WriteConfigurationReport(output, savedInspection{report.Inspection, report.Available, report.storage, report.hasStorage})
|
|
}
|
|
|
|
type savedInspection struct {
|
|
view configkit.Inspection
|
|
available bool
|
|
storage configkit.Inspection
|
|
hasStorage bool
|
|
}
|
|
|
|
func (saved savedInspection) InspectSubtree(path configkit.Path) (configkit.Inspection, bool) {
|
|
switch path.String() {
|
|
case "":
|
|
return saved.view, saved.available
|
|
case "/storage":
|
|
return saved.storage, saved.hasStorage
|
|
default:
|
|
return configkit.Inspection{}, false
|
|
}
|
|
}
|