From f668c6cc855dae530ac71d4ef82e78ecc20a8f84 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Fri, 22 Sep 2017 15:57:14 +0200 Subject: Added GetUserHome Signed-off-by: Sebastien Douheret --- golib/filepath.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/golib/filepath.go b/golib/filepath.go index b27ac6f..c91f63d 100644 --- a/golib/filepath.go +++ b/golib/filepath.go @@ -108,3 +108,17 @@ func PathNormalize(p string) string { res = strings.Replace(res, "\\", "/", -1) return filepath.Clean(res) } + +// GetUserHome returns the user's home directory or empty string on error +func GetUserHome() string { + if usr, err := user.Current(); err == nil && usr != nil && usr.HomeDir != "" { + return usr.HomeDir + } + for _, p := range []string{"HOME", "HomePath"} { + if h := os.Getenv(p); h != "" { + return h + } + } + + return "" +} -- cgit 1.2.3-korg