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

View File

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