diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-09-22 15:39:41 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-09-22 15:39:41 +0200 |
commit | 59a1e17527c91c1cddc344ec4273a905336eecdd (patch) | |
tree | 502f08064eaf58a68ea0a33f5c513b1d729be058 /golib | |
parent | 4b8e35b6786b614c11feb99232d0d39c1eb23f32 (diff) |
Added ${EXEPATH} support to ResolveEnvVar.
Diffstat (limited to 'golib')
-rw-r--r-- | golib/filepath.go | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/golib/filepath.go b/golib/filepath.go index 476096c..b27ac6f 100644 --- a/golib/filepath.go +++ b/golib/filepath.go @@ -52,15 +52,34 @@ func ResolveEnvVar(s string) (string, error) { vars := re.FindAllStringSubmatch(s, -1) res := s for _, v := range vars { - val := os.Getenv(v[1]) - if val == "" { - // 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 + val := "" + if v[1] == "EXEPATH" { + // Specific case to resolve $EXEPATH or ${EXEPATH} used as current executable path + exePath := os.Args[0] + ee, _ := os.Executable() + exeAbsPath, err := filepath.Abs(ee) + if err == nil { + exePath, err = filepath.EvalSymlinks(exeAbsPath) + if err == nil { + exePath = filepath.Dir(ee) + } else { + exePath = filepath.Dir(exeAbsPath) + } + } + val = exePath + + } else { + // Get env var value + val = os.Getenv(v[1]) + if val == "" { + // 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]) } - } else { - return res, fmt.Errorf("ERROR: %s env variable not defined", v[1]) } } |