mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
Add test case for load settings
This commit is contained in:
parent
78f33ffed7
commit
24d26c94be
12
godns.go
12
godns.go
@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
@ -32,7 +34,15 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
Configuration = LoadSettings(*optConf)
|
||||
var err error
|
||||
Configuration, err = LoadSettings(*optConf)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
log.Println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
dns_loop()
|
||||
}
|
||||
|
||||
|
13
settings.go
13
settings.go
@ -4,13 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
Email string
|
||||
Password string
|
||||
LoginToken string
|
||||
LoginToken string
|
||||
Domain string
|
||||
Sub_domain string
|
||||
IP_Url string
|
||||
@ -21,19 +20,19 @@ type Settings struct {
|
||||
Group int
|
||||
}
|
||||
|
||||
func LoadSettings(config_path string) Settings {
|
||||
func LoadSettings(config_path string) (Settings, error) {
|
||||
setting := Settings{}
|
||||
file, err := ioutil.ReadFile(config_path)
|
||||
if err != nil {
|
||||
fmt.Println("Error occurs while reading config file, please make sure config file exists!")
|
||||
os.Exit(1)
|
||||
return setting, err
|
||||
}
|
||||
|
||||
var setting Settings
|
||||
err = json.Unmarshal(file, &setting)
|
||||
if err != nil {
|
||||
fmt.Println("Error occurs while unmarshal config file, please make sure config file correct!")
|
||||
os.Exit(1)
|
||||
return setting, err
|
||||
}
|
||||
|
||||
return setting
|
||||
return setting, nil
|
||||
}
|
||||
|
17
settings_test.go
Normal file
17
settings_test.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadSetting(t *testing.T) {
|
||||
settings, err := LoadSettings("./config_sample.json")
|
||||
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
|
||||
if settings.IP_Url == "" {
|
||||
t.Error("Cannot load ip_url from config file")
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user