9cff8620cb915bd4d161af8ed4f90b00a1cb705a
[src/xds/xds-agent.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "io"
6         "path/filepath"
7
8         "os"
9
10         "github.com/Sirupsen/logrus"
11         "github.com/codegangsta/cli"
12         common "github.com/iotbzh/xds-common/golib"
13 )
14
15 // Config parameters (json format) of /config command
16 type Config struct {
17         Version       string
18         APIVersion    string
19         VersionGitTag string
20         Options       Options
21         FileConf      FileConfig
22         Log           *logrus.Logger
23         LogVerboseOut io.Writer
24 }
25
26 // Options set at the command line
27 type Options struct {
28         ConfigFile string
29         LogLevel   string
30         LogFile    string
31 }
32
33 // Config default values
34 const (
35         DefaultAPIVersion = "1"
36         DefaultLogLevel   = "error"
37 )
38
39 // Init loads the configuration on start-up
40 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
41         var err error
42
43         defaultWebAppDir := "${EXEPATH}/www"
44         defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
45
46         // Define default configuration
47         c := Config{
48                 Version:       ctx.App.Metadata["version"].(string),
49                 APIVersion:    DefaultAPIVersion,
50                 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
51
52                 Options: Options{
53                         ConfigFile: ctx.GlobalString("config"),
54                         LogLevel:   ctx.GlobalString("log"),
55                         LogFile:    ctx.GlobalString("logfile"),
56                 },
57
58                 FileConf: FileConfig{
59                         HTTPPort:  "8800",
60                         WebAppDir: defaultWebAppDir,
61                         LogsDir:   "/tmp/logs",
62                         // SEB XDSAPIKey: "1234abcezam",
63                         ServersConf: []XDSServerConf{
64                                 XDSServerConf{
65                                         URL:       "http://localhost:8000",
66                                         ConnRetry: 10,
67                                 },
68                         },
69                         SThgConf: &SyncThingConf{
70                                 Home: defaultSTHomeDir,
71                         },
72                 },
73                 Log: log,
74         }
75
76         // config file settings overwrite default config
77         err = readGlobalConfig(&c, c.Options.ConfigFile)
78         if err != nil {
79                 return nil, err
80         }
81
82         // Handle where Logs are redirected:
83         //  default 'stdout' (logfile option default value)
84         //  else use file (or filepath) set by --logfile option
85         //  that may be overwritten by LogsDir field of config file
86         logF := c.Options.LogFile
87         logD := c.FileConf.LogsDir
88         if logF != "stdout" {
89                 if logD != "" {
90                         lf := filepath.Base(logF)
91                         if lf == "" || lf == "." {
92                                 lf = "xds-agent.log"
93                         }
94                         logF = filepath.Join(logD, lf)
95                 } else {
96                         logD = filepath.Dir(logF)
97                 }
98         }
99         if logD == "" || logD == "." {
100                 logD = "/tmp/xds/logs"
101         }
102         c.Options.LogFile = logF
103         c.FileConf.LogsDir = logD
104
105         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
106                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
107                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
108                 }
109         }
110         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
111         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
112
113         return &c, nil
114 }