diff --git a/auth/config.go b/auth/config.go index d23ab88..40bfcea 100644 --- a/auth/config.go +++ b/auth/config.go @@ -6,18 +6,24 @@ import ( ) // testConfigDir can be set during tests to override the config directory -var testConfigDir string +var overrideConfigDir string // SetTestConfigDir sets the config directory for testing purposes. // Pass empty string to reset to default behavior. func SetTestConfigDir(dir string) { - testConfigDir = dir + OverrideConfigDir(dir) +} + +// OverrideConfigDir sets the config directory to something other than the default. +// Pass empty string to reset to default behavior. +func OverrideConfigDir(path string) { + overrideConfigDir = path } // configDir returns the configuration directory path (e.g., ~/.config/FreeCarnival) func configDir() (string, error) { - if testConfigDir != "" { - return testConfigDir, nil + if overrideConfigDir != "" { + return overrideConfigDir, nil } dir, err := os.UserConfigDir() if err != nil { diff --git a/auth/session_test.go b/auth/session_test.go index 3277904..6567eb3 100644 --- a/auth/session_test.go +++ b/auth/session_test.go @@ -15,9 +15,9 @@ func setupTestConfigDir(t *testing.T) func() { if err != nil { t.Fatalf("failed to create temp dir: %v", err) } - testConfigDir = tmpDir + overrideConfigDir = tmpDir return func() { - testConfigDir = "" + overrideConfigDir = "" os.RemoveAll(tmpDir) } } @@ -49,7 +49,7 @@ func TestSaveSession(t *testing.T) { } // Verify file was created - path := filepath.Join(testConfigDir, "session.json") + path := filepath.Join(overrideConfigDir, "session.json") if _, err := os.Stat(path); os.IsNotExist(err) { t.Fatal("session.json was not created") } @@ -130,8 +130,8 @@ func TestLoadSessionClient_InvalidJSON(t *testing.T) { defer cleanup() // Create invalid JSON session file - path := filepath.Join(testConfigDir, "session.json") - if err := os.MkdirAll(testConfigDir, 0o700); err != nil { + path := filepath.Join(overrideConfigDir, "session.json") + if err := os.MkdirAll(overrideConfigDir, 0o700); err != nil { t.Fatalf("failed to create config dir: %v", err) } if err := os.WriteFile(path, []byte("not valid json"), 0o600); err != nil { @@ -218,7 +218,7 @@ func TestClearSession_Success(t *testing.T) { } // Verify session exists - path := filepath.Join(testConfigDir, "session.json") + path := filepath.Join(overrideConfigDir, "session.json") if _, err := os.Stat(path); os.IsNotExist(err) { t.Fatal("session file should exist before clearing") } diff --git a/auth/userinfo_test.go b/auth/userinfo_test.go index 70a01ff..a345e65 100644 --- a/auth/userinfo_test.go +++ b/auth/userinfo_test.go @@ -251,7 +251,7 @@ func TestSaveUserInfo(t *testing.T) { } // Verify file was created - path := filepath.Join(testConfigDir, "user.json") + path := filepath.Join(overrideConfigDir, "user.json") if _, err := os.Stat(path); os.IsNotExist(err) { t.Fatal("user.json was not created") } @@ -288,7 +288,7 @@ func TestSaveUserInfo_NilFields(t *testing.T) { } // Verify file was created - path := filepath.Join(testConfigDir, "user.json") + path := filepath.Join(overrideConfigDir, "user.json") data, err := os.ReadFile(path) if err != nil { t.Fatalf("failed to read user.json: %v", err) @@ -351,7 +351,7 @@ func TestSaveLibrary(t *testing.T) { } // Verify file was created - path := filepath.Join(testConfigDir, "library.json") + path := filepath.Join(overrideConfigDir, "library.json") if _, err := os.Stat(path); os.IsNotExist(err) { t.Fatal("library.json was not created") } @@ -390,7 +390,7 @@ func TestSaveLibrary_Empty(t *testing.T) { } // Verify file was created - path := filepath.Join(testConfigDir, "library.json") + path := filepath.Join(overrideConfigDir, "library.json") data, err := os.ReadFile(path) if err != nil { t.Fatalf("failed to read library.json: %v", err) diff --git a/go.mod b/go.mod index 618d906..5175334 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,10 @@ go 1.25 require ( github.com/spf13/cobra v1.10.2 howett.net/plist v1.0.1 + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 ) require ( - github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.9 // indirect ) diff --git a/main.go b/main.go index cc27457..099bf7c 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "strings" "syscall" + "github.com/gustash/freecarnival/auth" "github.com/gustash/freecarnival/logger" "github.com/spf13/cobra" ) @@ -18,7 +19,7 @@ import ( var ( // Version is set at build time via -ldflags Version = "dev" - + //go:embed CODENAME codename string ) @@ -41,6 +42,7 @@ func defaultInstallBasePath() string { func main() { var logLevel string var showVersion bool + var configPath string // Set up context with signal handling for graceful shutdown ctx, cancel := context.WithCancel(context.Background()) @@ -83,6 +85,17 @@ func main() { default: logger.SetLevel(logger.LevelInfo) } + + // Set custom config path based on flag + if configPath != "" { + configPath, err := filepath.Abs(configPath) + if err != nil { + logger.Error("Could not get absolute path to custom config", "error", err) + } else { + logger.Debug("Using custom config path", "configPath", configPath) + auth.OverrideConfigDir(configPath) + } + } }, } @@ -99,6 +112,7 @@ func main() { rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "V", false, "Print version information") rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)") + rootCmd.PersistentFlags().StringVar(&configPath, "config-path", "", "Use custom config directory") if err := rootCmd.ExecuteContext(ctx); err != nil { logger.Error(err.Error())