10 "github.com/Sirupsen/logrus"
11 "github.com/urfave/cli"
12 common "github.com/iotbzh/xds-common/golib"
13 uuid "github.com/satori/go.uuid"
16 // Config parameters (json format) of /config command
25 LogVerboseOut io.Writer
28 // Options set at the command line
35 // Config default values
37 DefaultAPIVersion = "1"
38 DefaultLogLevel = "error"
41 // Init loads the configuration on start-up
42 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
45 defaultWebAppDir := "${EXEPATH}/www"
46 defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
48 // TODO: allocate uuid only the first time and save+reuse it later
49 uuid := uuid.NewV1().String()
51 // Define default configuration
54 Version: ctx.App.Metadata["version"].(string),
55 APIVersion: DefaultAPIVersion,
56 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
59 ConfigFile: ctx.GlobalString("config"),
60 LogLevel: ctx.GlobalString("log"),
61 LogFile: ctx.GlobalString("logfile"),
66 WebAppDir: defaultWebAppDir,
68 ServersConf: []XDSServerConf{
70 URL: "http://localhost:8000",
74 SThgConf: &SyncThingConf{
75 Home: defaultSTHomeDir,
81 c.Log.Infoln("Agent UUID: ", uuid)
83 // config file settings overwrite default config
84 err = readGlobalConfig(&c, c.Options.ConfigFile)
89 // Handle where Logs are redirected:
90 // default 'stdout' (logfile option default value)
91 // else use file (or filepath) set by --logfile option
92 // that may be overwritten by LogsDir field of config file
93 logF := c.Options.LogFile
94 logD := c.FileConf.LogsDir
97 lf := filepath.Base(logF)
98 if lf == "" || lf == "." {
101 logF = filepath.Join(logD, lf)
103 logD = filepath.Dir(logF)
106 if logD == "" || logD == "." {
107 logD = "/tmp/xds/logs"
109 c.Options.LogFile = logF
110 c.FileConf.LogsDir = logD
112 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
113 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
114 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
118 c.Log.Infoln("Logs file: ", c.Options.LogFile)
119 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)