10 "github.com/Sirupsen/logrus"
11 "github.com/codegangsta/cli"
12 common "github.com/iotbzh/xds-common/golib"
15 // Config parameters (json format) of /config command
23 LogVerboseOut io.Writer
26 // Options set at the command line
33 // Config default values
35 DefaultAPIVersion = "1"
36 DefaultLogLevel = "error"
39 // Init loads the configuration on start-up
40 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
43 defaultWebAppDir := "${EXEPATH}/www"
44 defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
46 // Define default configuration
48 Version: ctx.App.Metadata["version"].(string),
49 APIVersion: DefaultAPIVersion,
50 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
53 ConfigFile: ctx.GlobalString("config"),
54 LogLevel: ctx.GlobalString("log"),
55 LogFile: ctx.GlobalString("logfile"),
60 WebAppDir: defaultWebAppDir,
62 // SEB XDSAPIKey: "1234abcezam",
63 ServersConf: []XDSServerConf{
65 URL: "http://localhost:8000",
69 SThgConf: &SyncThingConf{
70 Home: defaultSTHomeDir,
76 // config file settings overwrite default config
77 err = readGlobalConfig(&c, c.Options.ConfigFile)
82 // Handle where Logs are redirected:
83 // default 'stdout' (logfile option default value)
84 // else use file (or filepath) set by --logfile option
85 // that may be overwritten by LogsDir field of config file
86 logF := c.Options.LogFile
87 logD := c.FileConf.LogsDir
90 lf := filepath.Base(logF)
91 if lf == "" || lf == "." {
94 logF = filepath.Join(logD, lf)
96 logD = filepath.Dir(logF)
99 if logD == "" || logD == "." {
100 logD = "/tmp/xds/logs"
102 c.Options.LogFile = logF
103 c.FileConf.LogsDir = logD
105 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
106 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
107 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
110 c.Log.Infoln("Logs file: ", c.Options.LogFile)
111 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)