path.go 857 B

123456789101112131415161718192021222324252627282930
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. )
  7. // EnvConfigPath is the env var that, when set, overrides XDG lookup entirely.
  8. const EnvConfigPath = "CC_SWITCH_CONFIG"
  9. // ResolvePath returns the path cc-switch reads/writes. Precedence:
  10. // 1. CC_SWITCH_CONFIG (full path, used as-is)
  11. // 2. $XDG_CONFIG_HOME/cc-switch/config.yaml
  12. // 3. ~/.config/cc-switch/config.yaml
  13. //
  14. // The --config flag, when provided, bypasses this function entirely.
  15. func ResolvePath() (string, error) {
  16. if v := os.Getenv(EnvConfigPath); v != "" {
  17. return ExpandUser(v)
  18. }
  19. if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
  20. return filepath.Join(xdg, "cc-switch", "config.yaml"), nil
  21. }
  22. home, err := os.UserHomeDir()
  23. if err != nil {
  24. return "", fmt.Errorf("resolve home dir: %w", err)
  25. }
  26. return filepath.Join(home, ".config", "cc-switch", "config.yaml"), nil
  27. }