forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apolloconfig#99 from lj99boy/withRawBackup
增加备份原始namespace功能
- Loading branch information
Showing
27 changed files
with
268 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ | |
application*.json | ||
/abc1.json | ||
/env_test.properties | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package file | ||
|
||
import ( | ||
"github.com/zouyx/agollo/v3/env" | ||
) | ||
|
||
//FileHandler 备份文件读写 | ||
type FileHandler interface { | ||
WriteConfigFile(config *env.ApolloConfig, configPath string) error | ||
GetConfigFile(configDir string, namespace string) string | ||
LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package json | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/zouyx/agollo/v3/component/log" | ||
"github.com/zouyx/agollo/v3/env" | ||
jsonConfig "github.com/zouyx/agollo/v3/env/config/json" | ||
"github.com/zouyx/agollo/v3/extension" | ||
) | ||
|
||
//Suffix 默认文件保存类型 | ||
const Suffix = ".json" | ||
|
||
func init() { | ||
extension.SetFileHandler(&JSONFileHandler{}) | ||
} | ||
|
||
var ( | ||
//jsonFileConfig 处理文件的json格式存取 | ||
jsonFileConfig = &jsonConfig.ConfigFile{} | ||
//configFileMap 存取namespace文件地址 | ||
configFileMap = make(map[string]string, 1) | ||
) | ||
|
||
//JSONFileHandler 默认备份文件读写 | ||
type JSONFileHandler struct { | ||
} | ||
|
||
//WriteConfigFile write config to file | ||
func (fileHandler *JSONFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error { | ||
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.NamespaceName)) | ||
} | ||
|
||
//GetConfigFile get real config file | ||
func (fileHandler *JSONFileHandler) GetConfigFile(configDir string, namespace string) string { | ||
fullPath := configFileMap[namespace] | ||
if fullPath == "" { | ||
filePath := fmt.Sprintf("%s%s", namespace, Suffix) | ||
if configDir != "" { | ||
configFileMap[namespace] = fmt.Sprintf("%s/%s", configDir, filePath) | ||
} else { | ||
configFileMap[namespace] = filePath | ||
} | ||
} | ||
return configFileMap[namespace] | ||
} | ||
|
||
//LoadConfigFile load config from file | ||
func (fileHandler *JSONFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) { | ||
configFilePath := fileHandler.GetConfigFile(configDir, namespace) | ||
log.Info("load config file from :", configFilePath) | ||
c, e := jsonFileConfig.Load(configFilePath, func(b []byte) (interface{}, error) { | ||
config := &env.ApolloConfig{} | ||
e := json.NewDecoder(bytes.NewBuffer(b)).Decode(config) | ||
return config, e | ||
}) | ||
|
||
if c == nil || e != nil { | ||
log.Errorf("loadConfigFile fail,error:", e) | ||
return nil, e | ||
} | ||
|
||
return c.(*env.ApolloConfig), e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.