mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
util: GetHome now uses $HOME as override instead of fallback.
This commit is contained in:
parent
7f58514b37
commit
ffc1634ce5
|
@ -7,7 +7,19 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// GetHome finds the home directory of a specified user. When given an empty
|
||||
// string, it finds the home directory of the current user.
|
||||
func GetHome(uname string) (string, error) {
|
||||
if uname == "" {
|
||||
// Use $HOME as override if we are looking for the home of the current
|
||||
// variable.
|
||||
home := os.Getenv("HOME")
|
||||
if home != "" {
|
||||
return strings.TrimRight(home, "/"), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Look up the
|
||||
var u *user.User
|
||||
var err error
|
||||
if uname == "" {
|
||||
|
@ -16,13 +28,6 @@ func GetHome(uname string) (string, error) {
|
|||
u, err = user.Lookup(uname)
|
||||
}
|
||||
if err != nil {
|
||||
if uname == "" {
|
||||
// Use $HOME as fallback
|
||||
home := os.Getenv("HOME")
|
||||
if home != "" {
|
||||
return strings.TrimRight(home, "/"), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("can't resolve ~%s: %s", uname, err.Error())
|
||||
}
|
||||
return strings.TrimRight(u.HomeDir, "/"), nil
|
||||
|
|
Loading…
Reference in New Issue
Block a user