summaryrefslogtreecommitdiff
path: root/internal/appconfig
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-10 19:38:06 +0200
committerPaul Buetow <paul@buetow.org>2026-03-10 19:38:06 +0200
commit526c3f0ed139bbacaa415154a272d96279d26239 (patch)
tree3754e0b969b47761583ec408e02ea45a7185ad71 /internal/appconfig
parent07115c22a74dd5311f70434029791d5346e627c4 (diff)
task 80330fc4: deduplicate default config path helper
Diffstat (limited to 'internal/appconfig')
-rw-r--r--internal/appconfig/config_defaults.go17
-rw-r--r--internal/appconfig/config_test.go21
2 files changed, 38 insertions, 0 deletions
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)