Config/Engine: Add data directory to config.json (#549)

* add data directory to config.json

* fix quality check issues

* adjust data directory only when explicitly set

* unexport ValidateSettings
* process flags earlier so they can also be used when loading config
* fix test depends on flags

* rename config.DataDir to DataDirectory

* also don't omit in JSON if empty

* datadir flag induces dry run

* log warning
* enable parallel for sub tests
* leave data dir empty in example config

* remove parallel for loadConfigWithSettings

* create a new config object instead of using a shared one

* remove a test that potentially reads user file

* rename test methods to MixedCaps

* clean up test dir after engine tests

* use global config variable
This commit is contained in:
Rauno Ots
2020-09-17 03:11:17 +02:00
committed by GitHub
parent 25466b2c9d
commit a67b5cf715
6 changed files with 165 additions and 16 deletions

View File

@@ -1303,7 +1303,7 @@ func (c *Config) CheckLoggerConfig() error {
log.GlobalLogConfig = &c.Logging
log.RWM.Unlock()
logPath := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "logs")
logPath := c.GetDataPath("logs")
err := common.CreateDir(logPath)
if err != nil {
return err
@@ -1325,7 +1325,7 @@ func (c *Config) checkGCTScriptConfig() error {
c.GCTScript.MaxVirtualMachines = gctscript.DefaultMaxVirtualMachines
}
scriptPath := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "scripts")
scriptPath := c.GetDataPath("scripts")
err := common.CreateDir(scriptPath)
if err != nil {
return err
@@ -1362,7 +1362,7 @@ func (c *Config) checkDatabaseConfig() error {
}
if c.Database.Driver == database.DBSQLite || c.Database.Driver == database.DBSQLite3 {
databaseDir := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "database")
databaseDir := c.GetDataPath("database")
err := common.CreateDir(databaseDir)
if err != nil {
return err
@@ -1845,3 +1845,14 @@ func (c *Config) AssetTypeEnabled(a asset.Item, exch string) (bool, error) {
}
return true, nil
}
// GetDataPath gets the data path for the given subpath
func (c *Config) GetDataPath(elem ...string) string {
var baseDir string
if c.DataDirectory != "" {
baseDir = c.DataDirectory
} else {
baseDir = common.GetDefaultDataDir(runtime.GOOS)
}
return filepath.Join(append([]string{baseDir}, elem...)...)
}

View File

@@ -1,6 +1,8 @@
package config
import (
"path/filepath"
"runtime"
"strings"
"testing"
@@ -2034,3 +2036,43 @@ func TestRemoveExchange(t *testing.T) {
t.Fatal("exchange shouldn't exist")
}
}
func TestGetDataPath(t *testing.T) {
tests := []struct {
name string
dir string
elem []string
want string
}{
{
name: "empty",
dir: "",
elem: []string{},
want: common.GetDefaultDataDir(runtime.GOOS),
},
{
name: "empty a b",
dir: "",
elem: []string{"a", "b"},
want: filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "a", "b"),
},
{
name: "target",
dir: "target",
elem: []string{"a", "b"},
want: filepath.Join("target", "a", "b"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := &Config{
DataDirectory: tt.dir,
}
if got := c.GetDataPath(tt.elem...); got != tt.want {
t.Errorf("Config.GetDataPath() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -78,6 +78,7 @@ var (
// Exchanges
type Config struct {
Name string `json:"name"`
DataDirectory string `json:"dataDirectory"`
EncryptConfig int `json:"encryptConfig"`
GlobalHTTPTimeout time.Duration `json:"globalHTTPTimeout"`
Database database.Config `json:"database"`