13 // Exists returns whether the given file or directory exists or not
14 func Exists(path string) bool {
15 _, err := os.Stat(path)
19 if os.IsNotExist(err) {
25 // ResolveEnvVar Resolved environment variable regarding the syntax ${MYVAR}
26 // or $MYVAR following by a slash or a backslash
27 func ResolveEnvVar(s string) (string, error) {
32 // Resolved tilde : ~/
33 if len(s) > 2 && s[:2] == "~/" {
34 if usr, err := user.Current(); err == nil {
35 s = filepath.Join(usr.HomeDir, s[2:])
40 re := regexp.MustCompile("\\${([^}]+)}")
41 vars := re.FindAllStringSubmatch(s, -1)
43 for _, v := range vars {
44 val := os.Getenv(v[1])
46 // Specific case to resolved $HOME or ${HOME} on Windows host
47 if runtime.GOOS == "windows" && v[1] == "HOME" {
48 if usr, err := user.Current(); err == nil {
52 return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
56 rer := regexp.MustCompile("\\${" + v[1] + "}")
57 res = rer.ReplaceAllString(res, val)
60 // Resolved $MYVAR following by a slash (or a backslash for Windows)
62 //re := regexp.MustCompile("\\$([^\\/])+/")
64 return path.Clean(res), nil