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.
25 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
28 type SyncThingConf struct {
29 BinDir string `json:"binDir"`
30 Home string `json:"home"`
31 GuiAddress string `json:"gui-address"`
32 GuiAPIKey string `json:"gui-apikey"`
35 type XDSServerConf struct {
36 URL string `json:"url"`
37 ConnRetry int `json:"connRetry"`
39 // private/not exported fields
41 URLIndex string `json:"-"`
42 APIBaseURL string `json:"-"`
43 APIPartialURL string `json:"-"`
46 type XDSBinderConf struct {
47 URL string `json:"url"`
48 ConnRetry int `json:"connRetry"`
51 type ProfileConfT struct {
52 XDSBinder XDSBinderConf `json:"xdsBinder"`
55 type FileConfig struct {
56 HTTPPort string `json:"httpPort"`
57 WebAppDir string `json:"webAppDir"`
58 LogsDir string `json:"logsDir"`
59 XDSAPIKey string `json:"xds-apikey"`
60 ServersConf []XDSServerConf `json:"xdsServers"`
61 SThgConf *SyncThingConf `json:"syncthing"`
62 ProfileConf ProfileConfT `json:"profileConf"`
65 // readGlobalConfig reads configuration from a config file.
66 // Order to determine which config file is used:
67 // 1/ from command line option: "--config myConfig.json"
68 // 2/ $HOME/.xds/agent/agent-config.json file
69 // 3/ /etc/xds/agent/agent-config.json file
71 func readGlobalConfig(c *Config, confFile string) error {
74 var fCfg = FileConfig{}
76 searchIn := make([]string, 0, 3)
78 searchIn = append(searchIn, confFile)
80 if homeDir := common.GetUserHome(); homeDir != "" {
81 searchIn = append(searchIn, path.Join(homeDir, ".xds", "agent", "agent-config.json"))
84 searchIn = append(searchIn, "/etc/xds/agent/agent-config.json")
87 for _, p := range searchIn {
88 if _, err := os.Stat(p); err == nil {
94 c.Log.Infof("No config file found")
95 // always resolved env vars even if no config file found!
99 c.Log.Infof("Use config file: %s", *cFile)
101 // TODO move on viper package to support comments in JSON and also
102 // bind with flags (command line options)
103 // see https://github.com/spf13/viper#working-with-flags
105 fd, _ = os.Open(*cFile)
112 // Decode config file content and save it in a first variable
113 if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
117 // Decode config file content and overwrite default settings
119 json.NewDecoder(fd).Decode(&c.FileConf)
121 // Disable Syncthing support when there is no syncthing field in config
122 if fCfg.SThgConf == nil {
123 c.FileConf.SThgConf = nil
126 // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
130 &c.FileConf.WebAppDir,
132 if c.FileConf.SThgConf != nil {
133 vars = append(vars, &c.FileConf.SThgConf.Home,
134 &c.FileConf.SThgConf.BinDir)
136 for _, field := range vars {
138 *field, err = common.ResolveEnvVar(*field)