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