From 526c3f0ed139bbacaa415154a272d96279d26239 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 10 Mar 2026 19:38:06 +0200 Subject: task 80330fc4: deduplicate default config path helper --- internal/appconfig/config_defaults.go | 17 +++++++++++++++++ internal/appconfig/config_test.go | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 internal/appconfig/config_defaults.go (limited to 'internal') diff --git a/internal/appconfig/config_defaults.go b/internal/appconfig/config_defaults.go new file mode 100644 index 0000000..80ba697 --- /dev/null +++ b/internal/appconfig/config_defaults.go @@ -0,0 +1,17 @@ +package appconfig + +const configPathFallback = "$XDG_CONFIG_HOME/hexai/config.toml" + +// DefaultConfigPath returns the resolved config path or the documented XDG +// fallback when the real path cannot be determined. +func DefaultConfigPath() string { + return configPathOrFallback(ConfigPath) +} + +func configPathOrFallback(resolve func() (string, error)) string { + path, err := resolve() + if err != nil { + return configPathFallback + } + return path +} diff --git a/internal/appconfig/config_test.go b/internal/appconfig/config_test.go index cf9a725..c98d904 100644 --- a/internal/appconfig/config_test.go +++ b/internal/appconfig/config_test.go @@ -2,6 +2,7 @@ package appconfig import ( "bytes" + "errors" "io" "log" "os" @@ -314,6 +315,26 @@ func TestGetConfigPath_XDG(t *testing.T) { } } +func TestDefaultConfigPath_UsesResolvedPath(t *testing.T) { + dir := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", dir) + + got := DefaultConfigPath() + want := filepath.Join(dir, "hexai", "config.toml") + if got != want { + t.Fatalf("DefaultConfigPath() = %q, want %q", got, want) + } +} + +func TestDefaultConfigPath_FallsBackOnError(t *testing.T) { + got := configPathOrFallback(func() (string, error) { + return "", errors.New("boom") + }) + if got != configPathFallback { + t.Fatalf("configPathOrFallback() = %q, want %q", got, configPathFallback) + } +} + func TestStateDir_XDG(t *testing.T) { dir := t.TempDir() t.Setenv("XDG_STATE_HOME", dir) -- cgit v1.2.3