mirror of
https://github.com/rancher/norman.git
synced 2025-09-17 23:59:36 +00:00
Move packages from rancher/rancher to norman
This commit is contained in:
39
pkg/resolvehome/home.go
Normal file
39
pkg/resolvehome/home.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package resolvehome
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/user"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
homes = []string{"$HOME", "${HOME}", "~"}
|
||||
)
|
||||
|
||||
func Resolve(s string) (string, error) {
|
||||
for _, home := range homes {
|
||||
if strings.Contains(s, home) {
|
||||
homeDir, err := getHomeDir()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "determining current user")
|
||||
}
|
||||
s = strings.Replace(s, home, homeDir, -1)
|
||||
}
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func getHomeDir() (string, error) {
|
||||
if os.Getuid() == 0 {
|
||||
return "/root", nil
|
||||
}
|
||||
|
||||
u, err := user.Current()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "determining current user, try set HOME and USER env vars")
|
||||
}
|
||||
return u.HomeDir, nil
|
||||
}
|
Reference in New Issue
Block a user