11 common "github.com/iotbzh/xds-common/golib"
15 // ConfigDir Directory in user HOME directory where xds config will be saved
17 // GlobalConfigFilename Global config filename
18 GlobalConfigFilename = "config.json"
19 // FoldersConfigFilename Folders config filename
20 FoldersConfigFilename = "server-config_folders.xml"
23 // SyncThingConf definition
24 type SyncThingConf struct {
25 BinDir string `json:"binDir"`
26 Home string `json:"home"`
27 GuiAddress string `json:"gui-address"`
28 GuiAPIKey string `json:"gui-apikey"`
29 RescanIntervalS int `json:"rescanIntervalS"`
32 // FileConfig is the JSON structure of xds-server config file (config.json)
33 type FileConfig struct {
34 WebAppDir string `json:"webAppDir"`
35 ShareRootDir string `json:"shareRootDir"`
36 SdkRootDir string `json:"sdkRootDir"`
37 HTTPPort string `json:"httpPort"`
38 SThgConf *SyncThingConf `json:"syncthing"`
39 LogsDir string `json:"logsDir"`
42 // readGlobalConfig reads configuration from a config file.
43 // Order to determine which config file is used:
44 // 1/ from command line option: "--config myConfig.json"
45 // 2/ $HOME/.xds/config.json file
46 // 3/ <current_dir>/config.json file
47 // 4/ <xds-server executable dir>/config.json file
48 func readGlobalConfig(c *Config, confFile string) error {
50 searchIn := make([]string, 0, 3)
52 searchIn = append(searchIn, confFile)
54 if usr, err := user.Current(); err == nil {
55 searchIn = append(searchIn, path.Join(usr.HomeDir, ConfigDir,
56 GlobalConfigFilename))
58 cwd, err := os.Getwd()
60 searchIn = append(searchIn, path.Join(cwd, "config.json"))
62 exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
64 searchIn = append(searchIn, path.Join(exePath, "config.json"))
68 for _, p := range searchIn {
69 if _, err := os.Stat(p); err == nil {
75 // No config file found
79 c.Log.Infof("Use config file: %s", *cFile)
81 // TODO move on viper package to support comments in JSON and also
82 // bind with flags (command line options)
83 // see https://github.com/spf13/viper#working-with-flags
84 fd, _ := os.Open(*cFile)
87 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
91 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
97 if fCfg.SThgConf != nil {
98 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
100 for _, field := range vars {
102 if *field, err = common.ResolveEnvVar(*field); err != nil {
107 // Use config file settings else use default config
108 if fCfg.WebAppDir == "" {
109 fCfg.WebAppDir = c.FileConf.WebAppDir
111 if fCfg.ShareRootDir == "" {
112 fCfg.ShareRootDir = c.FileConf.ShareRootDir
114 if fCfg.SdkRootDir == "" {
115 fCfg.SdkRootDir = c.FileConf.SdkRootDir
117 if fCfg.HTTPPort == "" {
118 fCfg.HTTPPort = c.FileConf.HTTPPort
121 // Resolve webapp dir (support relative or full path)
122 fCfg.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
123 if !strings.HasPrefix(fCfg.WebAppDir, "/") && exePath != "" {
124 // Check first from current directory
125 for _, rootD := range []string{cwd, exePath} {
126 ff := path.Join(rootD, fCfg.WebAppDir, "index.html")
127 if common.Exists(ff) {
128 fCfg.WebAppDir = path.Join(rootD, fCfg.WebAppDir)
138 // FoldersConfigFilenameGet
139 func FoldersConfigFilenameGet() (string, error) {
140 usr, err := user.Current()
144 return path.Join(usr.HomeDir, ConfigDir, FoldersConfigFilename), nil