get user home

This commit is contained in:
Liang Ding 2015-02-16 10:15:09 +08:00
parent c5f3942ed7
commit 9cc6e9fab6
2 changed files with 13 additions and 16 deletions

View File

@ -20,7 +20,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"sort"
"strconv"
@ -161,26 +160,17 @@ func initWide(confPath, confIP, confPort, confServer, confLogLevel, confStaticSe
logger.Debugf("${pwd} [%s]", Wide.WD)
// User Home
userHome := ""
user, err := user.Current()
if nil == err {
userHome = user.HomeDir
} else {
// cross compile support for darwin
home, er := util.OS.Home()
if nil != er {
logger.Error("Can't get user's home, please report this issue to developer", err, er)
home, err := util.OS.Home()
if nil != err {
logger.Error("Can't get user's home, please report this issue to developer", err)
os.Exit(-1)
}
userHome = home
os.Exit(-1)
}
logger.Debugf("${user.home} [%s]", userHome)
logger.Debugf("${user.home} [%s]", home)
// Playground Directory
Wide.Playground = strings.Replace(Wide.Playground, "${home}", userHome, 1)
Wide.Playground = strings.Replace(Wide.Playground, "${home}", home, 1)
if "" != confPlayground {
Wide.Playground = confPlayground
}

View File

@ -19,6 +19,7 @@ import (
"errors"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
@ -47,6 +48,12 @@ func (*myos) Pwd() string {
// This uses an OS-specific method for discovering the home directory.
// An error is returned if a home directory cannot be detected.
func (*myos) Home() (string, error) {
user, err := user.Current()
if nil == err {
return user.HomeDir, nil
}
// cross compile support
if OS.IsWindows() {
return homeWindows()
}