This is an automated email from the ASF dual-hosted git repository. warren pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit 6258718a07980ed79c49997732fa3e38069adc78 Author: Mr.An <[email protected]> AuthorDate: Tue Jun 7 17:29:25 2022 +0800 [improve][config] optimize configuration initialization function --- config/config.go | 33 ++++++++++++++--------- config/config_test.go | 75 +++++++++++++++++++-------------------------------- 2 files changed, 47 insertions(+), 61 deletions(-) diff --git a/config/config.go b/config/config.go index 4ecaedb6..8dcf8f3f 100644 --- a/config/config.go +++ b/config/config.go @@ -31,6 +31,8 @@ import ( "github.com/spf13/viper" ) +const CONFIG_NAME = ".env" + // Lowcase V for private this. You can use it by call GetConfig. var v *viper.Viper = nil @@ -38,8 +40,12 @@ func GetConfig() *viper.Viper { return v } +func getConfigName() string { + return CONFIG_NAME +} + // Set default value for no .env or .env not set it -func setDefaultValue() { +func setDefaultValue(v *viper.Viper) { v.SetDefault("DB_URL", "mysql://merico:merico@mysql:3306/lake?charset=utf8mb4&parseTime=True") v.SetDefault("PORT", ":8080") v.SetDefault("PLUGIN_DIR", "bin/plugins") @@ -85,18 +91,9 @@ func replaceNewEnvItemInOldContent(v *viper.Viper, envFileContent string) (error return nil, envFileContent } -// return the env path -func getEnvPath() string { - envPath := os.Getenv("ENV_PATH") - if envPath == "" { - envPath = ".env" - } - return envPath -} - // WriteConfig save viper to .env file func WriteConfig(v *viper.Viper) error { - return WriteConfigAs(v, getEnvPath()) + return WriteConfigAs(v, getConfigName()) } // WriteConfigAs save viper to custom filename @@ -147,14 +144,24 @@ func WriteConfigAs(v *viper.Viper, filename string) error { func init() { // create the object and load the .env file v = viper.New() - v.SetConfigFile(getEnvPath()) + v.SetConfigName(getConfigName()) + v.SetConfigType("env") + envPath := os.Getenv("ENV_PATH") + // AddConfigPath adds a path for Viper to search for the config file in. + if envPath == "" { + v.AddConfigPath("$PWD/../..") + v.AddConfigPath("..") + v.AddConfigPath(".") + } else { + v.AddConfigPath(envPath) + } err := v.ReadInConfig() if err != nil { logrus.Warn("Failed to read [.env] file:", err) } v.AutomaticEnv() - setDefaultValue() + setDefaultValue(v) // This line is essential for reading and writing v.WatchConfig() } diff --git a/config/config_test.go b/config/config_test.go index 41f63be8..187f3e2a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,57 +18,42 @@ limitations under the License. package config import ( - "os" - "testing" - - "github.com/spf13/viper" + "github.com/sirupsen/logrus" + "github.com/spf13/afero" "github.com/stretchr/testify/assert" + "testing" ) -func TestReadAndWriteToConfig(t *testing.T) { +func TestReadConfig(t *testing.T) { + DbUrl := "mysql://merico:merico@mysql:3306/lake?charset=utf8mb4&parseTime=True" v := GetConfig() currentDbUrl := v.GetString("DB_URL") - newDbUrl := "ThisIsATest" - assert.Equal(t, currentDbUrl != newDbUrl, true) - v.Set("DB_URL", newDbUrl) - err := v.WriteConfig() - assert.Equal(t, err == nil, true) - nowDbUrl := v.GetString("DB_URL") - assert.Equal(t, nowDbUrl == newDbUrl, true) - // Reset back to current - v.Set("DB_URL", currentDbUrl) - err = v.WriteConfig() - assert.Equal(t, err == nil, true) + logrus.Infof("current db url: %s\n", currentDbUrl) + assert.Equal(t, currentDbUrl == DbUrl, true) } -func TestGetEnvPath(t *testing.T) { - os.Unsetenv("ENV_PATH") - assert.Equal(t, getEnvPath(), ".env") - os.Setenv("ENV_PATH", "/foo/bar/config.env") - assert.Equal(t, getEnvPath(), "/foo/bar/config.env") +func TestWriteConfig(t *testing.T) { + filename := ".env" + v := GetConfig() + newDbUrl := "mysql://merico:merico@mysql:3307/lake?charset=utf8mb4&parseTime=True" + v.Set("DB_URL", newDbUrl) + fs := afero.NewOsFs() + file, _ := fs.Create(filename) + defer file.Close() + _ = WriteConfigAs(v, filename) + isEmpty, _ := afero.IsEmpty(fs, filename) + assert.False(t, isEmpty) + err := fs.Remove(filename) + assert.Equal(t, err == nil, true) } -func TestWriteConfigToEnvPath(t *testing.T) { - cwd, _ := os.Getwd() - envFilePath := cwd + string(os.PathSeparator) + "test.env" - os.Setenv("ENV_PATH", envFilePath) - // remove it, and WriteConfig should create it. - os.Remove(envFilePath) - defer os.Remove(envFilePath) - - config := GetConfig() - config.Set("FOO", "bar") - - err := WriteConfig(config) - assert.Equal(t, nil, err) - - configNew := viper.New() - configNew.SetConfigFile(envFilePath) - err = configNew.ReadInConfig() - assert.Equal(t, nil, err) - - bar := configNew.GetString("FOO") - assert.Equal(t, "bar", bar) +func TestSetConfigVariate(t *testing.T) { + v := GetConfig() + newDbUrl := "mysql://merico:merico@mysql:3307/lake?charset=utf8mb4&parseTime=True" + v.Set("DB_URL", newDbUrl) + currentDbUrl := v.GetString("DB_URL") + logrus.Infof("current db url: %s\n", currentDbUrl) + assert.Equal(t, currentDbUrl == newDbUrl, true) } func TestReplaceNewEnvItemInOldContent(t *testing.T) { @@ -84,19 +69,16 @@ func TestReplaceNewEnvItemInOldContent(t *testing.T) { err, s := replaceNewEnvItemInOldContent(v, ` some unuseful message # comment - a blank AA =123 bB= cc = dd = - # some comment eE= ff="some content" and some comment Ggg=132 h.278=1 - `) if err != nil { panic(err) @@ -104,18 +86,15 @@ h.278=1 assert.Equal(t, ` some unuseful message # comment - a blank AA="aaaa" BB="1#1" CC="1\"'1" DD="1\\\"1" - # some comment EE="=" FF="1.01" GGG="gggg" H.278="278" - `, s) }
