Fixed go linter warnings.
[src/xds/xds-server.git] / lib / xdsconfig / fileconfig.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package xdsconfig
19
20 import (
21         "encoding/json"
22         "os"
23         "os/user"
24         "path"
25         "path/filepath"
26         "strings"
27
28         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
29 )
30
31 // ConfigDir Directory in user HOME directory where xds config will be saved
32 var ConfigDir = path.Join(".xds", "server")
33
34 const (
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"
41 )
42
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"`
50 }
51
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         HTTPPort      string         `json:"httpPort"`
58         SThgConf      *SyncThingConf `json:"syncthing"`
59         LogsDir       string         `json:"logsDir"`
60 }
61
62 // readGlobalConfig reads configuration from a config file.
63 // Order to determine which config file is used:
64 //  1/ from command line option: "--config myConfig.json"
65 //  2/ $HOME/.xds/server/server-config.json file
66 //  3/ /etc/xds/server/server-config.json file
67 //  4/ <xds-server executable dir>/server-config.json file
68 func readGlobalConfig(c *Config, confFile string) error {
69
70         searchIn := make([]string, 0, 3)
71         if confFile != "" {
72                 searchIn = append(searchIn, confFile)
73         }
74         if usr, err := user.Current(); err == nil {
75                 searchIn = append(searchIn, path.Join(usr.HomeDir, ConfigDir, GlobalConfigFilename))
76         }
77
78         searchIn = append(searchIn, "/etc/xds/server/server-config.json")
79
80         exePath := os.Args[0]
81         ee, _ := os.Executable()
82         exeAbsPath, err := filepath.Abs(ee)
83         if err == nil {
84                 exePath, err = filepath.EvalSymlinks(exeAbsPath)
85                 if err == nil {
86                         exePath = filepath.Dir(ee)
87                 } else {
88                         exePath = filepath.Dir(exeAbsPath)
89                 }
90         }
91         searchIn = append(searchIn, path.Join(exePath, "server-config.json"))
92
93         var cFile *string
94         for _, p := range searchIn {
95                 if _, err := os.Stat(p); err == nil {
96                         cFile = &p
97                         break
98                 }
99         }
100         if cFile == nil {
101                 // No config file found
102                 return nil
103         }
104         c.Log.Infof("Use config file:       %s", *cFile)
105
106         // TODO move on viper package to support comments in JSON and also
107         // bind with flags (command line options)
108         // see https://github.com/spf13/viper#working-with-flags
109         fd, _ := os.Open(*cFile)
110         defer fd.Close()
111         fCfg := FileConfig{}
112         if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
113                 return err
114         }
115
116         // Support environment variables (IOW ${MY_ENV_VAR} syntax) in server-config.json
117         vars := []*string{
118                 &fCfg.WebAppDir,
119                 &fCfg.ShareRootDir,
120                 &fCfg.SdkScriptsDir,
121                 &fCfg.LogsDir}
122         if fCfg.SThgConf != nil {
123                 vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
124         }
125         for _, field := range vars {
126                 var err error
127                 if *field, err = common.ResolveEnvVar(*field); err != nil {
128                         return err
129                 }
130         }
131
132         // Use config file settings else use default config
133         if fCfg.WebAppDir == "" {
134                 fCfg.WebAppDir = c.FileConf.WebAppDir
135         }
136         if fCfg.ShareRootDir == "" {
137                 fCfg.ShareRootDir = c.FileConf.ShareRootDir
138         }
139         if fCfg.SdkScriptsDir == "" {
140                 fCfg.SdkScriptsDir = c.FileConf.SdkScriptsDir
141         }
142         if fCfg.HTTPPort == "" {
143                 fCfg.HTTPPort = c.FileConf.HTTPPort
144         }
145         if fCfg.LogsDir == "" {
146                 fCfg.LogsDir = c.FileConf.LogsDir
147         }
148
149         // Resolve webapp dir (support relative or full path)
150         fCfg.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
151         if !strings.HasPrefix(fCfg.WebAppDir, "/") && exePath != "" {
152                 cwd, _ := os.Getwd()
153
154                 // Check first from current directory
155                 for _, rootD := range []string{exePath, cwd} {
156                         ff := path.Join(rootD, fCfg.WebAppDir, "index.html")
157                         if common.Exists(ff) {
158                                 fCfg.WebAppDir = path.Join(rootD, fCfg.WebAppDir)
159                                 break
160                         }
161                 }
162         }
163
164         c.FileConf = fCfg
165         return nil
166 }
167
168 func configFilenameGet(cfgFile string) (string, error) {
169         usr, err := user.Current()
170         if err != nil {
171                 return "", err
172         }
173         return path.Join(usr.HomeDir, ConfigDir, cfgFile), nil
174 }
175
176 // FoldersConfigFilenameGet Return the FoldersConfig filename
177 func FoldersConfigFilenameGet() (string, error) {
178         return configFilenameGet(FoldersConfigFilename)
179 }
180
181 // ServerDataFilenameGet Return the ServerData filename
182 func ServerDataFilenameGet() (string, error) {
183         return configFilenameGet(ServerDataFilename)
184 }