Set SUB_VERSION as current commit ID.
[src/xds/xds-agent.git] / lib / common / filepath.go
index 42ef82f..d9cb3d5 100644 (file)
@@ -7,6 +7,7 @@ import (
        "path"
        "path/filepath"
        "regexp"
+       "runtime"
 )
 
 // 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:])
                }
@@ -39,7 +43,14 @@ func ResolveEnvVar(s string) (string, error) {
        for _, v := range vars {
                val := os.Getenv(v[1])
                if val == "" {
-                       return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
+                       // Specific case to resolved $HOME or ${HOME} on Windows host
+                       if runtime.GOOS == "windows" && v[1] == "HOME" {
+                               if usr, err := user.Current(); err == nil {
+                                       val = usr.HomeDir
+                               }
+                       } else {
+                               return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
+                       }
                }
 
                rer := regexp.MustCompile("\\${" + v[1] + "}")