internal/config both defined the types every package imports and performed
the file I/O to produce them. Importing a settings struct therefore pulled
in a TOML decoder and a YAML decoder, and the shared model owned discovery,
decoding, overlays, defaulting and validation all at once. See
docs/architecture/configuration.md, which requires loading to move out and
internal/config to be dissolved.
internal/config is now types only -- no os, no filepath, no toml. The new
package owns loading, one concern per file:
features.go the closed set of recognised feature files
discovery.go directory scan and classification
decode.go TOML/YAML decoding, format dispatch
defaults.go filling absent values, no errors
validate.go judging supplied values, no mutation
provenance.go which file contributed what
loader.go Loader wiring the above; File/Overlay/Dir
Beyond relocating, this fixes what the old code conflated:
- Defaulting and validation were interleaved. applyAgentDefaults and
applyDispatchDefaults returned validation errors; validateContainers and
validateSingleIdentity quietly mutated the config. There was no point at
which you could see what the operator actually supplied. They are now
separate passes: applyDefaults never fails, validate never writes.
- Errors were fmt.Errorf strings prefixed "config: ", so a caller could not
distinguish operator-fixable input from an unreadable file without
matching message text. Five sentinels now carry that distinction.
- dataHome() read XDG_DATA_HOME directly and discarded the error from
os.UserHomeDir, so defaulting could not be tested without the
environment. It is injectable on Loader and no longer swallows the error.
- Nothing recorded where a value came from. With overlays, feature files
and conf.d/ all able to set the same field, "which file set this?" was
answerable only by re-deriving precedence by hand. Provenance records it.
- Feature files were applied in map iteration order, making load order and
any resulting error arbitrary. Discovery now returns them sorted.
- loadMainConfig reported "no config.yaml or config.toml found in %s" with
an empty string: the directory was lost when it was extracted from
loadDirInto, so the error named no directory.
- Unreadable or malformed conf.d/ extras were skipped silently, which
presents as a setting the operator wrote simply not taking effect.
- Decode errors from the main config were returned bare, without naming
the file that failed to parse.
The 40 loader tests moved with the code and their assertions are unchanged,
so the defaulting and validation rewrite is behaviour-preserving.
Document.Config is the temporary seam: still internal/config.Config, named
as a field so the compromise is visible at each use site, with its deletion
condition recorded in doc.go. Building typed per-domain sections now, while
every consumer still reads config.Config, would mean two shapes kept in sync
by hand with no consumer for the new one; that belongs with the dissolution
work, not ahead of it.
No consumer outside cmd/archied changed.
Verified: go build, go vet, gofmt clean; go test ./internal/config/... and
./internal/infrastructure/configuration/... pass. Pre-existing gateway and
telegram failures are unchanged.
66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package configuration
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// Feature names a recognised config.<feature>.yaml section.
|
|
type Feature string
|
|
|
|
const (
|
|
FeatureGateway Feature = "gateway"
|
|
FeatureTools Feature = "tools"
|
|
FeatureMemory Feature = "memory"
|
|
FeatureModels Feature = "models"
|
|
FeatureIdentities Feature = "identities"
|
|
)
|
|
|
|
// knownFeatures is the closed set of recognised feature files. A
|
|
// config.<name>.yaml outside this set is an error rather than a silent
|
|
// no-op, so a misspelled filename does not look like a disabled feature.
|
|
var knownFeatures = map[Feature]bool{
|
|
FeatureGateway: true,
|
|
FeatureTools: true,
|
|
FeatureMemory: true,
|
|
FeatureModels: true,
|
|
FeatureIdentities: true,
|
|
}
|
|
|
|
// known reports whether f is a recognised feature.
|
|
func (f Feature) known() bool { return knownFeatures[f] }
|
|
|
|
// featureList renders the recognised names for error messages. Derived from
|
|
// knownFeatures so the message cannot drift from the set it describes -- it
|
|
// was previously spelled out as a literal beside the map.
|
|
func featureList() string {
|
|
names := make([]string, 0, len(knownFeatures))
|
|
for name := range knownFeatures {
|
|
names = append(names, string(name))
|
|
}
|
|
sort.Strings(names)
|
|
return strings.Join(names, ", ")
|
|
}
|
|
|
|
// featureFromFilename extracts the feature from a "config.<feature>.yaml"
|
|
// filename, or "" if the name does not have that shape.
|
|
func featureFromFilename(name string) Feature {
|
|
if !strings.HasPrefix(name, "config.") || !isYAML(name) {
|
|
return ""
|
|
}
|
|
return Feature(trimYAMLSuffix(strings.TrimPrefix(name, "config.")))
|
|
}
|
|
|
|
// isYAML reports whether name has a YAML extension.
|
|
func isYAML(name string) bool {
|
|
return strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml")
|
|
}
|
|
|
|
// isTOML reports whether name has a TOML extension.
|
|
func isTOML(name string) bool { return strings.HasSuffix(name, ".toml") }
|
|
|
|
// trimYAMLSuffix removes a .yaml or .yml extension.
|
|
func trimYAMLSuffix(name string) string {
|
|
return strings.TrimSuffix(strings.TrimSuffix(name, ".yaml"), ".yml")
|
|
}
|