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 10bf1f962b00aa608d8b9c781b1e3130a5cb892b
Author: Mr.An <[email protected]>
AuthorDate: Wed Jun 8 13:45:46 2022 +0800

    update write config function
---
 config/config.go      | 18 +++++++++++++++---
 config/config_test.go | 12 ++----------
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/config/config.go b/config/config.go
index 78f11c23..9e57cf96 100644
--- a/config/config.go
+++ b/config/config.go
@@ -43,7 +43,7 @@ func GetConfig() *viper.Viper {
 func initConfig(v *viper.Viper) {
        v.SetConfigName(getConfigName())
        v.SetConfigType("env")
-       envPath := os.Getenv("ENV_PATH")
+       envPath := getEnvPath()
        // AddConfigPath adds a path for Viper to search for the config file in.
        if envPath == "" {
                v.AddConfigPath("$PWD/../..")
@@ -58,6 +58,12 @@ func getConfigName() string {
        return CONFIG_NAME
 }
 
+// return the env path
+func getEnvPath() string {
+       envPath := os.Getenv("ENV_PATH")
+       return filepath.Dir(envPath)
+}
+
 // Set default value for no .env or .env not set it
 func setDefaultValue(v *viper.Viper) {
        v.SetDefault("DB_URL", 
"mysql://merico:merico@mysql:3306/lake?charset=utf8mb4&parseTime=True")
@@ -107,8 +113,14 @@ 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())
+       envPath := getEnvPath()
+       fileName := getConfigName()
+
+       if envPath != "" {
+               fileName = envPath + string(os.PathSeparator) + fileName
+       }
+
+       return WriteConfigAs(v, fileName)
 }
 
 // WriteConfigAs save viper to custom filename
diff --git a/config/config_test.go b/config/config_test.go
index 9928a7f1..43b0eb84 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -20,7 +20,6 @@ package config
 import (
        "github.com/sirupsen/logrus"
        "github.com/spf13/afero"
-       "github.com/spf13/viper"
        "github.com/stretchr/testify/assert"
        "os"
        "testing"
@@ -37,7 +36,7 @@ func TestReadConfig(t *testing.T) {
 func TestWriteConfig(t *testing.T) {
        filename := ".env"
        cwd, _ := os.Getwd()
-       envFilePath := cwd + string(os.PathSeparator) + filename
+       envFilePath := cwd + string(os.PathSeparator)
        os.Setenv("ENV_PATH", envFilePath)
        v := GetConfig()
        newDbUrl := 
"mysql://merico:merico@mysql:3307/lake?charset=utf8mb4&parseTime=True"
@@ -48,14 +47,7 @@ func TestWriteConfig(t *testing.T) {
        _ = 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)
+       err := fs.Remove(filename)
        assert.Equal(t, err == nil, true)
 }
 

Reply via email to