X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fxdsconfig%2Fconfig.go;h=53d16390ce4708126e5fbff6f7ee90876ac22b99;hb=5756bd350d585660cce53a28dc66bfcf162ecca1;hp=465620b3d2e4cdd2c5bb6ffedc5166776ade432b;hpb=40a7183f3b4aa32379aa8b4949f5f9c5e32f79f6;p=src%2Fxds%2Fxds-server.git diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 465620b..53d1639 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -2,12 +2,13 @@ package xdsconfig import ( "fmt" - + "io" "os" + "path/filepath" "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" - "github.com/iotbzh/xds-server/lib/common" + common "github.com/iotbzh/xds-common/golib" ) // Config parameters (json format) of /config command @@ -16,14 +17,20 @@ type Config struct { APIVersion string `json:"apiVersion"` VersionGitTag string `json:"gitTag"` Builder BuilderConfig `json:"builder"` - Folders FoldersConfig `json:"folders"` // Private (un-exported fields in REST GET /config route) - FileConf FileConfig `json:"-"` - WebAppDir string `json:"-"` - HTTPPort string `json:"-"` - ShareRootDir string `json:"-"` - Log *logrus.Logger `json:"-"` + Options Options `json:"-"` + FileConf FileConfig `json:"-"` + Log *logrus.Logger `json:"-"` + LogVerboseOut io.Writer `json:"-"` +} + +// Options set at the command line +type Options struct { + ConfigFile string + LogLevel string + LogFile string + NoFolderConfig bool } // Config default values @@ -31,6 +38,7 @@ const ( DefaultAPIVersion = "1" DefaultPort = "8000" DefaultShareDir = "/mnt/share" + DefaultSdkRootDir = "/xdt/sdk" ) // Init loads the configuration on start-up @@ -43,33 +51,66 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { APIVersion: DefaultAPIVersion, VersionGitTag: cliCtx.App.Metadata["git-tag"].(string), Builder: BuilderConfig{}, - Folders: FoldersConfig{}, - WebAppDir: "webapp/dist", - HTTPPort: DefaultPort, - ShareRootDir: DefaultShareDir, - Log: log, + Options: Options{ + ConfigFile: cliCtx.GlobalString("config"), + LogLevel: cliCtx.GlobalString("log"), + LogFile: cliCtx.GlobalString("logfile"), + NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"), + }, + FileConf: FileConfig{ + WebAppDir: "webapp/dist", + ShareRootDir: DefaultShareDir, + SdkRootDir: DefaultSdkRootDir, + HTTPPort: DefaultPort, + LogsDir: "", + }, + Log: log, } // config file settings overwrite default config - err = updateConfigFromFile(&c, cliCtx.GlobalString("config")) + err = readGlobalConfig(&c, c.Options.ConfigFile) if err != nil { return nil, err } // Update location of shared dir if needed - if !common.Exists(c.ShareRootDir) { - if err := os.MkdirAll(c.ShareRootDir, 0770); err != nil { + if !common.Exists(c.FileConf.ShareRootDir) { + if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil { return nil, fmt.Errorf("No valid shared directory found: %v", err) } } - c.Log.Infoln("Share root directory: ", c.ShareRootDir) + c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir) + + // Where Logs are redirected: + // default 'stdout' (logfile option default value) + // else use file (or filepath) set by --logfile option + // that may be overwritten by LogsDir field of config file + logF := c.Options.LogFile + logD := c.FileConf.LogsDir + if logF != "stdout" { + if logD != "" { + lf := filepath.Base(logF) + if lf == "" || lf == "." { + lf = "xds-server.log" + } + logF = filepath.Join(logD, lf) + } else { + logD = filepath.Dir(logF) + } + } + if logD == "" || logD == "." { + logD = "/tmp/xds/logs" + } + c.Options.LogFile = logF + c.FileConf.LogsDir = logD 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 file: ", c.Options.LogFile) c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir) return &c, nil