Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pkg/path/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ func ExpandHomeDir(path string) (string, error) {
if !isHomeDirPath(path) {
return path, nil
}

homeDir, err := os.UserHomeDir()
homeDir, err := userHomeDir()
if err != nil || homeDir == "" {
return "", errors.New("failed to get user home directory")
}

if path == "~" {
return filepath.Clean(homeDir), nil
}

return filepath.Join(homeDir, path[2:]), nil
}

// userHomeDir returns the user's home directory, preferring the HOME
// environment variable (used cross-platform, including in tests) before
// falling back to os.UserHomeDir(), which on Windows reads USERPROFILE
// instead of HOME.
func userHomeDir() (string, error) {
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
return os.UserHomeDir()
}

func isHomeDirPath(path string) bool {
return path == "~" || strings.HasPrefix(path, "~/") || (filepath.Separator == '\\' && strings.HasPrefix(path, `~\`))
}
Loading