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/ <xds-server executable dir>/config.json file
33 func updateConfigFromFile(c *Config, confFile string) error {
35 searchIn := make([]string, 0, 3)
37 searchIn = append(searchIn, confFile)
39 if usr, err := user.Current(); err == nil {
40 searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "config.json"))
42 cwd, err := os.Getwd()
44 searchIn = append(searchIn, path.Join(cwd, "config.json"))
46 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
48 searchIn = append(searchIn, path.Join(exePath, "config.json"))
52 for _, p := range searchIn {
53 if _, err := os.Stat(p); err == nil {
59 // No config file found
63 c.Log.Infof("Use config file: %s", *cFile)
65 // TODO move on viper package to support comments in JSON and also
66 // bind with flags (command line options)
67 // see https://github.com/spf13/viper#working-with-flags
69 fd, _ := os.Open(*cFile)
72 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
77 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
78 // TODO: better to use reflect package to iterate on fields and be more generic
80 if rep, err = resolveEnvVar(fCfg.WebAppDir); err != nil {
83 fCfg.WebAppDir = path.Clean(rep)
85 if rep, err = resolveEnvVar(fCfg.ShareRootDir); err != nil {
88 fCfg.ShareRootDir = path.Clean(rep)
90 if rep, err = resolveEnvVar(fCfg.SThgConf.Home); err != nil {
93 fCfg.SThgConf.Home = path.Clean(rep)
95 // Config file settings overwrite default config
97 if fCfg.WebAppDir != "" {
98 c.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
100 // Is it a full path ?
101 if !strings.HasPrefix(c.WebAppDir, "/") && exePath != "" {
102 // Check first from current directory
103 for _, rootD := range []string{cwd, exePath} {
104 ff := path.Join(rootD, c.WebAppDir, "index.html")
106 c.WebAppDir = path.Join(rootD, c.WebAppDir)
112 if fCfg.ShareRootDir != "" {
113 c.ShareRootDir = fCfg.ShareRootDir
116 if fCfg.HTTPPort != "" {
117 c.HTTPPort = fCfg.HTTPPort
123 // resolveEnvVar Resolved environment variable regarding the syntax ${MYVAR}
124 func resolveEnvVar(s string) (string, error) {
125 re := regexp.MustCompile("\\${(.*)}")
126 vars := re.FindAllStringSubmatch(s, -1)
128 for _, v := range vars {
129 val := os.Getenv(v[1])
131 return res, fmt.Errorf("ERROR: %s env variable not defined", v[1])
134 rer := regexp.MustCompile("\\${" + v[1] + "}")
135 res = rer.ReplaceAllString(res, val)
141 // exists returns whether the given file or directory exists or not
142 func exists(path string) bool {
143 _, err := os.Stat(path)
147 if os.IsNotExist(err) {