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.
151 lines
4.7 KiB
Go
151 lines
4.7 KiB
Go
package configuration
|
|
|
|
import (
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/secret"
|
|
)
|
|
|
|
// Defaults applied to absent input. Named so a reader can find the value
|
|
// without reading the code that applies it.
|
|
const (
|
|
defaultPollInterval = 60 * time.Second
|
|
defaultDiffCapLines = 400
|
|
defaultWebListen = "127.0.0.1:8484" // "off" disables
|
|
defaultMaxRetries = 3
|
|
defaultForgeType = "github"
|
|
defaultForgeHost = "https://github.com"
|
|
defaultAgentCommand = "archie-agent"
|
|
defaultDispatchTrigger = "assignee"
|
|
defaultAckReaction = "eyes"
|
|
defaultContainerUptime = 60 * time.Minute
|
|
defaultVolumeTTL = 72 * time.Hour
|
|
defaultPullPolicy = "missing"
|
|
)
|
|
|
|
// applyDefaults fills in every absent value. It never reports an error and
|
|
// never rejects input.
|
|
//
|
|
// Defaulting and validation were previously interleaved: functions named
|
|
// applyXDefaults also returned validation errors, and functions named
|
|
// validateX quietly mutated the config. That made it impossible to answer
|
|
// "what did the operator actually set?" -- the only way to see the supplied
|
|
// input was to not call them. Splitting them means defaults can be applied,
|
|
// inspected, and reasoned about independently of whether the result is valid.
|
|
func (l *Loader) applyDefaults(cfg *config.Config) {
|
|
l.applyGeneralDefaults(cfg)
|
|
applyForgeDefaults(cfg)
|
|
applyAgentDefaults(cfg)
|
|
applyDispatchDefaults(cfg)
|
|
applyIdentityDefaults(cfg)
|
|
applyContainerDefaults(cfg)
|
|
}
|
|
|
|
// applyGeneralDefaults derives the paths and limits that have no owning
|
|
// section.
|
|
func (l *Loader) applyGeneralDefaults(cfg *config.Config) {
|
|
if cfg.WorkDir == "" {
|
|
cfg.WorkDir = filepath.Join(l.dataHome(), "archie", "work")
|
|
}
|
|
if cfg.DBPath == "" {
|
|
cfg.DBPath = filepath.Join(l.dataHome(), "archie", "archie.db")
|
|
}
|
|
cfg.Indexing = cfg.Indexing.WithDefaults(cfg.WorkDir)
|
|
if cfg.PollInterval == 0 {
|
|
cfg.PollInterval = config.Duration(defaultPollInterval)
|
|
}
|
|
if cfg.DiffCapLines == 0 {
|
|
cfg.DiffCapLines = defaultDiffCapLines
|
|
}
|
|
if cfg.Web.Listen == "" {
|
|
cfg.Web.Listen = defaultWebListen
|
|
}
|
|
if cfg.MaxRetries == 0 {
|
|
cfg.MaxRetries = defaultMaxRetries
|
|
}
|
|
}
|
|
|
|
// applyForgeDefaults fills the forge section, promoting the legacy
|
|
// token_env field to a secret reference when no explicit one was given.
|
|
func applyForgeDefaults(cfg *config.Config) {
|
|
if cfg.Forge.Type == "" {
|
|
cfg.Forge.Type = defaultForgeType
|
|
}
|
|
if cfg.Forge.Host == "" {
|
|
cfg.Forge.Host = defaultForgeHost
|
|
}
|
|
if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" && cfg.Forge.TokenEnv != "" {
|
|
cfg.Forge.Token = secret.SecretRef{Engine: "env", Key: cfg.Forge.TokenEnv}
|
|
}
|
|
}
|
|
|
|
// applyAgentDefaults picks the execution mode. Containers require the NATS
|
|
// transport, so enabling them selects it unless the operator said otherwise.
|
|
func applyAgentDefaults(cfg *config.Config) {
|
|
if cfg.Agent.Mode == "" {
|
|
if cfg.Containers.Enabled {
|
|
cfg.Agent.Mode = agentModeNATS
|
|
} else {
|
|
cfg.Agent.Mode = agentModeInProcess
|
|
}
|
|
}
|
|
if cfg.Agent.Command == "" {
|
|
cfg.Agent.Command = defaultAgentCommand
|
|
}
|
|
}
|
|
|
|
// applyDispatchDefaults fills the dispatch section. "off" is the documented
|
|
// way to disable the acknowledgement reaction, and normalises to empty.
|
|
func applyDispatchDefaults(cfg *config.Config) {
|
|
if cfg.Dispatch.Trigger == "" {
|
|
cfg.Dispatch.Trigger = defaultDispatchTrigger
|
|
}
|
|
switch {
|
|
case cfg.Dispatch.AckReaction == "":
|
|
cfg.Dispatch.AckReaction = defaultAckReaction
|
|
case isOff(cfg.Dispatch.AckReaction):
|
|
cfg.Dispatch.AckReaction = ""
|
|
}
|
|
if cfg.Dispatch.Labels == nil {
|
|
cfg.Dispatch.Labels = map[string]string{}
|
|
}
|
|
for k, v := range config.DispatchLabelDefaults() {
|
|
if cfg.Dispatch.Labels[k] == "" {
|
|
cfg.Dispatch.Labels[k] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
// applyIdentityDefaults derives the commit email for a single-identity
|
|
// deployment. Multi-identity deployments configure each identity explicitly.
|
|
func applyIdentityDefaults(cfg *config.Config) {
|
|
if len(cfg.Identities) > 0 || cfg.BotUser == "" || cfg.BotEmail != "" {
|
|
return
|
|
}
|
|
switch cfg.Forge.Type {
|
|
case forgeTypeGitHub:
|
|
cfg.BotEmail = cfg.BotUser + "@users.noreply.github.com"
|
|
case forgeTypeGitea:
|
|
cfg.BotEmail = cfg.BotUser + "@gitea.local"
|
|
}
|
|
}
|
|
|
|
// applyContainerDefaults fills the container lifetimes. A negative VolumeTTL
|
|
// is left alone for validation to reject rather than silently corrected.
|
|
func applyContainerDefaults(cfg *config.Config) {
|
|
if !cfg.Containers.Enabled {
|
|
return
|
|
}
|
|
if cfg.Containers.MaxUptime == 0 {
|
|
cfg.Containers.MaxUptime = config.Duration(defaultContainerUptime)
|
|
}
|
|
if cfg.Containers.VolumeTTL == 0 {
|
|
cfg.Containers.VolumeTTL = config.Duration(defaultVolumeTTL)
|
|
}
|
|
if cfg.Containers.PullPolicy == "" {
|
|
cfg.Containers.PullPolicy = defaultPullPolicy
|
|
}
|
|
}
|