9 "github.com/Sirupsen/logrus"
10 "github.com/codegangsta/cli"
11 common "github.com/iotbzh/xds-common/golib"
14 // Config parameters (json format) of /config command
16 Version string `json:"version"`
17 APIVersion string `json:"apiVersion"`
18 VersionGitTag string `json:"gitTag"`
19 Builder BuilderConfig `json:"builder"`
21 // Private (un-exported fields in REST GET /config route)
22 Options Options `json:"-"`
23 FileConf FileConfig `json:"-"`
24 Log *logrus.Logger `json:"-"`
25 LogVerboseOut io.Writer `json:"-"`
28 // Options set at the command line
36 // Config default values
38 DefaultAPIVersion = "1"
40 DefaultShareDir = "${HOME}/.xds-server/projects"
41 DefaultSdkRootDir = "/xdt/sdk"
44 // Init loads the configuration on start-up
45 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
48 // Define default configuration
50 Version: cliCtx.App.Metadata["version"].(string),
51 APIVersion: DefaultAPIVersion,
52 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
53 Builder: BuilderConfig{},
56 ConfigFile: cliCtx.GlobalString("config"),
57 LogLevel: cliCtx.GlobalString("log"),
58 LogFile: cliCtx.GlobalString("logfile"),
59 NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
62 WebAppDir: "webapp/dist",
63 ShareRootDir: DefaultShareDir,
64 SdkRootDir: DefaultSdkRootDir,
65 HTTPPort: DefaultPort,
71 // config file settings overwrite default config
72 err = readGlobalConfig(&c, c.Options.ConfigFile)
77 // Update location of shared dir if needed
78 if !common.Exists(c.FileConf.ShareRootDir) {
79 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
80 return nil, fmt.Errorf("No valid shared directory found: %v", err)
83 c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
85 // Where Logs are redirected:
86 // default 'stdout' (logfile option default value)
87 // else use file (or filepath) set by --logfile option
88 // that may be overwritten by LogsDir field of config file
89 logF := c.Options.LogFile
90 logD := c.FileConf.LogsDir
93 lf := filepath.Base(logF)
94 if lf == "" || lf == "." {
97 logF = filepath.Join(logD, lf)
99 logD = filepath.Dir(logF)
102 if logD == "" || logD == "." {
103 logD = "/tmp/xds/logs"
105 c.Options.LogFile = logF
106 c.FileConf.LogsDir = logD
108 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
109 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
110 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
113 c.Log.Infoln("Logs file: ", c.Options.LogFile)
114 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)