fix(launcher): resolve gateway startup issue in tui mode
The picoclaw-launcher-tui was not respecting PICOCLAW_HOME and PICOCLAW_CONFIG environment variables when determining the config file path. This caused a configuration mismatch between the TUI and the gateway command when these environment variables were set. Changes: - Updated ConfigPath() to check PICOCLAW_CONFIG environment variable first - Updated ConfigDir() to check PICOCLAW_HOME environment variable first - Added comprehensive unit tests for both functions - Added documentation comments explaining the priority order This ensures that launcher-tui and gateway command use the same config file path resolution logic, fixing the issue where gateway could not start from TUI. Fixes #1704
This commit is contained in:
parent
f12c09b767
commit
1a0e65b495
2 changed files with 144 additions and 0 deletions
|
|
@ -13,7 +13,12 @@ const (
|
|||
configFileName = "config.json"
|
||||
)
|
||||
|
||||
// ConfigPath returns the path to the config file.
|
||||
// Priority: $PICOCLAW_CONFIG > $PICOCLAW_HOME/config.json > ~/.picoclaw/config.json
|
||||
func ConfigPath() (string, error) {
|
||||
if configPath := os.Getenv("PICOCLAW_CONFIG"); configPath != "" {
|
||||
return configPath, nil
|
||||
}
|
||||
dir, err := ConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -21,7 +26,12 @@ func ConfigPath() (string, error) {
|
|||
return filepath.Join(dir, configFileName), nil
|
||||
}
|
||||
|
||||
// ConfigDir returns the directory for picoclaw configuration.
|
||||
// Priority: $PICOCLAW_HOME > ~/.picoclaw
|
||||
func ConfigDir() (string, error) {
|
||||
if home := os.Getenv("PICOCLAW_HOME"); home != "" {
|
||||
return home, nil
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
|
|||
134
cmd/picoclaw-launcher-tui/internal/config/store_test.go
Normal file
134
cmd/picoclaw-launcher-tui/internal/config/store_test.go
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
package configstore
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigPath_Default(t *testing.T) {
|
||||
// Clear environment variables
|
||||
os.Unsetenv("PICOCLAW_CONFIG")
|
||||
os.Unsetenv("PICOCLAW_HOME")
|
||||
|
||||
got, err := ConfigPath()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigPath() error = %v", err)
|
||||
}
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
want := filepath.Join(home, ".picoclaw", "config.json")
|
||||
if got != want {
|
||||
t.Errorf("ConfigPath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigPath_WithPICOCLAW_CONFIG(t *testing.T) {
|
||||
// Set PICOCLAW_CONFIG
|
||||
customPath := "/custom/path/config.json"
|
||||
os.Setenv("PICOCLAW_CONFIG", customPath)
|
||||
defer os.Unsetenv("PICOCLAW_CONFIG")
|
||||
|
||||
os.Unsetenv("PICOCLAW_HOME")
|
||||
|
||||
got, err := ConfigPath()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigPath() error = %v", err)
|
||||
}
|
||||
|
||||
if got != customPath {
|
||||
t.Errorf("ConfigPath() = %q, want %q", got, customPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigPath_WithPICOCLAW_HOME(t *testing.T) {
|
||||
// Clear PICOCLAW_CONFIG, set PICOCLAW_HOME
|
||||
os.Unsetenv("PICOCLAW_CONFIG")
|
||||
customHome := "/custom/home"
|
||||
os.Setenv("PICOCLAW_HOME", customHome)
|
||||
defer os.Unsetenv("PICOCLAW_HOME")
|
||||
|
||||
got, err := ConfigPath()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigPath() error = %v", err)
|
||||
}
|
||||
|
||||
want := filepath.Join(customHome, "config.json")
|
||||
if got != want {
|
||||
t.Errorf("ConfigPath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDir_Default(t *testing.T) {
|
||||
os.Unsetenv("PICOCLAW_HOME")
|
||||
|
||||
got, err := ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir() error = %v", err)
|
||||
}
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
want := filepath.Join(home, ".picoclaw")
|
||||
if got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDir_WithPICOCLAW_HOME(t *testing.T) {
|
||||
customHome := "/custom/picoclaw/home"
|
||||
os.Setenv("PICOCLAW_HOME", customHome)
|
||||
defer os.Unsetenv("PICOCLAW_HOME")
|
||||
|
||||
got, err := ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir() error = %v", err)
|
||||
}
|
||||
|
||||
if got != customHome {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, customHome)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigPath_Priority(t *testing.T) {
|
||||
// PICOCLAW_CONFIG should take priority over PICOCLAW_HOME
|
||||
configPath := "/priority/config.json"
|
||||
homePath := "/priority/home"
|
||||
|
||||
os.Setenv("PICOCLAW_CONFIG", configPath)
|
||||
os.Setenv("PICOCLAW_HOME", homePath)
|
||||
defer func() {
|
||||
os.Unsetenv("PICOCLAW_CONFIG")
|
||||
os.Unsetenv("PICOCLAW_HOME")
|
||||
}()
|
||||
|
||||
got, err := ConfigPath()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigPath() error = %v", err)
|
||||
}
|
||||
|
||||
if got != configPath {
|
||||
t.Errorf("ConfigPath() = %q, want %q (PICOCLAW_CONFIG should have priority)", got, configPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Windows-specific test for case-insensitive comparison
|
||||
func TestConfigPath_Windows(t *testing.T) {
|
||||
if os.PathSeparator != '\\' {
|
||||
t.Skip("Skipping Windows-specific test on non-Windows platform")
|
||||
}
|
||||
|
||||
os.Unsetenv("PICOCLAW_CONFIG")
|
||||
os.Unsetenv("PICOCLAW_HOME")
|
||||
|
||||
got, err := ConfigPath()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigPath() error = %v", err)
|
||||
}
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
want := filepath.Join(home, ".picoclaw", "config.json")
|
||||
if !strings.EqualFold(got, want) {
|
||||
t.Errorf("ConfigPath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue