2018-07-19 16:31:50 +00:00
|
|
|
package cmd
|
2015-07-28 22:16:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/user"
|
2018-01-13 22:31:28 +00:00
|
|
|
"path"
|
2015-07-28 22:16:16 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Expands a file path
|
|
|
|
// 1. replace tilde with users home dir
|
|
|
|
// 2. expands embedded environment variables
|
|
|
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
|
|
|
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
|
|
|
func expandPath(p string) string {
|
|
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
2018-01-13 22:31:28 +00:00
|
|
|
if home := homeDir(); home != "" {
|
|
|
|
p = home + p[1:]
|
2015-07-28 22:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-13 22:31:28 +00:00
|
|
|
return path.Clean(os.ExpandEnv(p))
|
|
|
|
}
|
2015-07-28 22:16:16 +00:00
|
|
|
|
2018-01-13 22:31:28 +00:00
|
|
|
func homeDir() string {
|
|
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
if usr, err := user.Current(); err == nil {
|
|
|
|
return usr.HomeDir
|
|
|
|
}
|
|
|
|
return ""
|
2015-07-28 22:16:16 +00:00
|
|
|
}
|