Skip to content

Commit d4585d5

Browse files
committed
Update config dir
1 parent 470ece0 commit d4585d5

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ push notify-group my-team --title "Standup" --body "Daily standup in 5 minutes"
8787

8888
## Configuration
8989

90-
The API key is stored in `~/.push/config.yaml`.
90+
The API key is stored in `<config-dir>/push/config.yaml`, where `<config-dir>` is `~/Library/Application Support` on macOS, `~/.config` on Linux, and `%AppData%` on Windows.
9191

9292
## License
9393

internal/config/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ import (
1212
var osExit = os.Exit
1313

1414
const (
15-
configDir = ".push"
15+
configDir = "push"
1616
configFile = "config"
1717
configType = "yaml"
1818
)
1919

2020
func Init() {
21-
home, err := os.UserHomeDir()
21+
cfgBase, err := os.UserConfigDir()
2222
if err != nil {
23-
fmt.Fprintf(os.Stderr, "Error finding home directory: %v\n", err)
23+
fmt.Fprintf(os.Stderr, "Error finding config directory: %v\n", err)
2424
osExit(1)
2525
}
2626

27-
dir := filepath.Join(home, configDir)
27+
dir := filepath.Join(cfgBase, configDir)
2828
viper.AddConfigPath(dir)
2929
viper.SetConfigName(configFile)
3030
viper.SetConfigType(configType)
@@ -38,12 +38,12 @@ func Init() {
3838
}
3939

4040
func SetAPIKey(key string) error {
41-
home, err := os.UserHomeDir()
41+
cfgBase, err := os.UserConfigDir()
4242
if err != nil {
43-
return fmt.Errorf("finding home directory: %w", err)
43+
return fmt.Errorf("finding config directory: %w", err)
4444
}
4545

46-
dir := filepath.Join(home, configDir)
46+
dir := filepath.Join(cfgBase, configDir)
4747
if err := os.MkdirAll(dir, 0700); err != nil {
4848
return fmt.Errorf("creating config directory: %w", err)
4949
}

internal/config/config_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ func TestMaskedAPIKey_NineChars(t *testing.T) {
5252

5353
func TestInit_MalformedConfig(t *testing.T) {
5454
tmpDir := t.TempDir()
55-
configDir := filepath.Join(tmpDir, ".push")
56-
os.MkdirAll(configDir, 0700)
57-
os.WriteFile(filepath.Join(configDir, "config.yaml"), []byte(":::invalid yaml"), 0600)
55+
t.Setenv("HOME", tmpDir)
5856

59-
origHome := os.Getenv("HOME")
60-
os.Setenv("HOME", tmpDir)
61-
defer os.Setenv("HOME", origHome)
57+
cfgBase, err := os.UserConfigDir()
58+
if err != nil {
59+
t.Fatalf("UserConfigDir() error: %v", err)
60+
}
61+
cfgDir := filepath.Join(cfgBase, "push")
62+
os.MkdirAll(cfgDir, 0700)
63+
os.WriteFile(filepath.Join(cfgDir, "config.yaml"), []byte(":::invalid yaml"), 0600)
6264

6365
viper.Reset()
6466

@@ -78,18 +80,19 @@ func TestInit_MalformedConfig(t *testing.T) {
7880

7981
func TestSetAPIKey_FilePermissions(t *testing.T) {
8082
tmpDir := t.TempDir()
81-
82-
origHome := os.Getenv("HOME")
83-
os.Setenv("HOME", tmpDir)
84-
defer os.Setenv("HOME", origHome)
83+
t.Setenv("HOME", tmpDir)
8584

8685
viper.Reset()
8786

8887
if err := SetAPIKey("test-key"); err != nil {
8988
t.Fatalf("SetAPIKey() error: %v", err)
9089
}
9190

92-
configPath := filepath.Join(tmpDir, ".push", "config.yaml")
91+
cfgBase, err := os.UserConfigDir()
92+
if err != nil {
93+
t.Fatalf("UserConfigDir() error: %v", err)
94+
}
95+
configPath := filepath.Join(cfgBase, "push", "config.yaml")
9396
info, err := os.Stat(configPath)
9497
if err != nil {
9598
t.Fatalf("Stat(%q) error: %v", configPath, err)

0 commit comments

Comments
 (0)