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 return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
49 rer := regexp.MustCompile("\\${" + v[1] + "}")
50 res = rer.ReplaceAllString(res, val)
53 // Resolved $MYVAR following by a slash (or a backslash for Windows)
55 //re := regexp.MustCompile("\\$([^\\/])+/")
57 return path.Clean(res), nil
61 func PathNormalize(p string) string {
62 sep := string(filepath.Separator)
66 // Replace drive like C: by C/
69 res = p[0:1] + sep + p[2:]
71 res = strings.Replace(res, "\\", "/", -1)
72 return filepath.Clean(res)