7 "github.com/Sirupsen/logrus"
8 "github.com/codegangsta/cli"
9 common "github.com/iotbzh/xds-common/golib"
12 // Config parameters (json format) of /config command
14 Version string `json:"version"`
15 APIVersion string `json:"apiVersion"`
16 VersionGitTag string `json:"gitTag"`
17 Builder BuilderConfig `json:"builder"`
19 // Private (un-exported fields in REST GET /config route)
20 Options Options `json:"-"`
21 FileConf FileConfig `json:"-"`
22 Log *logrus.Logger `json:"-"`
25 // Options set at the command line
33 // Config default values
35 DefaultAPIVersion = "1"
37 DefaultShareDir = "/mnt/share"
38 DefaultSdkRootDir = "/xdt/sdk"
41 // Init loads the configuration on start-up
42 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
45 // Define default configuration
47 Version: cliCtx.App.Metadata["version"].(string),
48 APIVersion: DefaultAPIVersion,
49 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
50 Builder: BuilderConfig{},
53 ConfigFile: cliCtx.GlobalString("config"),
54 LogLevel: cliCtx.GlobalString("log"),
55 LogFile: cliCtx.GlobalString("logfile"),
56 NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
59 WebAppDir: "webapp/dist",
60 ShareRootDir: DefaultShareDir,
61 SdkRootDir: DefaultSdkRootDir,
62 HTTPPort: DefaultPort,
67 // config file settings overwrite default config
68 err = readGlobalConfig(&c, c.Options.ConfigFile)
73 // Update location of shared dir if needed
74 if !common.Exists(c.FileConf.ShareRootDir) {
75 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
76 return nil, fmt.Errorf("No valid shared directory found: %v", err)
79 c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
81 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
82 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
83 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
86 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)