Add logsDir setting and more
[src/xds/xds-agent.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5
6         "os"
7
8         "github.com/Sirupsen/logrus"
9         "github.com/codegangsta/cli"
10         "github.com/iotbzh/xds-server/lib/common"
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
19         // Private / un-exported fields
20         HTTPPort string         `json:"-"`
21         FileConf *FileConfig    `json:"-"`
22         Log      *logrus.Logger `json:"-"`
23 }
24
25 // Config default values
26 const (
27         DefaultAPIVersion = "1"
28         DefaultPort       = "8010"
29         DefaultLogLevel   = "error"
30 )
31
32 // Init loads the configuration on start-up
33 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
34         var err error
35
36         // Define default configuration
37         c := Config{
38                 Version:       ctx.App.Metadata["version"].(string),
39                 APIVersion:    DefaultAPIVersion,
40                 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
41
42                 HTTPPort: DefaultPort,
43                 Log:      log,
44         }
45
46         // config file settings overwrite default config
47         c.FileConf, err = updateConfigFromFile(&c, ctx.GlobalString("config"))
48         if err != nil {
49                 return nil, err
50         }
51
52         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
53                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
54                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
55                 }
56         }
57         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
58
59         return &c, nil
60 }
61
62 // UpdateAll Update the current configuration
63 func (c *Config) UpdateAll(newCfg Config) error {
64         return fmt.Errorf("Not Supported")
65 }