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: backup file has no content #312

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions env/config/json/json_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (
"github.com/apolloconfig/agollo/v4/utils"
)

//ConfigFile json文件读写
// ConfigFile json文件读写
type ConfigFile struct {
}

//Load json文件读
// Load json文件读
func (t *ConfigFile) Load(fileName string, unmarshal func([]byte) (interface{}, error)) (interface{}, error) {
fs, err := ioutil.ReadFile(fileName)
if err != nil {
Expand All @@ -47,7 +47,7 @@ func (t *ConfigFile) Load(fileName string, unmarshal func([]byte) (interface{},
return config, nil
}

//Write json文件写
// Write json文件写
func (t *ConfigFile) Write(content interface{}, configPath string) error {
if content == nil {
log.Error("content is null can not write backup file")
Expand All @@ -62,3 +62,13 @@ func (t *ConfigFile) Write(content interface{}, configPath string) error {

return json.NewEncoder(file).Encode(content)
}

// WriteWithBackup writes the given content to a new file, then renames the new file to the original file name.
func (t *ConfigFile) WriteWithBackup(content interface{}, configPath string) error {
newConfigPath := configPath + ".new"
err := t.Write(content, newConfigPath)
if err != nil {
return err
}
return os.Rename(newConfigPath, configPath)
}
23 changes: 23 additions & 0 deletions env/config/json/json_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,26 @@ func TestJSONConfigFile_Write_error(t *testing.T) {
Assert(t, e, NotNilVal())
Assert(t, file, NilVal())
}

func TestJSONConfigFile_WriteWithBackup(t *testing.T) {
fileName := "test.json"
jsonConfigFile.WriteWithBackup(`{"appId":"100004458","cluster":"default","namespaceName":"application","releaseKey":"20170430092936-dee2d58e74515ff3","configurations":{"key1":"value1","key2":"value2"}}`, fileName)
file, e := os.Open(fileName)
Assert(t, e, NilVal())
Assert(t, file, NotNilVal())
file.Close()
os.Remove(fileName)
}

func TestJSONConfigFile_WriteWithBackup_error(t *testing.T) {
fileName := "/a/a/a/a//s.k"
e := jsonConfigFile.WriteWithBackup(`{"appId":"100004458","cluster":"default","namespaceName":"application","releaseKey":"20170430092936-dee2d58e74515ff3","configurations":{"key1":"value1","key2":"value2"}}`, fileName)
file, _ := os.Open(fileName)
Assert(t, e, NotNilVal())
Assert(t, file, NilVal())

e = jsonConfigFile.Write(``, fileName)
file, _ = os.Open(fileName)
Assert(t, e, NotNilVal())
Assert(t, file, NilVal())
}
9 changes: 5 additions & 4 deletions env/file/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/apolloconfig/agollo/v4/env/config"
"os"
"sync"

"github.com/apolloconfig/agollo/v4/env/config"

"github.com/apolloconfig/agollo/v4/component/log"
jsonConfig "github.com/apolloconfig/agollo/v4/env/config/json"
)

//Suffix 默认文件保存类型
// Suffix 默认文件保存类型
const Suffix = ".json"

var (
Expand Down Expand Up @@ -75,7 +76,7 @@ func (fileHandler *FileHandler) WriteConfigFile(config *config.ApolloConfig, con
if err != nil {
return err
}
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.AppID, config.NamespaceName))
return jsonFileConfig.WriteWithBackup(config, fileHandler.GetConfigFile(configPath, config.AppID, config.NamespaceName))
}

// GetConfigFile get real config file
Expand All @@ -95,7 +96,7 @@ func (fileHandler *FileHandler) GetConfigFile(configDir string, appID string, na
return configFileMap[namespace]
}

//LoadConfigFile load config from file
// LoadConfigFile load config from file
func (fileHandler *FileHandler) LoadConfigFile(configDir string, appID string, namespace string) (*config.ApolloConfig, error) {
configFilePath := fileHandler.GetConfigFile(configDir, appID, namespace)
log.Infof("load config file from: %s", configFilePath)
Expand Down
11 changes: 6 additions & 5 deletions env/file/json/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package json

import (
"fmt"
"github.com/apolloconfig/agollo/v4/component/log"
"github.com/apolloconfig/agollo/v4/env/config"
"os"
"sync"

"github.com/apolloconfig/agollo/v4/component/log"
"github.com/apolloconfig/agollo/v4/env/config"

"github.com/apolloconfig/agollo/v4/env/file"
)

Expand All @@ -32,7 +33,7 @@ var (
rawOnce sync.Once
)

//rawFileHandler 写入备份文件时,同时写入原始内容和namespace类型
// rawFileHandler 写入备份文件时,同时写入原始内容和namespace类型
type rawFileHandler struct {
*FileHandler
}
Expand All @@ -59,7 +60,7 @@ func writeWithRaw(config *config.ApolloConfig, configDir string) error {
return nil
}

//WriteConfigFile write config to file
// WriteConfigFile write config to file
func (fileHandler *rawFileHandler) WriteConfigFile(config *config.ApolloConfig, configPath string) error {
err := fileHandler.createDir(configPath)
if err != nil {
Expand All @@ -70,7 +71,7 @@ func (fileHandler *rawFileHandler) WriteConfigFile(config *config.ApolloConfig,
if err != nil {
log.Errorf("writeWithRaw fail! error:%v", err)
}
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.AppID, config.NamespaceName))
return jsonFileConfig.WriteWithBackup(config, fileHandler.GetConfigFile(configPath, config.AppID, config.NamespaceName))
}

// GetRawFileHandler 获取 rawFileHandler 实例
Expand Down
Loading