9 common "github.com/iotbzh/xds-common/golib"
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"`
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"`
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
32 func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
34 searchIn := make([]string, 0, 3)
36 searchIn = append(searchIn, confFile)
38 if usr, err := user.Current(); err == nil {
39 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "agent", "agent-config.json"))
42 searchIn = append(searchIn, "/etc/xds-agent/config.json")
45 for _, p := range searchIn {
46 if _, err := os.Stat(p); err == nil {
51 // Use default settings
54 // Read config file when existing
56 c.Log.Infof("Use config file: %s", *cFile)
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
62 fd, _ := os.Open(*cFile)
64 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
69 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
73 if fCfg.SThgConf != nil {
74 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
76 for _, field := range vars {
78 *field, err = common.ResolveEnvVar(*field)
84 // Config file settings overwrite default config
85 if fCfg.HTTPPort != "" {
86 c.HTTPPort = fCfg.HTTPPort
90 // FIXME - rework with dynamic key
91 if fCfg.XDSAPIKey == "" {
92 fCfg.XDSAPIKey = "1234abcezam"