10 common "github.com/iotbzh/xds-common/golib"
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 XDSAPIKey string `json:"xds-apikey"`
24 SThgConf *SyncThingConf `json:"syncthing"`
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-config.json file
31 // 3/ <current_dir>/agent-config.json file
32 // 4/ <executable dir>/agent-config.json file
34 func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
36 searchIn := make([]string, 0, 3)
38 searchIn = append(searchIn, confFile)
40 if usr, err := user.Current(); err == nil {
41 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "agent-config.json"))
43 cwd, err := os.Getwd()
45 searchIn = append(searchIn, path.Join(cwd, "agent-config.json"))
47 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
49 searchIn = append(searchIn, path.Join(exePath, "agent-config.json"))
53 for _, p := range searchIn {
54 if _, err := os.Stat(p); err == nil {
61 // No config file found
65 c.Log.Infof("Use config file: %s", *cFile)
67 // TODO move on viper package to support comments in JSON and also
68 // bind with flags (command line options)
69 // see https://github.com/spf13/viper#working-with-flags
71 fd, _ := os.Open(*cFile)
73 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
77 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
81 if fCfg.SThgConf != nil {
82 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
84 for _, field := range vars {
86 *field, err = common.ResolveEnvVar(*field)
92 // Config file settings overwrite default config
93 if fCfg.HTTPPort != "" {
94 c.HTTPPort = fCfg.HTTPPort
98 // FIXME - rework with dynamic key
99 if fCfg.XDSAPIKey == "" {
100 fCfg.XDSAPIKey = "1234abcezam"