util: GetHome now uses $HOME as override instead of fallback.

This commit is contained in:
Qi Xiao 2016-02-17 01:23:36 +01:00
parent 7f58514b37
commit ffc1634ce5

View File

@ -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