Remove duplicate fields and set default SDK dir.
[src/xds/xds-server.git] / lib / common / filepath.go
index 42ef82f..8101c5a 100644 (file)
@@ -7,6 +7,7 @@ import (
        "path"
        "path/filepath"
        "regexp"
+       "strings"
 )
 
 // Exists returns whether the given file or directory exists or not
@@ -24,9 +25,12 @@ func Exists(path string) bool {
 // ResolveEnvVar Resolved environment variable regarding the syntax ${MYVAR}
 // or $MYVAR following by a slash or a backslash
 func ResolveEnvVar(s string) (string, error) {
+       if s == "" {
+               return s, nil
+       }
 
        // Resolved tilde : ~/
-       if s[:2] == "~/" {
+       if len(s) > 2 && s[:2] == "~/" {
                if usr, err := user.Current(); err == nil {
                        s = filepath.Join(usr.HomeDir, s[2:])
                }
@@ -52,3 +56,18 @@ func ResolveEnvVar(s string) (string, error) {
 
        return path.Clean(res), nil
 }
+
+// PathNormalize
+func PathNormalize(p string) string {
+       sep := string(filepath.Separator)
+       if sep != "/" {
+               return p
+       }
+       // Replace drive like C: by C/
+       res := p
+       if p[1:2] == ":" {
+               res = p[0:1] + sep + p[2:]
+       }
+       res = strings.Replace(res, "\\", "/", -1)
+       return filepath.Clean(res)
+}