common, node: move datadir defaults into package node

This commit is contained in:
Felix Lange
2016-09-16 11:53:50 +02:00
parent eeb322ae64
commit b42a5b118f
7 changed files with 43 additions and 49 deletions

View File

@ -137,9 +137,19 @@ func (self *DirectoryFlag) Set(value string) {
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func expandPath(p string) string {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
if user, err := user.Current(); err == nil {
p = user.HomeDir + p[1:]
if home := homeDir(); home != "" {
p = home + p[1:]
}
}
return path.Clean(os.ExpandEnv(p))
}
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}