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"`
21 type FileConfig struct {
22 WebAppDir string `json:"webAppDir"`
23 ShareRootDir string `json:"shareRootDir"`
24 SdkRootDir string `json:"sdkRootDir"`
25 HTTPPort string `json:"httpPort"`
26 SThgConf *SyncThingConf `json:"syncthing"`
27 LogsDir string `json:"logsDir"`
30 // getConfigFromFile reads configuration from a config file.
31 // Order to determine which config file is used:
32 // 1/ from command line option: "--config myConfig.json"
33 // 2/ $HOME/.xds/config.json file
34 // 3/ <current_dir>/config.json file
35 // 4/ <xds-server executable dir>/config.json file
37 func updateConfigFromFile(c *Config, confFile string) error {
39 searchIn := make([]string, 0, 3)
41 searchIn = append(searchIn, confFile)
43 if usr, err := user.Current(); err == nil {
44 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "config.json"))
46 cwd, err := os.Getwd()
48 searchIn = append(searchIn, path.Join(cwd, "config.json"))
50 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
52 searchIn = append(searchIn, path.Join(exePath, "config.json"))
56 for _, p := range searchIn {
57 if _, err := os.Stat(p); err == nil {
63 // 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 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
81 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
82 for _, field := range []*string{
88 &fCfg.SThgConf.BinDir} {
91 if *field, err = common.ResolveEnvVar(*field); err != nil {
96 // Config file settings overwrite default config
98 if fCfg.WebAppDir != "" {
99 c.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
101 // Is it a full path ?
102 if !strings.HasPrefix(c.WebAppDir, "/") && exePath != "" {
103 // Check first from current directory
104 for _, rootD := range []string{cwd, exePath} {
105 ff := path.Join(rootD, c.WebAppDir, "index.html")
106 if common.Exists(ff) {
107 c.WebAppDir = path.Join(rootD, c.WebAppDir)
113 if fCfg.ShareRootDir != "" {
114 c.ShareRootDir = fCfg.ShareRootDir
117 if fCfg.HTTPPort != "" {
118 c.HTTPPort = fCfg.HTTPPort