summaryrefslogtreecommitdiffstats
path: root/lib/xdsconfig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/xdsconfig')
-rw-r--r--lib/xdsconfig/config.go27
-rw-r--r--lib/xdsconfig/fileconfig.go40
-rw-r--r--lib/xdsconfig/folderconfig.go85
-rw-r--r--lib/xdsconfig/foldersconfig.go47
4 files changed, 51 insertions, 148 deletions
diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go
index f2d0710..82ca97f 100644
--- a/lib/xdsconfig/config.go
+++ b/lib/xdsconfig/config.go
@@ -2,7 +2,7 @@ package xdsconfig
import (
"fmt"
-
+ "io"
"os"
"github.com/Sirupsen/logrus"
@@ -16,11 +16,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:"-"`
- 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
@@ -41,7 +50,13 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
APIVersion: DefaultAPIVersion,
VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
Builder: BuilderConfig{},
- Folders: FoldersConfig{},
+
+ 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,
@@ -52,7 +67,7 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
}
// config file settings overwrite default config
- err = updateConfigFromFile(&c, cliCtx.GlobalString("config"))
+ err = readGlobalConfig(&c, c.Options.ConfigFile)
if err != nil {
return nil, err
}
diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go
index 90c1aad..2dbf884 100644
--- a/lib/xdsconfig/fileconfig.go
+++ b/lib/xdsconfig/fileconfig.go
@@ -11,6 +11,16 @@ import (
common "github.com/iotbzh/xds-common/golib"
)
+const (
+ // ConfigDir Directory in user HOME directory where xds config will be saved
+ ConfigDir = ".xds"
+ // GlobalConfigFilename Global config filename
+ GlobalConfigFilename = "config.json"
+ // FoldersConfigFilename Folders config filename
+ FoldersConfigFilename = "server-config_folders.xml"
+)
+
+// SyncThingConf definition
type SyncThingConf struct {
BinDir string `json:"binDir"`
Home string `json:"home"`
@@ -19,6 +29,7 @@ type SyncThingConf struct {
RescanIntervalS int `json:"rescanIntervalS"`
}
+// FileConfig is the JSON structure of xds-server config file (config.json)
type FileConfig struct {
WebAppDir string `json:"webAppDir"`
ShareRootDir string `json:"shareRootDir"`
@@ -28,21 +39,21 @@ type FileConfig struct {
LogsDir string `json:"logsDir"`
}
-// getConfigFromFile reads configuration from a config file.
+// readGlobalConfig reads configuration from a config file.
// Order to determine which config file is used:
// 1/ from command line option: "--config myConfig.json"
// 2/ $HOME/.xds/config.json file
// 3/ <current_dir>/config.json file
// 4/ <xds-server executable dir>/config.json file
-
-func updateConfigFromFile(c *Config, confFile string) error {
+func readGlobalConfig(c *Config, confFile string) error {
searchIn := make([]string, 0, 3)
if confFile != "" {
searchIn = append(searchIn, confFile)
}
if usr, err := user.Current(); err == nil {
- searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "config.json"))
+ searchIn = append(searchIn, path.Join(usr.HomeDir, ConfigDir,
+ GlobalConfigFilename))
}
cwd, err := os.Getwd()
if err == nil {
@@ -70,7 +81,6 @@ func updateConfigFromFile(c *Config, confFile string) error {
// TODO move on viper package to support comments in JSON and also
// bind with flags (command line options)
// see https://github.com/spf13/viper#working-with-flags
-
fd, _ := os.Open(*cFile)
defer fd.Close()
fCfg := FileConfig{}
@@ -79,14 +89,15 @@ func updateConfigFromFile(c *Config, confFile string) error {
}
// Support environment variables (IOW ${MY_ENV_VAR} syntax) in config.json
- for _, field := range []*string{
+ vars := []*string{
&fCfg.WebAppDir,
&fCfg.ShareRootDir,
&fCfg.SdkRootDir,
- &fCfg.LogsDir,
- &fCfg.SThgConf.Home,
- &fCfg.SThgConf.BinDir} {
-
+ &fCfg.LogsDir}
+ if fCfg.SThgConf != nil {
+ vars = append(vars, &fCfg.SThgConf.Home, &fCfg.SThgConf.BinDir)
+ }
+ for _, field := range vars {
var err error
if *field, err = common.ResolveEnvVar(*field); err != nil {
return err
@@ -123,3 +134,12 @@ func updateConfigFromFile(c *Config, confFile string) error {
c.FileConf = fCfg
return nil
}
+
+// FoldersConfigFilenameGet
+func FoldersConfigFilenameGet() (string, error) {
+ usr, err := user.Current()
+ if err != nil {
+ return "", err
+ }
+ return path.Join(usr.HomeDir, ConfigDir, FoldersConfigFilename), nil
+}
diff --git a/lib/xdsconfig/folderconfig.go b/lib/xdsconfig/folderconfig.go
deleted file mode 100644
index bb2b56f..0000000
--- a/lib/xdsconfig/folderconfig.go
+++ /dev/null
@@ -1,85 +0,0 @@
-package xdsconfig
-
-import (
- "fmt"
- "log"
- "path/filepath"
-)
-
-// FolderType constances
-const (
- FolderTypeDocker = 0
- FolderTypeWindowsSubsystem = 1
- FolderTypeCloudSync = 2
-
- FolderStatusErrorConfig = "ErrorConfig"
- FolderStatusDisable = "Disable"
- FolderStatusEnable = "Enable"
-)
-
-// FolderType is the type of sharing folder
-type FolderType int
-
-// FolderConfig is the config for one folder
-type FolderConfig struct {
- ID string `json:"id" binding:"required"`
- Label string `json:"label"`
- RelativePath string `json:"path"`
- Type FolderType `json:"type"`
- SyncThingID string `json:"syncThingID"`
- BuilderSThgID string `json:"builderSThgID"`
- Status string `json:"status"`
- DefaultSdk string `json:"defaultSdk"`
-
- // Not exported fields
- RootPath string `json:"-"`
-}
-
-// NewFolderConfig creates a new folder object
-func NewFolderConfig(id, label, rootDir, path string, defaultSdk string) FolderConfig {
- return FolderConfig{
- ID: id,
- Label: label,
- RelativePath: path,
- Type: FolderTypeCloudSync,
- SyncThingID: "",
- Status: FolderStatusDisable,
- RootPath: rootDir,
- DefaultSdk: defaultSdk,
- }
-}
-
-// GetFullPath returns the full path
-func (c *FolderConfig) GetFullPath(dir string) string {
- if &dir == nil {
- dir = ""
- }
- if filepath.IsAbs(dir) {
- return filepath.Join(c.RootPath, dir)
- }
- return filepath.Join(c.RootPath, c.RelativePath, dir)
-}
-
-// Verify is called to verify that a configuration is valid
-func (c *FolderConfig) Verify() error {
- var err error
-
- if c.Type != FolderTypeCloudSync {
- err = fmt.Errorf("Unsupported folder type")
- }
-
- if c.SyncThingID == "" {
- err = fmt.Errorf("device id not set (SyncThingID field)")
- }
-
- if c.RootPath == "" {
- err = fmt.Errorf("RootPath must not be empty")
- }
-
- if err != nil {
- c.Status = FolderStatusErrorConfig
- log.Printf("ERROR Verify: %v\n", err)
- }
-
- return err
-}
diff --git a/lib/xdsconfig/foldersconfig.go b/lib/xdsconfig/foldersconfig.go
deleted file mode 100644
index 4ad16df..0000000
--- a/lib/xdsconfig/foldersconfig.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package xdsconfig
-
-import (
- "fmt"
-)
-
-// FoldersConfig contains all the folder configurations
-type FoldersConfig []FolderConfig
-
-// GetIdx returns the index of the folder matching id in FoldersConfig array
-func (c FoldersConfig) GetIdx(id string) int {
- for i := range c {
- if id == c[i].ID {
- return i
- }
- }
- return -1
-}
-
-// Update is used to fully update or add a new FolderConfig
-func (c FoldersConfig) Update(newCfg FoldersConfig) FoldersConfig {
- for i := range newCfg {
- found := false
- for j := range c {
- if newCfg[i].ID == c[j].ID {
- c[j] = newCfg[i]
- found = true
- break
- }
- }
- if !found {
- c = append(c, newCfg[i])
- }
- }
- return c
-}
-
-// Delete is used to delete a folder matching id in FoldersConfig array
-func (c FoldersConfig) Delete(id string) (FoldersConfig, FolderConfig, error) {
- if idx := c.GetIdx(id); idx != -1 {
- f := c[idx]
- c = append(c[:idx], c[idx+1:]...)
- return c, f, nil
- }
-
- return c, FolderConfig{}, fmt.Errorf("invalid id")
-}