Merge remote-tracking branch 'origin/wip'
[src/xds/xds-server.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "io"
6         "os"
7
8         "github.com/Sirupsen/logrus"
9         "github.com/codegangsta/cli"
10         common "github.com/iotbzh/xds-common/golib"
11 )
12
13 // Config parameters (json format) of /config command
14 type Config struct {
15         Version       string        `json:"version"`
16         APIVersion    string        `json:"apiVersion"`
17         VersionGitTag string        `json:"gitTag"`
18         Builder       BuilderConfig `json:"builder"`
19
20         // Private (un-exported fields in REST GET /config route)
21         Options       Options        `json:"-"`
22         FileConf      FileConfig     `json:"-"`
23         Log           *logrus.Logger `json:"-"`
24         LogVerboseOut io.Writer      `json:"-"`
25 }
26
27 // Options set at the command line
28 type Options struct {
29         ConfigFile     string
30         LogLevel       string
31         LogFile        string
32         NoFolderConfig bool
33 }
34
35 // Config default values
36 const (
37         DefaultAPIVersion = "1"
38         DefaultPort       = "8000"
39         DefaultShareDir   = "/mnt/share"
40         DefaultSdkRootDir = "/xdt/sdk"
41 )
42
43 // Init loads the configuration on start-up
44 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
45         var err error
46
47         // Define default configuration
48         c := Config{
49                 Version:       cliCtx.App.Metadata["version"].(string),
50                 APIVersion:    DefaultAPIVersion,
51                 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
52                 Builder:       BuilderConfig{},
53
54                 Options: Options{
55                         ConfigFile:     cliCtx.GlobalString("config"),
56                         LogLevel:       cliCtx.GlobalString("log"),
57                         LogFile:        cliCtx.GlobalString("logfile"),
58                         NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
59                 },
60                 FileConf: FileConfig{
61                         WebAppDir:    "webapp/dist",
62                         ShareRootDir: DefaultShareDir,
63                         SdkRootDir:   DefaultSdkRootDir,
64                         HTTPPort:     DefaultPort,
65                 },
66                 Log: log,
67         }
68
69         // config file settings overwrite default config
70         err = readGlobalConfig(&c, c.Options.ConfigFile)
71         if err != nil {
72                 return nil, err
73         }
74
75         // Update location of shared dir if needed
76         if !common.Exists(c.FileConf.ShareRootDir) {
77                 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
78                         return nil, fmt.Errorf("No valid shared directory found: %v", err)
79                 }
80         }
81         c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
82
83         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
84                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
85                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
86                 }
87         }
88         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
89
90         return &c, nil
91 }