Initial commit.
[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 )
11
12 // Config parameters (json format) of /config command
13 type Config struct {
14         Version       string `json:"version"`
15         APIVersion    string `json:"apiVersion"`
16         VersionGitTag string `json:"gitTag"`
17
18         // Private / un-exported fields
19         HTTPPort string `json:"-"`
20         FileConf *FileConfig
21         log      *logrus.Logger
22 }
23
24 // Config default values
25 const (
26         DefaultAPIVersion = "1"
27         DefaultPort       = "8010"
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: DefaultPort,
42                 log:      log,
43         }
44
45         // config file settings overwrite default config
46         c.FileConf, err = updateConfigFromFile(&c, ctx.GlobalString("config"))
47         if err != nil {
48                 return Config{}, err
49         }
50
51         return c, nil
52 }
53
54 // UpdateAll Update the current configuration
55 func (c *Config) UpdateAll(newCfg Config) error {
56         return fmt.Errorf("Not Supported")
57 }
58
59 func dirExists(path string) bool {
60         _, err := os.Stat(path)
61         if os.IsNotExist(err) {
62                 return false
63         }
64         return true
65 }