From a50baa7c309f7eb55fe87c71f4c688ace325b6ac Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Wed, 17 May 2017 17:10:45 +0200 Subject: Add logsDir setting and more - add logsDir setting in config.json - redirect Syncthing and Syncthing-inotify into log files - Use autogenerated Syncthing apikey if gui-apikey not set in config.json --- lib/xdsconfig/config.go | 30 +++++++++++++++--------------- lib/xdsconfig/fileconfig.go | 37 +++++++++++++++---------------------- 2 files changed, 30 insertions(+), 37 deletions(-) (limited to 'lib/xdsconfig') diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 1f53cbd..efea5ba 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -7,6 +7,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" + "github.com/iotbzh/xds-server/lib/common" ) // Config parameters (json format) of /config command @@ -16,9 +17,9 @@ type Config struct { VersionGitTag string `json:"gitTag"` // Private / un-exported fields - HTTPPort string `json:"-"` - FileConf *FileConfig - log *logrus.Logger + HTTPPort string `json:"-"` + FileConf *FileConfig `json:"-"` + Log *logrus.Logger `json:"-"` } // Config default values @@ -29,7 +30,7 @@ const ( ) // Init loads the configuration on start-up -func Init(ctx *cli.Context, log *logrus.Logger) (Config, error) { +func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) { var err error // Define default configuration @@ -39,27 +40,26 @@ func Init(ctx *cli.Context, log *logrus.Logger) (Config, error) { VersionGitTag: ctx.App.Metadata["git-tag"].(string), HTTPPort: DefaultPort, - log: log, + Log: log, } // config file settings overwrite default config c.FileConf, err = updateConfigFromFile(&c, ctx.GlobalString("config")) if err != nil { - return Config{}, err + return nil, err } - return c, nil + if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) { + if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil { + return nil, fmt.Errorf("Cannot create logs dir: %v", err) + } + } + c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir) + + return &c, nil } // UpdateAll Update the current configuration func (c *Config) UpdateAll(newCfg Config) error { return fmt.Errorf("Not Supported") } - -func dirExists(path string) bool { - _, err := os.Stat(path) - if os.IsNotExist(err) { - return false - } - return true -} diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 0c4828c..5cf8db2 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -19,6 +19,7 @@ type SyncThingConf struct { type FileConfig struct { HTTPPort string `json:"httpPort"` + LogsDir string `json:"logsDir"` SThgConf *SyncThingConf `json:"syncthing"` } @@ -60,7 +61,7 @@ func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) { return &fCfg, nil } - c.log.Infof("Use config file: %s", *cFile) + c.Log.Infof("Use config file: %s", *cFile) // TODO move on viper package to support comments in JSON and also // bind with flags (command line options) @@ -73,18 +74,22 @@ func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) { } // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json - // TODO: better to use reflect package to iterate on fields and be more generic - var rep string - - if rep, err = resolveEnvVar(fCfg.SThgConf.BinDir); err != nil { - return nil, err + for _, field := range []*string{ + &fCfg.LogsDir, + &fCfg.SThgConf.Home, + &fCfg.SThgConf.BinDir} { + + rep, err := resolveEnvVar(*field) + if err != nil { + return nil, err + } + *field = path.Clean(rep) } - fCfg.SThgConf.BinDir = path.Clean(rep) - if rep, err = resolveEnvVar(fCfg.SThgConf.Home); err != nil { - return nil, err + // Config file settings overwrite default config + if fCfg.HTTPPort != "" { + c.HTTPPort = fCfg.HTTPPort } - fCfg.SThgConf.Home = path.Clean(rep) return &fCfg, nil } @@ -106,15 +111,3 @@ func resolveEnvVar(s string) (string, error) { return res, nil } - -// exists returns whether the given file or directory exists or not -func exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true - } - if os.IsNotExist(err) { - return false - } - return true -} -- cgit 1.2.3-korg