Remove duplicate fields and set default SDK dir.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Tue, 20 Jun 2017 20:29:59 +0000 (22:29 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Tue, 20 Jun 2017 20:29:59 +0000 (22:29 +0200)
- Removed duplicate fields between Config and FileConfig
- set /xdt/sdk as default SDK directory

lib/apiv1/agent.go
lib/model/folder.go
lib/syncthing/stfolder.go
lib/webserver/server.go
lib/xdsconfig/config.go
lib/xdsconfig/fileconfig.go
main.go

index d8feb61..b693608 100644 (file)
@@ -27,7 +27,7 @@ func (s *APIService) getXdsAgentInfo(c *gin.Context) {
 
        res := XDSAgentInfo{}
        tarballURL := "assets/xds-agent-tarballs"
-       tarballDir := filepath.Join(s.cfg.WebAppDir, "assets", "xds-agent-tarballs")
+       tarballDir := filepath.Join(s.cfg.FileConf.WebAppDir, "assets", "xds-agent-tarballs")
        if common.Exists(tarballDir) {
                files, err := filepath.Glob(path.Join(tarballDir, "xds-agent_*.zip"))
                if err != nil {
index fa94409..c73accc 100644 (file)
@@ -48,7 +48,7 @@ func (c *Folder) UpdateAll(newCfg xdsconfig.Config) error {
                                Label:        f.Label,
                                RelativePath: f.RelativePath,
                                SyncThingID:  f.SyncThingID,
-                               ShareRootDir: c.ShareRootDir,
+                               ShareRootDir: c.FileConf.ShareRootDir,
                        }); err != nil {
                                return err
                        }
@@ -62,7 +62,7 @@ func (c *Folder) UpdateAll(newCfg xdsconfig.Config) error {
 func (c *Folder) UpdateFolder(newFolder xdsconfig.FolderConfig) (xdsconfig.FolderConfig, error) {
        // rootPath should not be empty
        if newFolder.RootPath == "" {
-               newFolder.RootPath = c.Conf.ShareRootDir
+               newFolder.RootPath = c.Conf.FileConf.ShareRootDir
        }
 
        // Sanity check of folder settings
index e7ee0ec..661e19d 100644 (file)
@@ -55,7 +55,7 @@ func (s *SyncThing) FolderChange(f xdsconfig.FolderConfig) error {
        folder := config.FolderConfiguration{
                ID:      id,
                Label:   label,
-               RawPath: filepath.Join(s.conf.ShareRootDir, f.RelativePath),
+               RawPath: filepath.Join(s.conf.FileConf.ShareRootDir, f.RelativePath),
        }
 
        if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
index 4268b40..8fd7e44 100644 (file)
@@ -100,12 +100,12 @@ func (s *Server) Serve() error {
        */
 
        // Web Application (serve on / )
-       idxFile := path.Join(s.cfg.WebAppDir, indexFilename)
+       idxFile := path.Join(s.cfg.FileConf.WebAppDir, indexFilename)
        if _, err := os.Stat(idxFile); err != nil {
                s.log.Fatalln("Web app directory not found, check/use webAppDir setting in config file: ", idxFile)
        }
-       s.log.Infof("Serve WEB app dir: %s", s.cfg.WebAppDir)
-       s.router.Use(static.Serve("/", static.LocalFile(s.cfg.WebAppDir, true)))
+       s.log.Infof("Serve WEB app dir: %s", s.cfg.FileConf.WebAppDir)
+       s.router.Use(static.Serve("/", static.LocalFile(s.cfg.FileConf.WebAppDir, true)))
        s.webApp = s.router.Group("/", s.serveIndexFile)
        {
                s.webApp.GET("/")
@@ -114,8 +114,8 @@ func (s *Server) Serve() error {
        // Serve in the background
        serveError := make(chan error, 1)
        go func() {
-               fmt.Printf("Web Server running on localhost:%s ...\n", s.cfg.HTTPPort)
-               serveError <- http.ListenAndServe(":"+s.cfg.HTTPPort, s.router)
+               fmt.Printf("Web Server running on localhost:%s ...\n", s.cfg.FileConf.HTTPPort)
+               serveError <- http.ListenAndServe(":"+s.cfg.FileConf.HTTPPort, s.router)
        }()
 
        // Wait for stop, restart or error signals
index 465620b..5ada35f 100644 (file)
@@ -19,11 +19,8 @@ type Config struct {
        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:"-"`
+       FileConf FileConfig     `json:"-"`
+       Log      *logrus.Logger `json:"-"`
 }
 
 // Config default values
@@ -31,6 +28,7 @@ const (
        DefaultAPIVersion = "1"
        DefaultPort       = "8000"
        DefaultShareDir   = "/mnt/share"
+       DefaultSdkRootDir = "/xdt/sdk"
 )
 
 // Init loads the configuration on start-up
@@ -44,11 +42,13 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
                VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
                Builder:       BuilderConfig{},
                Folders:       FoldersConfig{},
-
-               WebAppDir:    "webapp/dist",
-               HTTPPort:     DefaultPort,
-               ShareRootDir: DefaultShareDir,
-               Log:          log,
+               FileConf: FileConfig{
+                       WebAppDir:    "webapp/dist",
+                       ShareRootDir: DefaultShareDir,
+                       SdkRootDir:   DefaultSdkRootDir,
+                       HTTPPort:     DefaultPort,
+               },
+               Log: log,
        }
 
        // config file settings overwrite default config
@@ -58,12 +58,12 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
        }
 
        // 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)
 
        if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
                if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
index 01bd4c9..99e5382 100644 (file)
@@ -77,7 +77,6 @@ func updateConfigFromFile(c *Config, confFile string) error {
        if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
                return err
        }
-       c.FileConf = fCfg
 
        // Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
        for _, field := range []*string{
@@ -94,30 +93,33 @@ func updateConfigFromFile(c *Config, confFile string) error {
                }
        }
 
-       // Config file settings overwrite default config
-
-       if fCfg.WebAppDir != "" {
-               c.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
+       // Use config file settings else use default config
+       if fCfg.WebAppDir == "" {
+               fCfg.WebAppDir = c.FileConf.WebAppDir
+       }
+       if fCfg.ShareRootDir == "" {
+               fCfg.ShareRootDir = c.FileConf.ShareRootDir
        }
-       // Is it a full path ?
-       if !strings.HasPrefix(c.WebAppDir, "/") && exePath != "" {
+       if fCfg.SdkRootDir == "" {
+               fCfg.SdkRootDir = c.FileConf.SdkRootDir
+       }
+       if fCfg.HTTPPort == "" {
+               fCfg.HTTPPort = c.FileConf.HTTPPort
+       }
+
+       // Resolve webapp dir (support relative or full path)
+       fCfg.WebAppDir = strings.Trim(fCfg.WebAppDir, " ")
+       if !strings.HasPrefix(fCfg.WebAppDir, "/") && exePath != "" {
                // Check first from current directory
                for _, rootD := range []string{cwd, exePath} {
-                       ff := path.Join(rootD, c.WebAppDir, "index.html")
+                       ff := path.Join(rootD, fCfg.WebAppDir, "index.html")
                        if common.Exists(ff) {
-                               c.WebAppDir = path.Join(rootD, c.WebAppDir)
+                               fCfg.WebAppDir = path.Join(rootD, fCfg.WebAppDir)
                                break
                        }
                }
        }
 
-       if fCfg.ShareRootDir != "" {
-               c.ShareRootDir = fCfg.ShareRootDir
-       }
-
-       if fCfg.HTTPPort != "" {
-               c.HTTPPort = fCfg.HTTPPort
-       }
-
+       c.FileConf = fCfg
        return nil
 }
diff --git a/main.go b/main.go
index 8d49bab..fd1480e 100644 (file)
--- a/main.go
+++ b/main.go
@@ -179,12 +179,16 @@ func xdsApp(cliCtx *cli.Context) error {
                        return cli.NewExitError(err, 2)
                }
                for _, stFld := range stCfg.Folders {
-                       relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.ShareRootDir)
+                       relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.FileConf.ShareRootDir)
                        if relativePath == "" {
                                relativePath = stFld.RawPath
                        }
 
-                       newFld := xdsconfig.NewFolderConfig(stFld.ID, stFld.Label, ctx.Config.ShareRootDir, strings.TrimRight(relativePath, "/"), defaultSdk)
+                       newFld := xdsconfig.NewFolderConfig(stFld.ID,
+                               stFld.Label,
+                               ctx.Config.FileConf.ShareRootDir,
+                               strings.TrimRight(relativePath, "/"),
+                               defaultSdk)
                        ctx.Config.Folders = ctx.Config.Folders.Update(xdsconfig.FoldersConfig{newFld})
                }