854d383ff1b5fa19d4c45ebb3ce3b6e3374586e7
[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         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
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         DefaultLogLevel   = "error"
29 )
30
31 // Init loads the configuration on start-up
32 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
33         var err error
34
35         // Define default configuration
36         c := Config{
37                 Version:       ctx.App.Metadata["version"].(string),
38                 APIVersion:    DefaultAPIVersion,
39                 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
40
41                 HTTPPort: "8010",
42                 FileConf: &FileConfig{
43                         LogsDir: "/tmp/logs",
44                         SThgConf: &SyncThingConf{
45                                 Home: "${HOME}/.xds/agent/syncthing-config",
46                         },
47                 },
48                 Log: log,
49         }
50
51         // config file settings overwrite default config
52         c.FileConf, err = updateConfigFromFile(&c, ctx.GlobalString("config"))
53         if err != nil {
54                 return nil, err
55         }
56
57         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
58                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
59                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
60                 }
61         }
62         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
63
64         return &c, nil
65 }
66
67 // UpdateAll Update the current configuration
68 func (c *Config) UpdateAll(newCfg Config) error {
69         return fmt.Errorf("Not Supported")
70 }