Set install dir to /opt/AGL and move conf to $HOME/.xds-server
[src/xds/xds-server.git] / lib / xdsconfig / config.go
index a3e5a7e..53d1639 100644 (file)
@@ -2,7 +2,9 @@ package xdsconfig
 
 import (
        "fmt"
+       "io"
        "os"
+       "path/filepath"
 
        "github.com/Sirupsen/logrus"
        "github.com/codegangsta/cli"
@@ -17,9 +19,10 @@ type Config struct {
        Builder       BuilderConfig `json:"builder"`
 
        // Private (un-exported fields in REST GET /config route)
-       Options  Options        `json:"-"`
-       FileConf FileConfig     `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
@@ -60,6 +63,7 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
                        ShareRootDir: DefaultShareDir,
                        SdkRootDir:   DefaultSdkRootDir,
                        HTTPPort:     DefaultPort,
+                       LogsDir:      "",
                },
                Log: log,
        }
@@ -78,11 +82,35 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
        }
        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