-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish #29
- Loading branch information
Showing
11 changed files
with
176 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package agollo | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
const FILE = "apolloConfig.json" | ||
var configFile="" | ||
|
||
//write config to file | ||
func writeConfigFile(config *ApolloConfig,configPath string)error{ | ||
if config==nil{ | ||
logger.Error("apollo config is null can not write backup file") | ||
return errors.New("apollo config is null can not write backup file") | ||
} | ||
file, e := os.Create(getConfigFile(configPath)) | ||
defer file.Close() | ||
if e!=nil{ | ||
logger.Errorf("writeConfigFile fail,error:",e) | ||
return e | ||
} | ||
|
||
return json.NewEncoder(file).Encode(config) | ||
} | ||
|
||
//get real config file | ||
func getConfigFile(configDir string) string { | ||
if configFile == "" { | ||
if configDir!="" { | ||
configFile=fmt.Sprintf("%s/%s",configDir,FILE) | ||
}else{ | ||
configFile=FILE | ||
} | ||
|
||
} | ||
return configFile | ||
} | ||
|
||
//load config from file | ||
func loadConfigFile(configDir string) (*ApolloConfig,error){ | ||
configFilePath := getConfigFile(configDir) | ||
logger.Info("load config file from :",configFilePath) | ||
file, e := os.Open(configFilePath) | ||
defer file.Close() | ||
if e!=nil{ | ||
logger.Errorf("loadConfigFile fail,error:",e) | ||
return nil,e | ||
} | ||
config:=&ApolloConfig{} | ||
e=json.NewDecoder(file).Decode(config) | ||
|
||
if e!=nil{ | ||
logger.Errorf("loadConfigFile fail,error:",e) | ||
return nil,e | ||
} | ||
|
||
return config,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package agollo | ||
|
||
import ( | ||
"github.com/zouyx/agollo/test" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestWriteConfigFile(t *testing.T) { | ||
configPath:="" | ||
os.Remove(getConfigFile(configPath)) | ||
jsonStr := `{ | ||
"appId": "100004458", | ||
"cluster": "default", | ||
"namespaceName": "application", | ||
"configurations": { | ||
"key1":"value1", | ||
"key2":"value2" | ||
}, | ||
"releaseKey": "20170430092936-dee2d58e74515ff3" | ||
}` | ||
|
||
config, err := createApolloConfigWithJson([]byte(jsonStr)) | ||
|
||
isNil(err) | ||
e := writeConfigFile(config,configPath) | ||
isNil(e) | ||
} | ||
|
||
func TestLoadConfigFile(t *testing.T) { | ||
jsonStr := `{ | ||
"appId": "100004458", | ||
"cluster": "default", | ||
"namespaceName": "application", | ||
"configurations": { | ||
"key1":"value1", | ||
"key2":"value2" | ||
}, | ||
"releaseKey": "20170430092936-dee2d58e74515ff3" | ||
}` | ||
|
||
config, err := createApolloConfigWithJson([]byte(jsonStr)) | ||
|
||
isNil(err) | ||
newConfig,e := loadConfigFile("") | ||
|
||
t.Log(newConfig) | ||
isNil(e) | ||
test.Equal(t,config.AppId,newConfig.AppId) | ||
test.Equal(t,config.ReleaseKey,newConfig.ReleaseKey) | ||
test.Equal(t,config.Cluster,newConfig.Cluster) | ||
test.Equal(t,config.NamespaceName,newConfig.NamespaceName) | ||
} |
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