10 "github.com/Sirupsen/logrus"
11 "github.com/codegangsta/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 // config file settings overwrite default config
82 err = readGlobalConfig(&c, c.Options.ConfigFile)
87 // Handle where Logs are redirected:
88 // default 'stdout' (logfile option default value)
89 // else use file (or filepath) set by --logfile option
90 // that may be overwritten by LogsDir field of config file
91 logF := c.Options.LogFile
92 logD := c.FileConf.LogsDir
95 lf := filepath.Base(logF)
96 if lf == "" || lf == "." {
99 logF = filepath.Join(logD, lf)
101 logD = filepath.Dir(logF)
104 if logD == "" || logD == "." {
105 logD = "/tmp/xds/logs"
107 c.Options.LogFile = logF
108 c.FileConf.LogsDir = logD
110 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
111 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
112 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
116 c.Log.Infoln("Agent UUID: ", uuid)
117 c.Log.Infoln("Logs file: ", c.Options.LogFile)
118 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)