mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
fix: 解决env文件保存的时候特殊字符被加上转义符的BUG
This commit is contained in:
parent
275dfc6904
commit
0c6b52ed4c
@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/env"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
|
||||
"github.com/joho/godotenv"
|
||||
"io/ioutil"
|
||||
@ -236,7 +237,7 @@ func (a AppInstallService) Update(req request.AppInstalledUpdate) error {
|
||||
return err
|
||||
}
|
||||
installed.Env = string(paramByte)
|
||||
if err := godotenv.Write(oldEnvMaps, envPath); err != nil {
|
||||
if err := env.Write(oldEnvMaps, envPath); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = appInstallRepo.Save(&installed)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/env"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
@ -22,7 +23,6 @@ import (
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@ -326,7 +326,7 @@ func copyAppData(key, version, installName string, params map[string]interface{}
|
||||
|
||||
envParams := make(map[string]string, len(params))
|
||||
handleMap(params, envParams)
|
||||
if err = godotenv.Write(envParams, envPath); err != nil {
|
||||
if err = env.Write(envParams, envPath); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
@ -525,7 +525,7 @@ func updateToolApp(installed model.AppInstall) {
|
||||
}
|
||||
envFileMap := make(map[string]string)
|
||||
handleMap(envMap, envFileMap)
|
||||
if err = godotenv.Write(envFileMap, envPath); err != nil {
|
||||
if err = env.Write(envFileMap, envPath); err != nil {
|
||||
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, err.Error())
|
||||
return
|
||||
}
|
||||
|
39
backend/utils/env/env.go
vendored
Normal file
39
backend/utils/env/env.go
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Write(envMap map[string]string, filename string) error {
|
||||
content, err := Marshal(envMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = file.WriteString(content + "\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return file.Sync()
|
||||
}
|
||||
|
||||
func Marshal(envMap map[string]string) (string, error) {
|
||||
lines := make([]string, 0, len(envMap))
|
||||
for k, v := range envMap {
|
||||
if d, err := strconv.Atoi(v); err == nil {
|
||||
lines = append(lines, fmt.Sprintf(`%s=%d`, k, d))
|
||||
} else {
|
||||
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, v))
|
||||
}
|
||||
}
|
||||
sort.Strings(lines)
|
||||
return strings.Join(lines, "\n"), nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user