fix config cli option (#7850)
Этот коммит содержится в:
23
app/app.go
23
app/app.go
@@ -48,7 +48,8 @@ type App struct {
|
|||||||
Mfa einterfaces.MfaInterface
|
Mfa einterfaces.MfaInterface
|
||||||
Saml einterfaces.SamlInterface
|
Saml einterfaces.SamlInterface
|
||||||
|
|
||||||
newStore func() store.Store
|
configFile string
|
||||||
|
newStore func() store.Store
|
||||||
|
|
||||||
sessionCache *utils.Cache
|
sessionCache *utils.Cache
|
||||||
}
|
}
|
||||||
@@ -63,27 +64,29 @@ func New(options ...Option) *App {
|
|||||||
panic("Only one App should exist at a time. Did you forget to call Shutdown()?")
|
panic("Only one App should exist at a time. Did you forget to call Shutdown()?")
|
||||||
}
|
}
|
||||||
|
|
||||||
if utils.T == nil {
|
|
||||||
utils.TranslationsPreInit()
|
|
||||||
}
|
|
||||||
utils.LoadGlobalConfig("config.json")
|
|
||||||
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
|
||||||
|
|
||||||
l4g.Info(utils.T("api.server.new_server.init.info"))
|
|
||||||
|
|
||||||
app := &App{
|
app := &App{
|
||||||
goroutineExitSignal: make(chan struct{}, 1),
|
goroutineExitSignal: make(chan struct{}, 1),
|
||||||
Srv: &Server{
|
Srv: &Server{
|
||||||
Router: mux.NewRouter(),
|
Router: mux.NewRouter(),
|
||||||
},
|
},
|
||||||
sessionCache: utils.NewLru(model.SESSION_CACHE_SIZE),
|
sessionCache: utils.NewLru(model.SESSION_CACHE_SIZE),
|
||||||
|
configFile: "config.json",
|
||||||
}
|
}
|
||||||
app.initEnterprise()
|
|
||||||
|
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(app)
|
option(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if utils.T == nil {
|
||||||
|
utils.TranslationsPreInit()
|
||||||
|
}
|
||||||
|
utils.LoadGlobalConfig(app.configFile)
|
||||||
|
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
||||||
|
|
||||||
|
l4g.Info(utils.T("api.server.new_server.init.info"))
|
||||||
|
|
||||||
|
app.initEnterprise()
|
||||||
|
|
||||||
if app.newStore == nil {
|
if app.newStore == nil {
|
||||||
app.newStore = func() store.Store {
|
app.newStore = func() store.Store {
|
||||||
return store.NewLayeredStore(sqlstore.NewSqlSupplier(app.Config().SqlSettings, app.Metrics), app.Metrics, app.Cluster)
|
return store.NewLayeredStore(sqlstore.NewSqlSupplier(app.Config().SqlSettings, app.Metrics), app.Metrics, app.Cluster)
|
||||||
|
|||||||
@@ -29,3 +29,9 @@ func StoreOverride(override interface{}) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ConfigFile(file string) Option {
|
||||||
|
return func(a *App) {
|
||||||
|
a.configFile = file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
29
cmd/platform/config_test.go
Обычный файл
29
cmd/platform/config_test.go
Обычный файл
@@ -0,0 +1,29 @@
|
|||||||
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost-server/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConfigValidate(t *testing.T) {
|
||||||
|
dir, err := ioutil.TempDir("", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
path := filepath.Join(dir, "config.json")
|
||||||
|
config := &model.Config{}
|
||||||
|
config.SetDefaults()
|
||||||
|
require.NoError(t, ioutil.WriteFile(path, []byte(config.ToJson()), 0600))
|
||||||
|
|
||||||
|
assert.Contains(t, checkCommand(t, "--config", path, "config", "validate"), "The document is valid")
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ func initDBCommandContext(configFileLocation string) (*app.App, error) {
|
|||||||
|
|
||||||
utils.ConfigureCmdLineLog()
|
utils.ConfigureCmdLineLog()
|
||||||
|
|
||||||
a := app.New()
|
a := app.New(app.ConfigFile(configFileLocation))
|
||||||
if model.BuildEnterpriseReady == "true" {
|
if model.BuildEnterpriseReady == "true" {
|
||||||
a.LoadLicense()
|
a.LoadLicense()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func checkCommand(t *testing.T, args ...string) string {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
output, err := exec.Command(path, execArgs(t, args)...).CombinedOutput()
|
output, err := exec.Command(path, execArgs(t, args)...).CombinedOutput()
|
||||||
require.NoError(t, err, string(output))
|
require.NoError(t, err, string(output))
|
||||||
return string(output)
|
return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(string(output)), "PASS"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCommand(t *testing.T, args ...string) error {
|
func runCommand(t *testing.T, args ...string) error {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func runServer(configFileLocation string) {
|
|||||||
l4g.Error("Problem with file storage settings: " + err.Error())
|
l4g.Error("Problem with file storage settings: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
a := app.New()
|
a := app.New(app.ConfigFile(configFileLocation))
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
if model.BuildEnterpriseReady == "true" {
|
if model.BuildEnterpriseReady == "true" {
|
||||||
|
|||||||
@@ -70,11 +70,20 @@ func RemoveConfigListener(id string) {
|
|||||||
delete(cfgListeners, id)
|
delete(cfgListeners, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindConfigFile attempts to find an existing configuration file. fileName can be an absolute or
|
||||||
|
// relative path or name such as "/opt/mattermost/config.json" or simply "config.json". An empty
|
||||||
|
// string is returned if no configuration is found.
|
||||||
func FindConfigFile(fileName string) (path string) {
|
func FindConfigFile(fileName string) (path string) {
|
||||||
for _, dir := range []string{"./config", "../config", "../../config", "."} {
|
if filepath.IsAbs(fileName) {
|
||||||
path, _ := filepath.Abs(filepath.Join(dir, fileName))
|
if _, err := os.Stat(fileName); err == nil {
|
||||||
if _, err := os.Stat(path); err == nil {
|
return fileName
|
||||||
return path
|
}
|
||||||
|
} else {
|
||||||
|
for _, dir := range []string{"./config", "../config", "../../config", "."} {
|
||||||
|
path, _ := filepath.Abs(filepath.Join(dir, fileName))
|
||||||
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
return path
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
@@ -310,8 +319,8 @@ func ReadConfigFile(path string, allowEnvironmentOverrides bool) (*model.Config,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnsureConfigFile will attempt to locate a config file with the given name. If it does not exist,
|
// EnsureConfigFile will attempt to locate a config file with the given name. If it does not exist,
|
||||||
// it will attempt to locate a default config file, and copy it. In either case, the config file
|
// it will attempt to locate a default config file, and copy it to a file named fileName in the same
|
||||||
// path is returned.
|
// directory. In either case, the config file path is returned.
|
||||||
func EnsureConfigFile(fileName string) (string, error) {
|
func EnsureConfigFile(fileName string) (string, error) {
|
||||||
if configFile := FindConfigFile(fileName); configFile != "" {
|
if configFile := FindConfigFile(fileName); configFile != "" {
|
||||||
return configFile, nil
|
return configFile, nil
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -21,6 +23,23 @@ func TestConfig(t *testing.T) {
|
|||||||
InitTranslations(Cfg.LocalizationSettings)
|
InitTranslations(Cfg.LocalizationSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindConfigFile(t *testing.T) {
|
||||||
|
dir, err := ioutil.TempDir("", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
path := filepath.Join(dir, "config.json")
|
||||||
|
require.NoError(t, ioutil.WriteFile(path, []byte("{}"), 0600))
|
||||||
|
|
||||||
|
assert.Equal(t, path, FindConfigFile(path))
|
||||||
|
|
||||||
|
prevDir, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer os.Chdir(prevDir)
|
||||||
|
os.Chdir(dir)
|
||||||
|
assert.Equal(t, path, FindConfigFile(path))
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigFromEnviroVars(t *testing.T) {
|
func TestConfigFromEnviroVars(t *testing.T) {
|
||||||
os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Enviroment")
|
os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Enviroment")
|
||||||
os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")
|
os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user