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.
27 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
28 "github.com/Sirupsen/logrus"
29 uuid "github.com/satori/go.uuid"
30 "github.com/urfave/cli"
33 // Config parameters (json format) of /config command
42 LogVerboseOut io.Writer
45 // Options set at the command line
52 // Config default values
54 DefaultAPIVersion = "1"
55 DefaultLogLevel = "error"
58 // Init loads the configuration on start-up
59 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
62 defaultWebAppDir := "${EXEPATH}/www"
63 defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
65 // TODO: allocate uuid only the first time and save+reuse it later
66 uuid := uuid.NewV1().String()
68 // Define default configuration
71 Version: ctx.App.Metadata["version"].(string),
72 APIVersion: DefaultAPIVersion,
73 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
76 ConfigFile: ctx.GlobalString("config"),
77 LogLevel: ctx.GlobalString("log"),
78 LogFile: ctx.GlobalString("logfile"),
83 WebAppDir: defaultWebAppDir,
85 ServersConf: []XDSServerConf{
87 URL: "http://localhost:8000",
91 SThgConf: &SyncThingConf{
92 Home: defaultSTHomeDir,
94 ProfileConf: ProfileConfT{
95 XDSBinder: XDSBinderConf{
96 URL: "http://localhost:8810",
104 c.Log.Infoln("Agent UUID: ", uuid)
106 // config file settings overwrite default config
107 err = readGlobalConfig(&c, c.Options.ConfigFile)
112 // Handle where Logs are redirected:
113 // default 'stdout' (logfile option default value)
114 // else use file (or filepath) set by --logfile option
115 // else use LogsDir field of config file
116 logF := c.Options.LogFile
117 logD := c.FileConf.LogsDir
118 if logF != "stdout" {
120 if common.IsDir(logF) {
122 logF = filepath.Join(logF, "xds-agent.log")
124 logD = filepath.Dir(logF)
126 } else if logD != "" {
127 lf := filepath.Base(logF)
128 if lf == "" || lf == "." {
131 logF = filepath.Join(logD, lf)
133 logD = filepath.Dir(logF)
136 if logD == "" || logD == "." {
137 logD = "/tmp/xds/logs"
139 c.Options.LogFile = logF
140 c.FileConf.LogsDir = logD
142 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
143 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
144 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
148 c.Log.Infoln("Logs file: ", c.Options.LogFile)
149 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)