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.
This commit is contained in:
Kevin McDermott 2020-11-20 09:17:17 +00:00 committed by Sam Coe
parent d0a46399b7
commit d54a7618d4
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
2 changed files with 32 additions and 0 deletions

View file

@ -14,6 +14,10 @@ import (
"gopkg.in/yaml.v3"
)
const (
GH_CONFIG_DIR = "GH_CONFIG_DIR"
)
func ConfigDir() string {
homeDir, _ := homeDirAutoMigrate()
return homeDir
@ -263,3 +267,10 @@ func findRegularFile(p string) string {
}
return ""
}
func configPath() string {
if v := os.Getenv(GH_CONFIG_DIR); v != "" {
return v
}
return "~/.config/gh"
}

View file

@ -3,6 +3,7 @@ package config
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
@ -146,3 +147,23 @@ func Test_parseConfigFile(t *testing.T) {
})
}
}
func Test_configPath(t *testing.T) {
tests := []struct {
envVar string
want string
}{
{"/tmp/gh", "/tmp/gh"},
{"", "~/.config/gh"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("envVar: %q", tt.envVar), func(t *testing.T) {
if tt.envVar != "" {
os.Setenv(GH_CONFIG_DIR, tt.envVar)
defer os.Unsetenv(GH_CONFIG_DIR)
}
eq(t, configPath(), tt.want)
})
}
}