Set default xds-apikey when not set by config.
[src/xds/xds-agent.git] / lib / xdsconfig / fileconfig.go
index 0c4828c..2795206 100644 (file)
@@ -2,12 +2,12 @@ package xdsconfig
 
 import (
        "encoding/json"
-       "fmt"
        "os"
        "os/user"
        "path"
        "path/filepath"
-       "regexp"
+
+       common "github.com/iotbzh/xds-common/golib"
 )
 
 type SyncThingConf struct {
@@ -18,8 +18,10 @@ type SyncThingConf struct {
 }
 
 type FileConfig struct {
-       HTTPPort string         `json:"httpPort"`
-       SThgConf *SyncThingConf `json:"syncthing"`
+       HTTPPort  string         `json:"httpPort"`
+       LogsDir   string         `json:"logsDir"`
+       XDSAPIKey string         `json:"xds-apikey"`
+       SThgConf  *SyncThingConf `json:"syncthing"`
 }
 
 // getConfigFromFile reads configuration from a config file.
@@ -60,7 +62,7 @@ func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
                return &fCfg, nil
        }
 
-       c.log.Infof("Use config file: %s", *cFile)
+       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)
@@ -73,48 +75,28 @@ func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
        }
 
        // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
-       // TODO: better to use reflect package to iterate on fields and be more generic
-       var rep string
-
-       if rep, err = resolveEnvVar(fCfg.SThgConf.BinDir); err != nil {
-               return nil, err
+       for _, field := range []*string{
+               &fCfg.LogsDir,
+               &fCfg.SThgConf.Home,
+               &fCfg.SThgConf.BinDir} {
+
+               var err error
+               *field, err = common.ResolveEnvVar(*field)
+               if err != nil {
+                       return nil, err
+               }
        }
-       fCfg.SThgConf.BinDir = path.Clean(rep)
 
-       if rep, err = resolveEnvVar(fCfg.SThgConf.Home); err != nil {
-               return nil, err
+       // Config file settings overwrite default config
+       if fCfg.HTTPPort != "" {
+               c.HTTPPort = fCfg.HTTPPort
        }
-       fCfg.SThgConf.Home = path.Clean(rep)
-
-       return &fCfg, nil
-}
 
-// resolveEnvVar Resolved environment variable regarding the syntax ${MYVAR}
-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 == "" {
-                       return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
-               }
-
-               rer := regexp.MustCompile("\\${" + v[1] + "}")
-               res = rer.ReplaceAllString(res, val)
+       // Set default apikey
+       // FIXME - rework with dynamic key
+       if fCfg.XDSAPIKey == "" {
+               fCfg.XDSAPIKey = "1234abcezam"
        }
 
-       return res, nil
-}
-
-// exists returns whether the given file or directory exists or not
-func exists(path string) bool {
-       _, err := os.Stat(path)
-       if err == nil {
-               return true
-       }
-       if os.IsNotExist(err) {
-               return false
-       }
-       return true
+       return &fCfg, nil
 }