Demonstrate source-aware path normalization
This commit is contained in:
@@ -4,6 +4,7 @@ package consumers
|
||||
|
||||
import (
|
||||
"math"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -146,6 +147,69 @@ func NotariusWindow(configuration NotariusConfig, binding int) (int, bool) {
|
||||
return int(value), true
|
||||
}
|
||||
|
||||
// NotariusPathConfig demonstrates application-owned interpretation of a
|
||||
// path-valued configuration field. ConfigKit supplies provenance; this example
|
||||
// selects its own relative-path bases.
|
||||
type NotariusPathConfig struct {
|
||||
Reference string `yaml:"reference"`
|
||||
Sensitive string `yaml:"sensitive"`
|
||||
}
|
||||
|
||||
// LoadNotariusPaths loads the path-bearing Notarius-shaped example. InvocationDir
|
||||
// is the application-supplied base for relative CLI overrides; it is never read
|
||||
// from the process working directory.
|
||||
func LoadNotariusPaths(path, invocationDir string, overrides ...configkit.Override) (*configkit.Result[NotariusPathConfig], error) {
|
||||
return configkit.Load(configkit.File(path), configkit.Options[NotariusPathConfig]{
|
||||
Imports: true, Overrides: overrides,
|
||||
Definition: configkit.Definition[NotariusPathConfig]{
|
||||
Defaults: func() NotariusPathConfig { return NotariusPathConfig{Reference: "defaults/reference.txt"} },
|
||||
Resolve: func(configuration *NotariusPathConfig, resolution *configkit.Resolution) error {
|
||||
resolveNotariusReference(configuration, resolution, invocationDir)
|
||||
return nil
|
||||
},
|
||||
Secret: func(path configkit.Path) bool { return path.String() == "/sensitive" },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func resolveNotariusReference(configuration *NotariusPathConfig, resolution *configkit.Resolution, invocationDir string) {
|
||||
path := configkit.Keys("reference")
|
||||
if configuration.Reference == "" || filepath.IsAbs(configuration.Reference) {
|
||||
return
|
||||
}
|
||||
info, found := resolution.InputInfo(path)
|
||||
if !found {
|
||||
resolution.Warning(path, "relative reference has no entry provenance")
|
||||
return
|
||||
}
|
||||
|
||||
base := ""
|
||||
if info.Origin.Layer == configkit.LayerCLI {
|
||||
base = invocationDir
|
||||
if base == "" {
|
||||
resolution.Warning(path, "relative CLI reference has no invocation directory")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
sourceID := "source-1" // ConfigKit's primary-source identity convention.
|
||||
if info.Origin.Layer != configkit.LayerDefault {
|
||||
if len(info.Origin.Locations) != 1 {
|
||||
resolution.Warning(path, "relative reference has no unambiguous declaring source")
|
||||
return
|
||||
}
|
||||
sourceID = info.Origin.Locations[0].SourceID
|
||||
}
|
||||
source, exists := resolution.Source(sourceID)
|
||||
if !exists || source.Path == "" {
|
||||
resolution.Warning(path, "relative reference has no declaring file")
|
||||
return
|
||||
}
|
||||
base = filepath.Dir(source.Path)
|
||||
}
|
||||
configkit.Normalize(resolution, path, &configuration.Reference,
|
||||
filepath.Join(base, configuration.Reference), "resolved relative reference from entry source")
|
||||
}
|
||||
|
||||
// ModuleOptions is a WeatherReporter-shaped union of known module option fields.
|
||||
type ModuleOptions struct {
|
||||
WindowUnits *int `yaml:"window_units"`
|
||||
|
||||
Reference in New Issue
Block a user