Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove write file config code #5460

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 0 additions & 100 deletions backend/core/config/config_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down
81 changes: 2 additions & 79 deletions backend/core/config/config_viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
24 changes: 8 additions & 16 deletions backend/test/helper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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")
}
}

Expand Down