mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
33 lines
653 B
Go
33 lines
653 B
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
"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, "~\\") {
|
|
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 ""
|
|
}
|