Этот коммит содержится в:
Chris
2017-11-16 08:40:26 -06:00
коммит произвёл GitHub
родитель 6eb4b4604c
Коммит bf6bb9bce9
8 изменённых файлов: 85 добавлений и 19 удалений

Просмотреть файл

@@ -70,11 +70,20 @@ func RemoveConfigListener(id string) {
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) {
for _, dir := range []string{"./config", "../config", "../../config", "."} {
path, _ := filepath.Abs(filepath.Join(dir, fileName))
if _, err := os.Stat(path); err == nil {
return path
if filepath.IsAbs(fileName) {
if _, err := os.Stat(fileName); err == nil {
return fileName
}
} 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 ""
@@ -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,
// it will attempt to locate a default config file, and copy it. In either case, the config file
// path is returned.
// it will attempt to locate a default config file, and copy it to a file named fileName in the same
// directory. In either case, the config file path is returned.
func EnsureConfigFile(fileName string) (string, error) {
if configFile := FindConfigFile(fileName); configFile != "" {
return configFile, nil

Просмотреть файл

@@ -4,7 +4,9 @@
package utils
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
@@ -21,6 +23,23 @@ func TestConfig(t *testing.T) {
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) {
os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Enviroment")
os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")