This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 5f57fcdee fix: env config reload logic (#5408)
5f57fcdee is described below

commit 5f57fcdee6211cfd300d14be5bfd5acd1742b9c2
Author: abeizn <[email protected]>
AuthorDate: Thu Jun 8 15:16:38 2023 +0800

    fix: env config reload logic (#5408)
    
    * fix: env config reload logic
    
    * fix: e2e github action
    
    * feat: add some tips and add mutil-loop var path
    
    * fix: github action e2e path
---
 .github/workflows/test-e2e.yml                |  4 ++--
 backend/core/config/config_viper.go           | 29 +++++++++++++++++----------
 backend/core/runner/db.go                     |  2 +-
 backend/helpers/e2ehelper/data_flow_tester.go |  2 +-
 4 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml
index 1bb3c0d3b..8d8f6a1f2 100644
--- a/.github/workflows/test-e2e.yml
+++ b/.github/workflows/test-e2e.yml
@@ -70,7 +70,7 @@ jobs:
           DB_URL: mysql://root:root@db:3306/lake?charset=utf8mb4&parseTime=True
           E2E_DB_URL: 
mysql://root:root@db:3306/lake?charset=utf8mb4&parseTime=True
         run: |
-          cp env.example backend/.env
+          cp env.example .env
           cd backend
           make e2e-plugins-test
           make e2e-test
@@ -109,7 +109,7 @@ jobs:
           DB_URL: postgres://merico:merico@db:5432/lake
           E2E_DB_URL: postgres://merico:merico@db:5432/lake
         run: |
-          cp env.example backend/.env
+          cp env.example .env
           cd backend
           make e2e-plugins-test
           make e2e-test
diff --git a/backend/core/config/config_viper.go 
b/backend/core/config/config_viper.go
index 57e7ee303..b0c09c83d 100644
--- a/backend/core/config/config_viper.go
+++ b/backend/core/config/config_viper.go
@@ -28,7 +28,6 @@ import (
 
        goerror "github.com/cockroachdb/errors"
 
-       "github.com/sirupsen/logrus"
        "github.com/spf13/afero"
        "github.com/spf13/viper"
 )
@@ -47,17 +46,34 @@ func GetConfig() *viper.Viper {
 func initConfig(v *viper.Viper) {
        v.SetConfigName(getConfigName())
        v.SetConfigType("env")
+       paths := []string{
+               "./../../../../..",
+               "./../../../..",
+               "./../../..",
+               "./../..",
+               "./../",
+               "./",
+       }
+       for _, path := range paths {
+               v.AddConfigPath(path)
+       }
 
        if envFile := os.Getenv("ENV_FILE"); envFile != "" {
                v.SetConfigFile(envFile)
        } else {
-               v.SetConfigFile("./.env")
+               v.SetConfigFile(".env")
        }
+
        if _, err := os.Stat(v.ConfigFileUsed()); err == nil {
                if err := v.ReadInConfig(); err != nil {
                        panic(fmt.Errorf("failed to read configuration file: 
%v", err))
                }
        }
+
+       v.AutomaticEnv()
+       setDefaultValue(v)
+       // This line is essential for reading
+       v.WatchConfig()
 }
 
 func getConfigName() string {
@@ -179,13 +195,4 @@ func init() {
        // create the object and load the .env file
        v = viper.New()
        initConfig(v)
-       err := v.ReadInConfig()
-       if err != nil {
-               logrus.Warn("Failed to read [.env] file:", err)
-       }
-       v.AutomaticEnv()
-
-       setDefaultValue(v)
-       // This line is essential for reading and writing
-       v.WatchConfig()
 }
diff --git a/backend/core/runner/db.go b/backend/core/runner/db.go
index abbfe2277..bec811059 100644
--- a/backend/core/runner/db.go
+++ b/backend/core/runner/db.go
@@ -77,7 +77,7 @@ func NewGormDbEx(configReader config.ConfigReader, logger 
log.Logger, sessionCon
        }
        dbUrl := configReader.GetString("DB_URL")
        if dbUrl == "" {
-               return nil, errors.BadInput.New("DB_URL is required")
+               return nil, errors.BadInput.New("DB_URL is required, please set 
it in environment variable or .env file")
        }
        u, err := url.Parse(dbUrl)
        if err != nil {
diff --git a/backend/helpers/e2ehelper/data_flow_tester.go 
b/backend/helpers/e2ehelper/data_flow_tester.go
index fca165a47..869ccbfff 100644
--- a/backend/helpers/e2ehelper/data_flow_tester.go
+++ b/backend/helpers/e2ehelper/data_flow_tester.go
@@ -101,7 +101,7 @@ func NewDataFlowTester(t *testing.T, pluginName string, 
pluginMeta plugin.Plugin
        cfg := config.GetConfig()
        e2eDbUrl := cfg.GetString(`E2E_DB_URL`)
        if e2eDbUrl == `` {
-               panic(errors.Default.New(`e2e can only run with E2E_DB_URL, 
please set it in .env`))
+               panic(errors.Default.New(`e2e can only run with E2E_DB_URL, 
please set it in environment variable or .env file`))
        }
        cfg.Set(`DB_URL`, cfg.GetString(`E2E_DB_URL`))
        db, err := runner.NewGormDb(cfg, logruslog.Global)

Reply via email to