2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
31 // ConfigDir Directory in user HOME directory where xds config will be saved
32 var ConfigDir = path.Join(".xds", "server")
35 // GlobalConfigFilename Global config filename
36 GlobalConfigFilename = "server-config.json"
37 // ServerDataFilename Server data filename
38 ServerDataFilename = "server-data.xml"
39 // FoldersConfigFilename Folders config filename
40 FoldersConfigFilename = "server-config_folders.xml"
43 // SyncThingConf definition
44 type SyncThingConf struct {
45 BinDir string `json:"binDir"`
46 Home string `json:"home"`
47 GuiAddress string `json:"gui-address"`
48 GuiAPIKey string `json:"gui-apikey"`
49 RescanIntervalS int `json:"rescanIntervalS"`
52 // FileConfig is the JSON structure of xds-server config file (server-config.json)
53 type FileConfig struct {
54 WebAppDir string `json:"webAppDir"`
55 ShareRootDir string `json:"shareRootDir"`
56 SdkScriptsDir string `json:"sdkScriptsDir"`
57 SdkDbUpdate string `json:"sdkDbUpdate"`
58 HTTPPort string `json:"httpPort"`
59 SThgConf *SyncThingConf `json:"syncthing"`
60 LogsDir string `json:"logsDir"`
63 // readGlobalConfig reads configuration from a config file.
64 // Order to determine which config file is used:
65 // 1/ from command line option: "--config myConfig.json"
66 // 2/ $HOME/.xds/server/server-config.json file
67 // 3/ /etc/xds/server/server-config.json file
68 // 4/ <xds-server executable dir>/server-config.json file
69 func readGlobalConfig(c *Config, confFile string) error {
71 searchIn := make([]string, 0, 3)
73 searchIn = append(searchIn, confFile)
75 if usr, err := user.Current(); err == nil {
76 searchIn = append(searchIn, path.Join(usr.HomeDir, ConfigDir, GlobalConfigFilename))
79 searchIn = append(searchIn, "/etc/xds/server/server-config.json")
82 ee, _ := os.Executable()
83 exeAbsPath, err := filepath.Abs(ee)
85 exePath, err = filepath.EvalSymlinks(exeAbsPath)
87 exePath = filepath.Dir(ee)
89 exePath = filepath.Dir(exeAbsPath)
92 searchIn = append(searchIn, path.Join(exePath, "server-config.json"))
95 for _, p := range searchIn {
96 if _, err := os.Stat(p); err == nil {
102 // No config file found
105 c.Log.Infof("Use config file: %s", *cFile)
107 // TODO move on viper package to support comments in JSON and also
108 // bind with flags (command line options)
109 // see https://github.com/spf13/viper#working-with-flags
110 fd, _ := os.Open(*cFile)
113 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
117 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in server-config.json
123 if fCfg.SThgConf != nil {
124 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
126 for _, field := range vars {
128 if *field, err = common.ResolveEnvVar(*field); err != nil {
133 // Use config file settings else use default config
134 if fCfg.WebAppDir == "" {
135 fCfg.WebAppDir = c.FileConf.WebAppDir
137 if fCfg.ShareRootDir == "" {
138 fCfg.ShareRootDir = c.FileConf.ShareRootDir
140 if fCfg.SdkScriptsDir == "" {
141 fCfg.SdkScriptsDir = c.FileConf.SdkScriptsDir
143 if fCfg.SdkDbUpdate == "" {
144 fCfg.SdkDbUpdate = c.FileConf.SdkDbUpdate
146 if fCfg.HTTPPort == "" {
147 fCfg.HTTPPort = c.FileConf.HTTPPort
149 if fCfg.LogsDir == "" {
150 fCfg.LogsDir = c.FileConf.LogsDir
153 // Resolve webapp dir (support relative or full path)
154 fCfg.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
155 if !strings.HasPrefix(fCfg.WebAppDir, "/") && exePath != "" {
158 // Check first from current directory
159 for _, rootD := range []string{exePath, cwd} {
160 ff := path.Join(rootD, fCfg.WebAppDir, "index.html")
161 if common.Exists(ff) {
162 fCfg.WebAppDir = path.Join(rootD, fCfg.WebAppDir)
172 func configFilenameGet(cfgFile string) (string, error) {
173 usr, err := user.Current()
177 return path.Join(usr.HomeDir, ConfigDir, cfgFile), nil
180 // FoldersConfigFilenameGet Return the FoldersConfig filename
181 func FoldersConfigFilenameGet() (string, error) {
182 return configFilenameGet(FoldersConfigFilename)
185 // ServerDataFilenameGet Return the ServerData filename
186 func ServerDataFilenameGet() (string, error) {
187 return configFilenameGet(ServerDataFilename)