summaryrefslogtreecommitdiff
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
parent07115c22a74dd5311f70434029791d5346e627c4 (diff)
task 80330fc4: deduplicate default config path helper
-rw-r--r--cmd/hexai-lsp-server/main.go10
-rw-r--r--cmd/hexai-tmux-action/main.go10
-rw-r--r--cmd/hexai-tmux-edit/main.go10
-rw-r--r--cmd/hexai/main.go10
-rw-r--r--internal/appconfig/config_defaults.go17
-rw-r--r--internal/appconfig/config_test.go21
6 files changed, 42 insertions, 36 deletions
diff --git a/cmd/hexai-lsp-server/main.go b/cmd/hexai-lsp-server/main.go
index e3e458d..6577bc2 100644
--- a/cmd/hexai-lsp-server/main.go
+++ b/cmd/hexai-lsp-server/main.go
@@ -17,7 +17,7 @@ import (
func main() {
defaultLog := defaultLogPath()
logPath := flag.String("log", defaultLog, "path to log file (optional)")
- defaultCfg := defaultConfigPath()
+ defaultCfg := appconfig.DefaultConfigPath()
configPath := flag.String("config", "", fmt.Sprintf("path to config file (default: %s)", defaultCfg))
showVersion := flag.Bool("version", false, "print version and exit")
flag.Parse()
@@ -32,14 +32,6 @@ func main() {
}
}
-func defaultConfigPath() string {
- path, err := appconfig.ConfigPath()
- if err != nil {
- return "$XDG_CONFIG_HOME/hexai/config.toml"
- }
- return path
-}
-
// defaultLogPath returns the default LSP log file path in the state directory.
// Falls back to the system temp directory if the state directory is unavailable.
func defaultLogPath() string {
diff --git a/cmd/hexai-tmux-action/main.go b/cmd/hexai-tmux-action/main.go
index 32dd725..6249de3 100644
--- a/cmd/hexai-tmux-action/main.go
+++ b/cmd/hexai-tmux-action/main.go
@@ -15,7 +15,7 @@ func main() {
infile := flag.String("infile", "", "Read input from this file instead of stdin")
outfile := flag.String("outfile", "", "Write output to this file instead of stdout")
uiChild := flag.Bool("ui-child", false, "INTERNAL: run interactive UI and write to -outfile atomically")
- defaultPath := defaultConfigPath()
+ defaultPath := appconfig.DefaultConfigPath()
configPath := flag.String("config", "", fmt.Sprintf("path to config file (default: %s)", defaultPath))
tmuxTarget := flag.String("tmux-target", "", "tmux split target (advanced)")
tmuxSplit := flag.String("tmux-split", "v", "tmux split orientation: v or h")
@@ -35,11 +35,3 @@ func main() {
os.Exit(1)
}
}
-
-func defaultConfigPath() string {
- path, err := appconfig.ConfigPath()
- if err != nil {
- return "$XDG_CONFIG_HOME/hexai/config.toml"
- }
- return path
-}
diff --git a/cmd/hexai-tmux-edit/main.go b/cmd/hexai-tmux-edit/main.go
index 931d1c4..ea3330b 100644
--- a/cmd/hexai-tmux-edit/main.go
+++ b/cmd/hexai-tmux-edit/main.go
@@ -22,7 +22,7 @@ import (
)
func main() {
- defaultPath := defaultConfigPath()
+ defaultPath := appconfig.DefaultConfigPath()
configPath := flag.String("config", "", fmt.Sprintf("path to config file (default: %s)", defaultPath))
agent := flag.String("agent", "", "AI agent name (auto-detected if omitted)")
pane := flag.String("pane", "", "tmux target pane ID (e.g. %%5)")
@@ -38,11 +38,3 @@ func main() {
os.Exit(1)
}
}
-
-func defaultConfigPath() string {
- path, err := appconfig.ConfigPath()
- if err != nil {
- return "$XDG_CONFIG_HOME/hexai/config.toml"
- }
- return path
-}
diff --git a/cmd/hexai/main.go b/cmd/hexai/main.go
index 15fda42..de96bbf 100644
--- a/cmd/hexai/main.go
+++ b/cmd/hexai/main.go
@@ -25,7 +25,7 @@ func main() {
cliEntries = []appconfig.SurfaceConfig{{Provider: cfg.Provider}}
}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
- defaultPath := defaultConfigPath()
+ defaultPath := appconfig.DefaultConfigPath()
configFlag := fs.String("config", configPath, fmt.Sprintf("path to config file (default: %s)", defaultPath))
showVersion := fs.Bool("version", false, "print version and exit")
selectedFlags := make([]bool, len(cliEntries))
@@ -106,11 +106,3 @@ func pickDefaultModel(cfg appconfig.App, provider string) string {
return strings.TrimSpace(cfg.OpenAIModel)
}
}
-
-func defaultConfigPath() string {
- cfgPath, err := appconfig.ConfigPath()
- if err != nil {
- return "$XDG_CONFIG_HOME/hexai/config.toml"
- }
- return cfgPath
-}
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)