8 common "github.com/iotbzh/xds-common/golib"
11 type SyncThingConf struct {
12 BinDir string `json:"binDir"`
13 Home string `json:"home"`
14 GuiAddress string `json:"gui-address"`
15 GuiAPIKey string `json:"gui-apikey"`
18 type XDSServerConf struct {
19 URL string `json:"url"`
20 ConnRetry int `json:"connRetry"`
22 // private/not exported fields
24 APIBaseURL string `json:"-"`
25 APIPartialURL string `json:"-"`
28 type FileConfig struct {
29 HTTPPort string `json:"httpPort"`
30 WebAppDir string `json:"webAppDir"`
31 LogsDir string `json:"logsDir"`
32 XDSAPIKey string `json:"xds-apikey"`
33 ServersConf []XDSServerConf `json:"xdsServers"`
34 SThgConf *SyncThingConf `json:"syncthing"`
37 // readGlobalConfig reads configuration from a config file.
38 // Order to determine which config file is used:
39 // 1/ from command line option: "--config myConfig.json"
40 // 2/ $HOME/.xds/agent/agent-config.json file
41 // 3/ /etc/xds-agent/config.json file
43 func readGlobalConfig(c *Config, confFile string) error {
45 searchIn := make([]string, 0, 3)
47 searchIn = append(searchIn, confFile)
49 if homeDir := common.GetUserHome(); homeDir != "" {
50 searchIn = append(searchIn, path.Join(homeDir, ".xds", "agent", "agent-config.json"))
53 searchIn = append(searchIn, "/etc/xds-agent/agent-config.json")
56 for _, p := range searchIn {
57 if _, err := os.Stat(p); err == nil {
63 c.Log.Infof("No config file found")
67 c.Log.Infof("Use config file: %s", *cFile)
69 // TODO move on viper package to support comments in JSON and also
70 // bind with flags (command line options)
71 // see https://github.com/spf13/viper#working-with-flags
73 fd, _ := os.Open(*cFile)
76 // Decode config file content and save it in a first variable
78 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
82 // Decode config file content and overwrite default settings
84 json.NewDecoder(fd).Decode(&c.FileConf)
86 // Disable Syncthing support when there is no syncthing field in config
87 if fCfg.SThgConf == nil {
88 c.FileConf.SThgConf = nil
91 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
94 &c.FileConf.WebAppDir,
96 if c.FileConf.SThgConf != nil {
97 vars = append(vars, &c.FileConf.SThgConf.Home,
98 &c.FileConf.SThgConf.BinDir)
100 for _, field := range vars {
102 *field, err = common.ResolveEnvVar(*field)