fix(configuration): resolve the default config path in one place
Four binaries derived the default with os.UserConfigDir while the daemon resolved it from its own config home. The two agree on Linux with an absolute XDG_CONFIG_HOME and diverge everywhere else, so one deployment booted from a different config file depending on which binary started. Move the rule into the package that owns configuration resolution (internal/infrastructure/configuration, which already owns xdgDataHome and DefaultWorkDir) as DefaultConfigDir/DefaultConfigPath, and have every -config default take it from there: archie-gateway, archie-messaging, archie-state-store (serve and recovery) and archie-ui, plus the daemon's own parseArgs, setup generator and drain-marker path. Fence the claim at the source, since the resolved value is only observable by starting a process: no source under cmd/ or internal/app/archied may call os.UserConfigDir, and every -config registration must default to the helper.
This commit is contained in:
1 parent
51feab85e5
commit
856e2eef71
12 files changed
+369
-75
No files matched your search
@@ -7,22 +7,17 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
func main() { os.Exit(run()) }
|
||||
|
||||
func run() int {
|
||||
base, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
var options archied.GatewayOptions
|
||||
flag.StringVar(&options.Config, "config", filepath.Join(base, "archie", "config.toml"), "configuration file or directory")
|
||||
flag.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory")
|
||||
flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
flag.StringVar(&options.Listen, "listen", "", "gateway gRPC listen address (defaults to [services.gateway].listen, else 127.0.0.1:8585)")
|
||||
flag.StringVar(&options.Token, "token", "", "Bearer [REDACTED] required for a non-loopback listener (defaults to [services.gateway].target_token)")
|
||||
|
||||
@@ -8,22 +8,17 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archiemessaging"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
func main() { os.Exit(run()) }
|
||||
|
||||
func run() int {
|
||||
base, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
var options archiemessaging.Options
|
||||
flag.StringVar(&options.Config, "config", filepath.Join(base, "archie", "config.toml"), "configuration file or directory")
|
||||
flag.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory")
|
||||
flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
flag.StringVar(&options.Gateway.Target, "gateway-target", "", "archie-gateway gRPC address (defaults to [services.gateway].target, else 127.0.0.1:8585)")
|
||||
flag.StringVar(&options.Gateway.Token, "gateway-token", "", "bearer token presented to archie-gateway")
|
||||
|
||||
@@ -14,11 +14,11 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
func main() { os.Exit(run()) }
|
||||
@@ -31,13 +31,8 @@ func run() int {
|
||||
if args := os.Args[1:]; len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
||||
return runRecovery(args, os.Stdout, os.Stderr)
|
||||
}
|
||||
base, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
var options archied.StateStoreOptions
|
||||
flag.StringVar(&options.Config, "config", filepath.Join(base, "archie", "config.toml"), "configuration file or directory")
|
||||
flag.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory")
|
||||
flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
flag.StringVar(&options.Listen, "listen", "", "state store gRPC listen address (defaults to [services.state].listen, else 127.0.0.1:9090)")
|
||||
flag.StringVar(&options.Token, "token", "", "bearer token required for a non-loopback listener (defaults to [services.state].target_token)")
|
||||
|
||||
@@ -8,15 +8,9 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
// defaultConfigPath is the configuration the serve path reads by default, and
|
||||
// the one the daemon boots with. It is the daemon's own rule rather than a
|
||||
// second derivation of it: os.UserConfigDir (what the sibling processes use)
|
||||
// disagrees with archied on darwin and rejects a relative XDG_CONFIG_HOME
|
||||
// outright, which would leave validate answering about a file nobody boots.
|
||||
func defaultConfigPath() string { return archied.DefaultConfigPath() }
|
||||
|
||||
// recoveryUsage is the whole flag surface of the offline recovery commands.
|
||||
// They are subcommands of this binary rather than a recovery binary of their
|
||||
// own: offline database maintenance belongs to the process that owns the file
|
||||
@@ -68,7 +62,7 @@ func runRecovery(args []string, stdout, stderr io.Writer) int {
|
||||
// Only validate asks a question about the process rather than the file, so
|
||||
// only validate takes the configuration the process would boot with.
|
||||
if command == archied.RecoveryValidate {
|
||||
flags.StringVar(&options.Config, "config", defaultConfigPath(), "configuration file or directory the daemon boots with")
|
||||
flags.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory the daemon boots with")
|
||||
flags.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
}
|
||||
if err := flags.Parse(args[1:]); err != nil {
|
||||
|
||||
@@ -388,24 +388,55 @@ func TestRecoveryValidateLeavesTheDaemonLogAlone(t *testing.T) {
|
||||
}
|
||||
|
||||
// validate's verdict is the daemon's verdict only if it reads the configuration
|
||||
// the daemon reads. archied resolves that default from its own configHome, which
|
||||
// honours a relative XDG_CONFIG_HOME; os.UserConfigDir rejects a relative one
|
||||
// outright, which left the command with no config and no verdict at all.
|
||||
func TestRecoveryDefaultConfigFollowsTheDaemonsConfigHome(t *testing.T) {
|
||||
relative := filepath.Join("relative", "config")
|
||||
t.Setenv("XDG_CONFIG_HOME", relative)
|
||||
if got, want := defaultConfigPath(), filepath.Join(relative, "archie", "config.toml"); got != want {
|
||||
t.Errorf("defaultConfigPath() with a relative XDG_CONFIG_HOME = %q, want %q", got, want)
|
||||
// the daemon reads, and with no -config it reads whatever the one shared
|
||||
// helper resolves. The resolution rule itself is pinned where it lives
|
||||
// (internal/infrastructure/configuration); what only this test can pin is that
|
||||
// the command takes its default from that helper rather than deriving it.
|
||||
//
|
||||
// A relative XDG_CONFIG_HOME is the case that separates the two: the command
|
||||
// has to resolve ${XDG_CONFIG_HOME}/archie/config.toml relative to its working
|
||||
// directory, and a second derivation through os.UserConfigDir rejects a
|
||||
// relative value outright, leaving the command with no config to answer about.
|
||||
func TestRecoveryDefaultConfigResolvesWhereTheDaemonReads(t *testing.T) {
|
||||
// The second layout is the teeth: the command has to read the config at the
|
||||
// resolved path, not merely find some config somewhere under the config home.
|
||||
// A resolution that drifts finds nothing and validate has no verdict.
|
||||
tests := []struct {
|
||||
name string
|
||||
configDir string // relative to $XDG_CONFIG_HOME
|
||||
wantOK bool
|
||||
}{
|
||||
{name: "$XDG_CONFIG_HOME/archie/config.toml", configDir: "archie", wantOK: true},
|
||||
{name: "a config elsewhere in the config home is not the default", configDir: "wrong-place"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
t.Chdir(root)
|
||||
t.Setenv("XDG_CONFIG_HOME", "xdg")
|
||||
|
||||
absolute := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", absolute)
|
||||
if got, want := defaultConfigPath(), filepath.Join(absolute, "archie", "config.toml"); got != want {
|
||||
t.Errorf("defaultConfigPath() = %q, want %q", got, want)
|
||||
}
|
||||
// The equality itself: one helper, so the two cannot drift apart again.
|
||||
if got, want := defaultConfigPath(), archied.DefaultConfigPath(); got != want {
|
||||
t.Errorf("defaultConfigPath() = %q, but the daemon resolves %q", got, want)
|
||||
configDir := filepath.Join(root, "xdg", tc.configDir)
|
||||
if err := os.MkdirAll(configDir, 0o700); err != nil {
|
||||
t.Fatalf("create %s: %v", configDir, err)
|
||||
}
|
||||
configPath := writeMinimalConfig(t, configDir)
|
||||
db := filepath.Join(root, "archie.db-tasks.sqlite")
|
||||
seedStoreResources(t, openTaskStore(t, db), configPath)
|
||||
|
||||
code, stdout, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
||||
if !tc.wantOK {
|
||||
if code == 0 {
|
||||
t.Fatalf("validate exited 0 with no config at the resolved path %s: %s", configPath, stdout)
|
||||
}
|
||||
return
|
||||
}
|
||||
if code != 0 {
|
||||
t.Fatalf("validate with no -config exited %d, so it did not read %s: %s", code, configPath, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "stored resources validate") {
|
||||
t.Errorf("validate reported %q, want the verdict on the store seeded from the resolved config", stdout)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,23 +13,18 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archieui"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
func main() { os.Exit(run()) }
|
||||
|
||||
func run() int {
|
||||
base, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
var options archieui.Options
|
||||
flag.StringVar(&options.Config, "config", filepath.Join(base, "archie", "config.toml"), "configuration file or directory (only [services.*] targets and [web] are read)")
|
||||
flag.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory (only [services.*] targets and [web] are read)")
|
||||
flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
||||
flag.StringVar(&options.Listen, "listen", "", "dashboard HTTP listen address (defaults to [web].listen, else 127.0.0.1:8484)")
|
||||
flag.StringVar(&options.Token, "token", "", "dashboard token; required for a non-loopback listener")
|
||||
|
||||
Reference in new issue
Block a user