10 "github.com/iotbzh/xds-agent/lib/common"
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"`
20 type FileConfig struct {
21 HTTPPort string `json:"httpPort"`
22 LogsDir string `json:"logsDir"`
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-config.json file
30 // 3/ <current_dir>/agent-config.json file
31 // 4/ <executable dir>/agent-config.json file
33 func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
35 searchIn := make([]string, 0, 3)
37 searchIn = append(searchIn, confFile)
39 if usr, err := user.Current(); err == nil {
40 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "agent-config.json"))
42 cwd, err := os.Getwd()
44 searchIn = append(searchIn, path.Join(cwd, "agent-config.json"))
46 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
48 searchIn = append(searchIn, path.Join(exePath, "agent-config.json"))
52 for _, p := range searchIn {
53 if _, err := os.Stat(p); err == nil {
60 // No config file found
64 c.Log.Infof("Use config file: %s", *cFile)
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
70 fd, _ := os.Open(*cFile)
72 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
76 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
77 for _, field := range []*string{
80 &fCfg.SThgConf.BinDir} {
83 *field, err = common.ResolveEnvVar(*field)
89 // Config file settings overwrite default config
90 if fCfg.HTTPPort != "" {
91 c.HTTPPort = fCfg.HTTPPort