Skip to content

Commit 9883bd3

Browse files
committed
Support named profiles in XDG path
1 parent c48f297 commit 9883bd3

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

cmd/tokendiff/main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func prescanProfile() string {
110110

111111
// defineFlags sets up all command-line flags with config defaults
112112
func defineFlags(cfg config) cliFlags {
113-
_ = flag.String("profile", "", "use settings from ~/.tokendiffrc.<profile>")
113+
_ = flag.String("profile", "", "use settings from ~/.tokendiffrc.<profile> or $XDG_CONFIG_HOME/tokendiff/config.<profile>")
114114

115115
f := cliFlags{
116116
delimiters: flag.StringP("delimiters", "d", cfg.delimiters, "delimiter characters"),
@@ -577,12 +577,23 @@ func findConfigFile(profile string) (string, error) {
577577
return "", nil // No default config found, use defaults
578578
}
579579

580-
// Profile explicitly specified - file must exist
580+
// Profile explicitly specified - check ~/.tokendiffrc.<profile> first
581581
path := filepath.Join(home, ".tokendiffrc."+profile)
582-
if _, err := os.Stat(path); err != nil {
583-
return "", fmt.Errorf("profile config file not found: %s", path)
582+
if _, err := os.Stat(path); err == nil {
583+
return path, nil
584584
}
585-
return path, nil
585+
586+
// Then check XDG_CONFIG_HOME/tokendiff/config.<profile>
587+
xdgConfig := os.Getenv("XDG_CONFIG_HOME")
588+
if xdgConfig == "" {
589+
xdgConfig = filepath.Join(home, ".config")
590+
}
591+
xdgPath := filepath.Join(xdgConfig, "tokendiff", "config."+profile)
592+
if _, err := os.Stat(xdgPath); err == nil {
593+
return xdgPath, nil
594+
}
595+
596+
return "", fmt.Errorf("profile config file not found: %s or %s", path, xdgPath)
586597
}
587598

588599
// loadConfig reads a config file and returns the configuration.

cmd/tokendiff/main_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,78 @@ func TestFindConfigFile(t *testing.T) {
818818
}
819819
})
820820

821+
t.Run("finds XDG profile-specific config", func(t *testing.T) {
822+
xdgDir := filepath.Join(tmpHome, ".config", "tokendiff")
823+
if err := os.MkdirAll(xdgDir, 0755); err != nil {
824+
t.Fatalf("failed to create XDG dir: %v", err)
825+
}
826+
profileConfig := filepath.Join(xdgDir, "config.xdgprofile")
827+
if err := os.WriteFile(profileConfig, []byte("# xdg profile config"), 0644); err != nil {
828+
t.Fatalf("failed to create XDG profile config: %v", err)
829+
}
830+
defer os.RemoveAll(filepath.Join(tmpHome, ".config"))
831+
832+
result, err := findConfigFile("xdgprofile")
833+
if err != nil {
834+
t.Errorf("unexpected error: %v", err)
835+
}
836+
if result != profileConfig {
837+
t.Errorf("expected %q, got %q", profileConfig, result)
838+
}
839+
})
840+
841+
t.Run("prefers home profile over XDG profile", func(t *testing.T) {
842+
// Create home profile config
843+
homeProfile := filepath.Join(tmpHome, ".tokendiffrc.bothprofile")
844+
if err := os.WriteFile(homeProfile, []byte("# home profile config"), 0644); err != nil {
845+
t.Fatalf("failed to create home profile config: %v", err)
846+
}
847+
defer os.Remove(homeProfile)
848+
849+
// Create XDG profile config
850+
xdgDir := filepath.Join(tmpHome, ".config", "tokendiff")
851+
if err := os.MkdirAll(xdgDir, 0755); err != nil {
852+
t.Fatalf("failed to create XDG dir: %v", err)
853+
}
854+
xdgProfile := filepath.Join(xdgDir, "config.bothprofile")
855+
if err := os.WriteFile(xdgProfile, []byte("# xdg profile config"), 0644); err != nil {
856+
t.Fatalf("failed to create XDG profile config: %v", err)
857+
}
858+
defer os.RemoveAll(filepath.Join(tmpHome, ".config"))
859+
860+
result, err := findConfigFile("bothprofile")
861+
if err != nil {
862+
t.Errorf("unexpected error: %v", err)
863+
}
864+
if result != homeProfile {
865+
t.Errorf("expected home profile %q, got %q", homeProfile, result)
866+
}
867+
})
868+
869+
t.Run("respects XDG_CONFIG_HOME for profiles", func(t *testing.T) {
870+
customXDG := filepath.Join(tmpHome, "custom-xdg-profile")
871+
xdgDir := filepath.Join(customXDG, "tokendiff")
872+
if err := os.MkdirAll(xdgDir, 0755); err != nil {
873+
t.Fatalf("failed to create custom XDG dir: %v", err)
874+
}
875+
profileConfig := filepath.Join(xdgDir, "config.customprofile")
876+
if err := os.WriteFile(profileConfig, []byte("# custom xdg profile config"), 0644); err != nil {
877+
t.Fatalf("failed to create profile config: %v", err)
878+
}
879+
defer os.RemoveAll(customXDG)
880+
881+
os.Setenv("XDG_CONFIG_HOME", customXDG)
882+
defer os.Unsetenv("XDG_CONFIG_HOME")
883+
884+
result, err := findConfigFile("customprofile")
885+
if err != nil {
886+
t.Errorf("unexpected error: %v", err)
887+
}
888+
if result != profileConfig {
889+
t.Errorf("expected %q, got %q", profileConfig, result)
890+
}
891+
})
892+
821893
t.Run("respects XDG_CONFIG_HOME", func(t *testing.T) {
822894
customXDG := filepath.Join(tmpHome, "custom-xdg")
823895
xdgDir := filepath.Join(customXDG, "tokendiff")

0 commit comments

Comments
 (0)