Fix default config values.
[src/xds/xds-agent.git] / lib / xdsconfig / fileconfig.go
1 package xdsconfig
2
3 import (
4         "encoding/json"
5         "os"
6         "os/user"
7         "path"
8         "path/filepath"
9
10         common "github.com/iotbzh/xds-common/golib"
11 )
12
13 type SyncThingConf struct {
14         BinDir     string `json:"binDir"`
15         Home       string `json:"home"`
16         GuiAddress string `json:"gui-address"`
17         GuiAPIKey  string `json:"gui-apikey"`
18 }
19
20 type FileConfig struct {
21         HTTPPort  string         `json:"httpPort"`
22         LogsDir   string         `json:"logsDir"`
23         XDSAPIKey string         `json:"xds-apikey"`
24         SThgConf  *SyncThingConf `json:"syncthing"`
25 }
26
27 // getConfigFromFile reads configuration from a config file.
28 // Order to determine which config file is used:
29 //  1/ from command line option: "--config myConfig.json"
30 //  2/ $HOME/.xds/agent/agent-config.json file
31 //  3/ <current_dir>/agent-config.json file
32 //  4/ <executable dir>/agent-config.json file
33
34 func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
35
36         searchIn := make([]string, 0, 3)
37         if confFile != "" {
38                 searchIn = append(searchIn, confFile)
39         }
40         if usr, err := user.Current(); err == nil {
41                 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "agent", "agent-config.json"))
42         }
43         cwd, err := os.Getwd()
44         if err == nil {
45                 searchIn = append(searchIn, path.Join(cwd, "agent-config.json"))
46         }
47         exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
48         if err == nil {
49                 searchIn = append(searchIn, path.Join(exePath, "agent-config.json"))
50         }
51
52         var cFile *string
53         for _, p := range searchIn {
54                 if _, err := os.Stat(p); err == nil {
55                         cFile = &p
56                         break
57                 }
58         }
59         // Use default settings
60         fCfg := *c.FileConf
61
62         // Read config file when existing
63         if cFile != nil {
64                 c.Log.Infof("Use config file: %s", *cFile)
65
66                 // TODO move on viper package to support comments in JSON and also
67                 // bind with flags (command line options)
68                 // see https://github.com/spf13/viper#working-with-flags
69
70                 fd, _ := os.Open(*cFile)
71                 defer fd.Close()
72                 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
73                         return nil, err
74                 }
75         }
76
77         // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
78         vars := []*string{
79                 &fCfg.LogsDir,
80         }
81         if fCfg.SThgConf != nil {
82                 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
83         }
84         for _, field := range vars {
85                 var err error
86                 *field, err = common.ResolveEnvVar(*field)
87                 if err != nil {
88                         return nil, err
89                 }
90         }
91
92         // Config file settings overwrite default config
93         if fCfg.HTTPPort != "" {
94                 c.HTTPPort = fCfg.HTTPPort
95         }
96
97         // Set default apikey
98         // FIXME - rework with dynamic key
99         if fCfg.XDSAPIKey == "" {
100                 fCfg.XDSAPIKey = "1234abcezam"
101         }
102
103         return &fCfg, nil
104 }