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 0a96a9549d07a6ed199f0b6e24909a94ddb72821 Author: Mr.An <[email protected]> AuthorDate: Wed Jun 8 12:10:10 2022 +0800 add public function initConfig --- config/config.go | 27 ++++++++++++++++----------- config/config_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 8dcf8f3f..78f11c23 100644 --- a/config/config.go +++ b/config/config.go @@ -40,6 +40,20 @@ func GetConfig() *viper.Viper { return v } +func initConfig(v *viper.Viper) { + 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) + } +} + func getConfigName() string { return CONFIG_NAME } @@ -93,6 +107,7 @@ func replaceNewEnvItemInOldContent(v *viper.Viper, envFileContent string) (error // WriteConfig save viper to .env file func WriteConfig(v *viper.Viper) error { + initConfig(v) return WriteConfigAs(v, getConfigName()) } @@ -144,17 +159,7 @@ func WriteConfigAs(v *viper.Viper, filename string) error { func init() { // create the object and load the .env file v = viper.New() - 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) - } + initConfig(v) err := v.ReadInConfig() if err != nil { logrus.Warn("Failed to read [.env] file:", err) diff --git a/config/config_test.go b/config/config_test.go index 187f3e2a..9928a7f1 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -20,7 +20,9 @@ package config import ( "github.com/sirupsen/logrus" "github.com/spf13/afero" + "github.com/spf13/viper" "github.com/stretchr/testify/assert" + "os" "testing" ) @@ -33,6 +35,31 @@ func TestReadConfig(t *testing.T) { } func TestWriteConfig(t *testing.T) { + filename := ".env" + cwd, _ := os.Getwd() + envFilePath := cwd + string(os.PathSeparator) + filename + os.Setenv("ENV_PATH", envFilePath) + 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() + _ = WriteConfig(v) + isEmpty, _ := afero.IsEmpty(fs, filename) + assert.False(t, isEmpty) + configNew := viper.New() + configNew.SetConfigFile(envFilePath) + err := configNew.ReadInConfig() + assert.Equal(t, nil, err) + + bar := configNew.GetString("DB_URL") + assert.Equal(t, newDbUrl, bar) + err = fs.Remove(filename) + assert.Equal(t, err == nil, true) +} + +func TestWriteConfigAs(t *testing.T) { filename := ".env" v := GetConfig() newDbUrl := "mysql://merico:merico@mysql:3307/lake?charset=utf8mb4&parseTime=True"
