Skip to content

Commit 7fecaaa

Browse files
authored
Merge pull request cli#2740 from Matt-Gleich/homedir
Refactor to use os.UserHomeDir()
2 parents 188ff13 + 64fda21 commit 7fecaaa

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

git/ssh_config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"regexp"
1010
"strings"
1111

12-
"github.com/mitchellh/go-homedir"
12+
"github.com/cli/cli/internal/config"
1313
)
1414

1515
var (
@@ -147,10 +147,10 @@ func ParseSSHConfig() SSHAliasMap {
147147

148148
p := sshParser{}
149149

150-
if homedir, err := homedir.Dir(); err == nil {
151-
userConfig := filepath.Join(homedir, ".ssh", "config")
150+
if sshDir, err := config.HomeDirPath(".ssh"); err == nil {
151+
userConfig := filepath.Join(sshDir, "config")
152152
configFiles = append([]string{userConfig}, configFiles...)
153-
p.homeDir = homedir
153+
p.homeDir = filepath.Dir(sshDir)
154154
}
155155

156156
for _, file := range configFiles {

internal/config/config_file.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77
"io/ioutil"
88
"os"
99
"path"
10+
"path/filepath"
1011
"syscall"
1112

1213
"github.com/mitchellh/go-homedir"
1314
"gopkg.in/yaml.v3"
1415
)
1516

1617
func ConfigDir() string {
17-
dir, _ := homedir.Expand("~/.config/gh")
18-
return dir
18+
homeDir, _ := homeDirAutoMigrate()
19+
return homeDir
1920
}
2021

2122
func ConfigFile() string {
@@ -30,6 +31,62 @@ func ParseDefaultConfig() (Config, error) {
3031
return ParseConfig(ConfigFile())
3132
}
3233

34+
func HomeDirPath(subdir string) (string, error) {
35+
homeDir, err := os.UserHomeDir()
36+
if err != nil {
37+
// TODO: remove go-homedir fallback in GitHub CLI v2
38+
if legacyDir, err := homedir.Dir(); err == nil {
39+
return filepath.Join(legacyDir, subdir), nil
40+
}
41+
return "", err
42+
}
43+
44+
newPath := filepath.Join(homeDir, subdir)
45+
if s, err := os.Stat(newPath); err == nil && s.IsDir() {
46+
return newPath, nil
47+
}
48+
49+
// TODO: remove go-homedir fallback in GitHub CLI v2
50+
if legacyDir, err := homedir.Dir(); err == nil {
51+
legacyPath := filepath.Join(legacyDir, subdir)
52+
if s, err := os.Stat(legacyPath); err == nil && s.IsDir() {
53+
return legacyPath, nil
54+
}
55+
}
56+
57+
return newPath, nil
58+
}
59+
60+
// Looks up the `~/.config/gh` directory with backwards-compatibility with go-homedir and auto-migration
61+
// when an old homedir location was found.
62+
func homeDirAutoMigrate() (string, error) {
63+
homeDir, err := os.UserHomeDir()
64+
if err != nil {
65+
// TODO: remove go-homedir fallback in GitHub CLI v2
66+
if legacyDir, err := homedir.Dir(); err == nil {
67+
return filepath.Join(legacyDir, ".config", "gh"), nil
68+
}
69+
return "", err
70+
}
71+
72+
newPath := filepath.Join(homeDir, ".config", "gh")
73+
_, newPathErr := os.Stat(newPath)
74+
if newPathErr == nil || !os.IsNotExist(err) {
75+
return newPath, newPathErr
76+
}
77+
78+
// TODO: remove go-homedir fallback in GitHub CLI v2
79+
if legacyDir, err := homedir.Dir(); err == nil {
80+
legacyPath := filepath.Join(legacyDir, ".config", "gh")
81+
if s, err := os.Stat(legacyPath); err == nil && s.IsDir() {
82+
_ = os.MkdirAll(filepath.Dir(newPath), 0755)
83+
return newPath, os.Rename(legacyPath, newPath)
84+
}
85+
}
86+
87+
return newPath, nil
88+
}
89+
3390
var ReadConfigFile = func(filename string) ([]byte, error) {
3491
f, err := os.Open(filename)
3592
if err != nil {

0 commit comments

Comments
 (0)