This is an automated email from the ASF dual-hosted git repository.
zhangliang2022 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 8cfe2b8a4 fix: remove write file config code (#5460)
8cfe2b8a4 is described below
commit 8cfe2b8a46a7b609a42527fd893e5bc09b43698f
Author: abeizn <[email protected]>
AuthorDate: Tue Jun 13 15:06:07 2023 +0800
fix: remove write file config code (#5460)
---
backend/core/config/config_viper.go | 100 -------------------------------
backend/core/config/config_viper_test.go | 81 +------------------------
backend/test/helper/client.go | 24 +++-----
3 files changed, 10 insertions(+), 195 deletions(-)
diff --git a/backend/core/config/config_viper.go
b/backend/core/config/config_viper.go
index dae4b5153..d718506e1 100644
--- a/backend/core/config/config_viper.go
+++ b/backend/core/config/config_viper.go
@@ -21,15 +21,9 @@ import (
"fmt"
"os"
"path/filepath"
- "regexp"
- "strings"
- "github.com/apache/incubator-devlake/core/errors"
"github.com/sirupsen/logrus"
- goerror "github.com/cockroachdb/errors"
-
- "github.com/spf13/afero"
"github.com/spf13/viper"
)
@@ -114,100 +108,6 @@ func setDefaultValue(v *viper.Viper) {
v.SetDefault("SWAGGER_DOCS_DIR", "resources/swagger")
}
-// replaceNewEnvItemInOldContent replace old config to new config in env file
content
-func replaceNewEnvItemInOldContent(v *viper.Viper, envFileContent string)
(string, errors.Error) {
- // prepare reg exp
- encodeEnvNameReg := regexp.MustCompile(`[^a-zA-Z0-9]`)
- if encodeEnvNameReg == nil {
- return ``, errors.Default.New("encodeEnvNameReg err")
- }
-
- for _, key := range v.AllKeys() {
- envName := strings.ToUpper(key)
- val := v.Get(envName)
- encodeEnvName := encodeEnvNameReg.ReplaceAllStringFunc(envName,
func(s string) string {
- return fmt.Sprintf(`\%v`, s)
- })
- envItemReg, err :=
regexp.Compile(fmt.Sprintf(`(?im)^\s*%v\s*\=.*$`, encodeEnvName))
- if err != nil {
- return ``, errors.Default.Wrap(err, "regexp Compile
failed")
- }
- envFileContent =
envItemReg.ReplaceAllStringFunc(envFileContent, func(s string) string {
- switch ret := val.(type) {
- case string:
- ret = strings.Replace(ret, `\`, `\\`, -1)
- //ret = strings.Replace(ret, `=`, `\=`, -1)
- //ret = strings.Replace(ret, `'`, `\'`, -1)
- ret = strings.Replace(ret, `"`, `\"`, -1)
- return fmt.Sprintf(`%v="%v"`, envName, ret)
- default:
- if val == nil {
- return fmt.Sprintf(`%v=`, envName)
- }
- return fmt.Sprintf(`%v="%v"`, envName, ret)
- }
- })
- }
- return envFileContent, nil
-}
-
-// WriteConfig save viper to .env file
-func WriteConfig(v *viper.Viper) errors.Error {
- envPath := getEnvPath()
- fileName := getConfigName()
-
- if envPath != "" {
- fileName = envPath + string(os.PathSeparator) + fileName
- }
-
- return WriteConfigAs(v, fileName)
-}
-
-// WriteConfigAs save viper to custom filename
-func WriteConfigAs(v *viper.Viper, filename string) errors.Error {
- aferoFile := afero.NewOsFs()
- fmt.Println("Attempting to write configuration to .env file.")
- var configType string
-
- ext := filepath.Ext(filename)
- if ext != "" {
- configType = ext[1:]
- }
- if configType != "env" && configType != "dotenv" {
- return errors.Convert(v.WriteConfigAs(filename))
- }
-
- // FIXME viper just have setter and have no getter so create new
configPermissions and file
- flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
- configPermissions := os.FileMode(0644)
- file, err := afero.ReadFile(aferoFile, filename)
- if err != nil && !goerror.Is(err, os.ErrNotExist) {
- return errors.Convert(err)
- }
-
- envFileContent := string(file)
- f, err := aferoFile.OpenFile(filename, flags, configPermissions)
- if err != nil {
- return errors.Convert(err)
- }
- defer f.Close()
-
- for _, key := range v.AllKeys() {
- envName := strings.ToUpper(key)
- if !strings.Contains(envFileContent, envName) {
- envFileContent = fmt.Sprintf("%s\n%s=", envFileContent,
envName)
- }
- }
- envFileContent, err = replaceNewEnvItemInOldContent(v, envFileContent)
- if err != nil {
- return errors.Convert(err)
- }
- if _, err := f.WriteString(envFileContent); err != nil {
- return errors.Convert(err)
- }
- return errors.Convert(f.Sync())
-}
-
func init() {
// create the object and load the .env file
v = viper.New()
diff --git a/backend/core/config/config_viper_test.go
b/backend/core/config/config_viper_test.go
index a48a94899..a6806ee79 100644
--- a/backend/core/config/config_viper_test.go
+++ b/backend/core/config/config_viper_test.go
@@ -18,46 +18,12 @@ limitations under the License.
package config
import (
+ "testing"
+
"github.com/sirupsen/logrus"
- "github.com/spf13/afero"
"github.com/stretchr/testify/assert"
- "os"
- "testing"
)
-func TestWriteConfig(t *testing.T) {
- filename := ".env"
- cwd, _ := os.Getwd()
- envFilePath := cwd + string(os.PathSeparator)
- 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)
- 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"
- 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 TestSetConfigVariate(t *testing.T) {
v := GetConfig()
newDbUrl :=
"mysql://merico:merico@mysql:3307/lake?charset=utf8mb4&parseTime=True"
@@ -66,46 +32,3 @@ func TestSetConfigVariate(t *testing.T) {
logrus.Infof("current db url: %s\n", currentDbUrl)
assert.Equal(t, currentDbUrl == newDbUrl, true)
}
-
-func TestReplaceNewEnvItemInOldContent(t *testing.T) {
- v := GetConfig()
- v.Set(`aa`, `aaaa`)
- v.Set(`bb`, `1#1`)
- v.Set(`cc`, `1"'1`)
- v.Set(`dd`, `1\"1`)
- v.Set(`ee`, `=`)
- v.Set(`ff`, 1.01)
- v.Set(`gGg`, `gggg`)
- v.Set(`h.278`, 278)
- s, err := 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)
- }
- 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)
-}
diff --git a/backend/test/helper/client.go b/backend/test/helper/client.go
index 7c0f6a5ea..320dee298 100644
--- a/backend/test/helper/client.go
+++ b/backend/test/helper/client.go
@@ -23,11 +23,6 @@ import (
"encoding/json"
goerror "errors"
"fmt"
- "github.com/apache/incubator-devlake/core/dal"
- dora "github.com/apache/incubator-devlake/plugins/dora/impl"
- org "github.com/apache/incubator-devlake/plugins/org/impl"
- refdiff "github.com/apache/incubator-devlake/plugins/refdiff/impl"
- remotePlugin
"github.com/apache/incubator-devlake/server/services/remote/plugin"
"io"
"math"
"net/http"
@@ -37,6 +32,12 @@ import (
"testing"
"time"
+ "github.com/apache/incubator-devlake/core/dal"
+ dora "github.com/apache/incubator-devlake/plugins/dora/impl"
+ org "github.com/apache/incubator-devlake/plugins/org/impl"
+ refdiff "github.com/apache/incubator-devlake/plugins/refdiff/impl"
+ remotePlugin
"github.com/apache/incubator-devlake/server/services/remote/plugin"
+
"github.com/apache/incubator-devlake/core/config"
corectx "github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
@@ -219,17 +220,8 @@ func (d *DevlakeClient) configureEncryption() {
encryptionSecret := v.GetString(plugin.EncodeKeyEnvStr)
// only test environment should have this set
if encryptionSecret == "" {
- var err errors.Error
- // Randomly generate a bunch of encryption keys and set them to
config
- encryptionSecret, err = plugin.RandomEncryptionSecret()
- if err != nil {
- panic(err)
- }
- v.Set(plugin.EncodeKeyEnvStr, encryptionSecret)
- err = config.WriteConfig(v)
- if err != nil {
- panic(err)
- }
+ // default value
+ v.Set(plugin.EncodeKeyEnvStr,
"DFLFZLMBBFDDCYWRECDCIYUROPPAKQDFQMMJEFPIKVFVHZBRGAZIHKRJIJZMOHWEVRSCETAGGONPSULGOXITVXISVCQGPSFAOGRDLUANEYDQFBDKVMYYHUZFHYVYGPPT")
}
}