Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions auth/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions auth/userinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
"strings"
"syscall"

"github.com/gustash/freecarnival/auth"
"github.com/gustash/freecarnival/logger"
"github.com/spf13/cobra"
)

var (
// Version is set at build time via -ldflags
Version = "dev"

//go:embed CODENAME
codename string
)
Expand All @@ -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())
Expand Down Expand Up @@ -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)
}
}
},
}

Expand All @@ -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())
Expand Down