14 type SyncThingConf struct {
15 Home string `json:"home"`
16 GuiAddress string `json:"gui-address"`
17 GuiAPIKey string `json:"gui-apikey"`
20 type FileConfig struct {
21 WebAppDir string `json:"webAppDir"`
22 ShareRootDir string `json:"shareRootDir"`
23 HTTPPort string `json:"httpPort"`
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/config.json file
31 // 3/ <current_dir>/agent-config.json file
32 // 4/ <xds-server executable dir>/config.json file
34 func updateConfigFromFile(c *Config, confFile string) 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", "config.json"))
43 cwd, err := os.Getwd()
45 searchIn = append(searchIn, path.Join(cwd, "config.json"))
47 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
49 searchIn = append(searchIn, path.Join(exePath, "config.json"))
53 for _, p := range searchIn {
54 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)
73 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
78 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
79 // TODO: better to use reflect package to iterate on fields and be more generic
81 if rep, err = resolveEnvVar(fCfg.WebAppDir); err != nil {
84 fCfg.WebAppDir = path.Clean(rep)
86 if rep, err = resolveEnvVar(fCfg.ShareRootDir); err != nil {
89 fCfg.ShareRootDir = path.Clean(rep)
91 if rep, err = resolveEnvVar(fCfg.SThgConf.Home); err != nil {
94 fCfg.SThgConf.Home = path.Clean(rep)
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")
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
124 // resolveEnvVar Resolved environment variable regarding the syntax ${MYVAR}
125 func resolveEnvVar(s string) (string, error) {
126 re := regexp.MustCompile("\\${(.*)}")
127 vars := re.FindAllStringSubmatch(s, -1)
129 for _, v := range vars {
130 val := os.Getenv(v[1])
132 return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
135 rer := regexp.MustCompile("\\${" + v[1] + "}")
136 res = rer.ReplaceAllString(res, val)
142 // exists returns whether the given file or directory exists or not
143 func exists(path string) bool {
144 _, err := os.Stat(path)
148 if os.IsNotExist(err) {