summaryrefslogtreecommitdiffstats
path: root/lib/xdsconfig
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-13 22:38:03 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-13 22:38:12 +0200
commit7ecaccbae9b7741d8a285b98885e786dc1d4c79c (patch)
tree0de38877f1abce617ab89932901ab34aa40dfd38 /lib/xdsconfig
parent9c87f58ae1bc719f17fb690e7cb886c1a60d7d3b (diff)
Detect undefined env variables used in config.json
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/xdsconfig')
-rw-r--r--lib/xdsconfig/fileconfig.go34
1 files changed, 26 insertions, 8 deletions
diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go
index 262d023..ed3e82d 100644
--- a/lib/xdsconfig/fileconfig.go
+++ b/lib/xdsconfig/fileconfig.go
@@ -2,6 +2,7 @@ package xdsconfig
import (
"encoding/json"
+ "fmt"
"os"
"os/user"
"path"
@@ -59,6 +60,8 @@ func updateConfigFromFile(c *Config, confFile string) error {
return nil
}
+ c.Log.Infof("Use config file: %s", *cFile)
+
// TODO move on viper package to support comments in JSON and also
// bind with flags (command line options)
// see https://github.com/spf13/viper#working-with-flags
@@ -73,9 +76,21 @@ func updateConfigFromFile(c *Config, confFile string) error {
// Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
// TODO: better to use reflect package to iterate on fields and be more generic
- fCfg.WebAppDir = path.Clean(resolveEnvVar(fCfg.WebAppDir))
- fCfg.ShareRootDir = path.Clean(resolveEnvVar(fCfg.ShareRootDir))
- fCfg.SThgConf.Home = path.Clean(resolveEnvVar(fCfg.SThgConf.Home))
+ var rep string
+ if rep, err = resolveEnvVar(fCfg.WebAppDir); err != nil {
+ return err
+ }
+ fCfg.WebAppDir = path.Clean(rep)
+
+ if rep, err = resolveEnvVar(fCfg.ShareRootDir); err != nil {
+ return err
+ }
+ fCfg.ShareRootDir = path.Clean(rep)
+
+ if rep, err = resolveEnvVar(fCfg.SThgConf.Home); err != nil {
+ return err
+ }
+ fCfg.SThgConf.Home = path.Clean(rep)
// Config file settings overwrite default config
@@ -106,18 +121,21 @@ func updateConfigFromFile(c *Config, confFile string) error {
}
// resolveEnvVar Resolved environment variable regarding the syntax ${MYVAR}
-func resolveEnvVar(s string) string {
+func resolveEnvVar(s string) (string, error) {
re := regexp.MustCompile("\\${(.*)}")
vars := re.FindAllStringSubmatch(s, -1)
res := s
for _, v := range vars {
val := os.Getenv(v[1])
- if val != "" {
- rer := regexp.MustCompile("\\${" + v[1] + "}")
- res = rer.ReplaceAllString(res, val)
+ if val == "" {
+ return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
}
+
+ rer := regexp.MustCompile("\\${" + v[1] + "}")
+ res = rer.ReplaceAllString(res, val)
}
- return res
+
+ return res, nil
}
// exists returns whether the given file or directory exists or not