Skip to content

Commit d54a761

Browse files
bigkevmcdsamcoe
authored andcommitted
Configuration path can come from environment.
This adds support for using the GH_CONFIG_DIR environment variable to determine where the config files are written, this is useful for cases where the homedir is not writable.
1 parent d0a4639 commit d54a761

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

internal/config/config_file.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"gopkg.in/yaml.v3"
1515
)
1616

17+
const (
18+
GH_CONFIG_DIR = "GH_CONFIG_DIR"
19+
)
20+
1721
func ConfigDir() string {
1822
homeDir, _ := homeDirAutoMigrate()
1923
return homeDir
@@ -263,3 +267,10 @@ func findRegularFile(p string) string {
263267
}
264268
return ""
265269
}
270+
271+
func configPath() string {
272+
if v := os.Getenv(GH_CONFIG_DIR); v != "" {
273+
return v
274+
}
275+
return "~/.config/gh"
276+
}

internal/config/config_file_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"bytes"
55
"fmt"
6+
"os"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -146,3 +147,23 @@ func Test_parseConfigFile(t *testing.T) {
146147
})
147148
}
148149
}
150+
151+
func Test_configPath(t *testing.T) {
152+
tests := []struct {
153+
envVar string
154+
want string
155+
}{
156+
{"/tmp/gh", "/tmp/gh"},
157+
{"", "~/.config/gh"},
158+
}
159+
160+
for _, tt := range tests {
161+
t.Run(fmt.Sprintf("envVar: %q", tt.envVar), func(t *testing.T) {
162+
if tt.envVar != "" {
163+
os.Setenv(GH_CONFIG_DIR, tt.envVar)
164+
defer os.Unsetenv(GH_CONFIG_DIR)
165+
}
166+
eq(t, configPath(), tt.want)
167+
})
168+
}
169+
}

0 commit comments

Comments
 (0)