1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

Refactor load settings func

This commit is contained in:
Timothy 2016-06-30 17:17:38 +08:00
commit 9787cf537b
3 changed files with 20 additions and 17 deletions

View File

@ -116,10 +116,10 @@ func getSubDomain(domainID int64, name string) (string, string) {
return "", ""
}
sjson, parse_err := simplejson.NewJson([]byte(response))
sjson, parseErr := simplejson.NewJson([]byte(response))
if parse_err != nil {
log.Println(parse_err)
if parseErr != nil {
log.Println(parseErr)
return "", ""
}
@ -144,11 +144,11 @@ func getSubDomain(domainID int64, name string) (string, string) {
return ret, ip
}
func updateIP(domain_id int64, sub_domain_id string, sub_domain_name string, ip string) {
func updateIP(domainID int64, subDomainID string, subDomainName string, ip string) {
value := url.Values{}
value.Add("domain_id", strconv.FormatInt(domain_id, 10))
value.Add("record_id", sub_domain_id)
value.Add("sub_domain", sub_domain_name)
value.Add("domain_id", strconv.FormatInt(domainID, 10))
value.Add("record_id", subDomainID)
value.Add("sub_domain", subDomainName)
value.Add("record_type", "A")
value.Add("record_line", "默认")
value.Add("value", ip)
@ -161,10 +161,10 @@ func updateIP(domain_id int64, sub_domain_id string, sub_domain_name string, ip
return
}
sjson, parse_err := simplejson.NewJson([]byte(response))
sjson, parseErr := simplejson.NewJson([]byte(response))
if parse_err != nil {
log.Println(parse_err)
if parseErr != nil {
log.Println(parseErr)
return
}
@ -183,7 +183,6 @@ func postData(url string, content url.Values) (string, error) {
req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", configuration.Email))
response, err := client.Do(req)
defer response.Body.Close()
if err != nil {
log.Println("Post failed...")
@ -191,6 +190,7 @@ func postData(url string, content url.Values) (string, error) {
return "", err
}
defer response.Body.Close()
resp, _ := ioutil.ReadAll(response.Body)
return string(resp), nil

View File

@ -21,19 +21,21 @@ type Settings struct {
Group int
}
func LoadSettings(config_path string, settings *Settings) error {
//LoadSettings -- Load settings from config file
func LoadSettings(configPath string, settings *Settings) error {
//LoadSettings from config file
setting := Settings{}
file, err := ioutil.ReadFile(config_path)
file, err := ioutil.ReadFile(configPath)
if err != nil {
fmt.Println("Error occurs while reading config file, please make sure config file exists!")
return setting, err
return err
}
err = json.Unmarshal(file, &setting)
if err != nil {
fmt.Println("Error occurs while unmarshal config file, please make sure config file correct!")
return setting, err
return err
}
return setting, nil
return nil
}

View File

@ -5,7 +5,8 @@ import (
)
func TestLoadSetting(t *testing.T) {
settings, err := LoadSettings("./config_sample.json")
var settings Settings
err := LoadSettings("./config_sample.json", &settings)
if err != nil {
t.Error(err.Error())