Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use os.UserHomeDir() #2740

Merged
merged 2 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions git/ssh_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"regexp"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/cli/cli/internal/config"
)

var (
Expand Down Expand Up @@ -147,10 +147,10 @@ func ParseSSHConfig() SSHAliasMap {

p := sshParser{}

if homedir, err := homedir.Dir(); err == nil {
userConfig := filepath.Join(homedir, ".ssh", "config")
if sshDir, err := config.HomeDirPath(".ssh"); err == nil {
userConfig := filepath.Join(sshDir, "config")
configFiles = append([]string{userConfig}, configFiles...)
p.homeDir = homedir
p.homeDir = filepath.Dir(sshDir)
}

for _, file := range configFiles {
Expand Down
61 changes: 59 additions & 2 deletions internal/config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"syscall"

"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3"
)

func ConfigDir() string {
dir, _ := homedir.Expand("~/.config/gh")
return dir
homeDir, _ := homeDirAutoMigrate()
return homeDir
}

func ConfigFile() string {
Expand All @@ -30,6 +31,62 @@ func ParseDefaultConfig() (Config, error) {
return ParseConfig(ConfigFile())
}

func HomeDirPath(subdir string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
// TODO: remove go-homedir fallback in GitHub CLI v2
if legacyDir, err := homedir.Dir(); err == nil {
return filepath.Join(legacyDir, subdir), nil
}
return "", err
}

newPath := filepath.Join(homeDir, subdir)
if s, err := os.Stat(newPath); err == nil && s.IsDir() {
return newPath, nil
}

// TODO: remove go-homedir fallback in GitHub CLI v2
if legacyDir, err := homedir.Dir(); err == nil {
legacyPath := filepath.Join(legacyDir, subdir)
if s, err := os.Stat(legacyPath); err == nil && s.IsDir() {
return legacyPath, nil
}
}

return newPath, nil
}

// Looks up the `~/.config/gh` directory with backwards-compatibility with go-homedir and auto-migration
// when an old homedir location was found.
func homeDirAutoMigrate() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
// TODO: remove go-homedir fallback in GitHub CLI v2
if legacyDir, err := homedir.Dir(); err == nil {
return filepath.Join(legacyDir, ".config", "gh"), nil
}
return "", err
}

newPath := filepath.Join(homeDir, ".config", "gh")
_, newPathErr := os.Stat(newPath)
if newPathErr == nil || !os.IsNotExist(err) {
return newPath, newPathErr
}

// TODO: remove go-homedir fallback in GitHub CLI v2
if legacyDir, err := homedir.Dir(); err == nil {
legacyPath := filepath.Join(legacyDir, ".config", "gh")
if s, err := os.Stat(legacyPath); err == nil && s.IsDir() {
_ = os.MkdirAll(filepath.Dir(newPath), 0755)
return newPath, os.Rename(legacyPath, newPath)
}
}

return newPath, nil
}

var ReadConfigFile = func(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
Expand Down