Split bootstrap into readConfig and writeConfig functions
This commit is contained in:
parent
16b8a8f4df
commit
605d3e5b50
|
@ -40,13 +40,22 @@ func bootstrap() {
|
|||
}
|
||||
if fileExist(configFile) {
|
||||
log.Println("Loading config file")
|
||||
readConfig()
|
||||
} else {
|
||||
log.Println("Config file not found, saving an empty one")
|
||||
writeConfig()
|
||||
}
|
||||
}
|
||||
|
||||
func readConfig() {
|
||||
body, err := readFile(configFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
update(body)
|
||||
} else {
|
||||
log.Println("Config file not found, saving an empty one")
|
||||
updateConfig(body)
|
||||
}
|
||||
|
||||
func writeConfig() {
|
||||
body, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -55,9 +64,8 @@ func bootstrap() {
|
|||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func update(body []byte) {
|
||||
func updateConfig(body []byte) {
|
||||
dupe := duplicate(config)
|
||||
if err := json.Unmarshal(body, dupe); err != nil {
|
||||
log.Println("Failed to update config")
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package confection2
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testConf struct {
|
||||
Foo string `json:"foo"`
|
||||
|
@ -28,11 +31,11 @@ func TestUnmarshal(t *testing.T) {
|
|||
conf := testConf{}
|
||||
var i interface{} = &conf
|
||||
|
||||
if err := unmarshal([]byte(badJSON), i); err == nil {
|
||||
if err := json.Unmarshal([]byte(badJSON), i); err == nil {
|
||||
t.Error("Expected error")
|
||||
}
|
||||
|
||||
if err := unmarshal([]byte(goodJSON), i); err != nil {
|
||||
if err := json.Unmarshal([]byte(goodJSON), i); err != nil {
|
||||
t.Error("Unexpected error")
|
||||
}
|
||||
if conf.Foo != "baz" {
|
||||
|
|
Loading…
Reference in New Issue