11 "github.com/iotbzh/xds-server/lib/common"
14 type SyncThingConf struct {
15 BinDir string `json:"binDir"`
16 Home string `json:"home"`
17 GuiAddress string `json:"gui-address"`
18 GuiAPIKey string `json:"gui-apikey"`
19 RescanIntervalS int `json:"rescanIntervalS"`
22 type FileConfig struct {
23 WebAppDir string `json:"webAppDir"`
24 ShareRootDir string `json:"shareRootDir"`
25 SdkRootDir string `json:"sdkRootDir"`
26 HTTPPort string `json:"httpPort"`
27 SThgConf *SyncThingConf `json:"syncthing"`
28 LogsDir string `json:"logsDir"`
31 // getConfigFromFile reads configuration from a config file.
32 // Order to determine which config file is used:
33 // 1/ from command line option: "--config myConfig.json"
34 // 2/ $HOME/.xds/config.json file
35 // 3/ <current_dir>/config.json file
36 // 4/ <xds-server executable dir>/config.json file
38 func updateConfigFromFile(c *Config, confFile string) error {
40 searchIn := make([]string, 0, 3)
42 searchIn = append(searchIn, confFile)
44 if usr, err := user.Current(); err == nil {
45 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "config.json"))
47 cwd, err := os.Getwd()
49 searchIn = append(searchIn, path.Join(cwd, "config.json"))
51 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
53 searchIn = append(searchIn, path.Join(exePath, "config.json"))
57 for _, p := range searchIn {
58 if _, err := os.Stat(p); err == nil {
64 // No config file found
68 c.Log.Infof("Use config file: %s", *cFile)
70 // TODO move on viper package to support comments in JSON and also
71 // bind with flags (command line options)
72 // see https://github.com/spf13/viper#working-with-flags
74 fd, _ := os.Open(*cFile)
77 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
82 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
83 for _, field := range []*string{
89 &fCfg.SThgConf.BinDir} {
92 if *field, err = common.ResolveEnvVar(*field); err != nil {
97 // Config file settings overwrite default config
99 if fCfg.WebAppDir != "" {
100 c.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
102 // Is it a full path ?
103 if !strings.HasPrefix(c.WebAppDir, "/") && exePath != "" {
104 // Check first from current directory
105 for _, rootD := range []string{cwd, exePath} {
106 ff := path.Join(rootD, c.WebAppDir, "index.html")
107 if common.Exists(ff) {
108 c.WebAppDir = path.Join(rootD, c.WebAppDir)
114 if fCfg.ShareRootDir != "" {
115 c.ShareRootDir = fCfg.ShareRootDir
118 if fCfg.HTTPPort != "" {
119 c.HTTPPort = fCfg.HTTPPort