diff --git a/confection.go b/confection.go index a88e9ad..9f8c11b 100644 --- a/confection.go +++ b/confection.go @@ -40,24 +40,32 @@ func bootstrap() { } if fileExist(configFile) { log.Println("Loading config file") - body, err := readFile(configFile) - if err != nil { - panic(err) - } - update(body) + readConfig() } else { log.Println("Config file not found, saving an empty one") - body, err := json.Marshal(config) - if err != nil { - panic(err) - } - if err = writeFile(configFile, body); err != nil { - panic(err) - } + writeConfig() } } -func update(body []byte) { +func readConfig() { + body, err := readFile(configFile) + if err != nil { + panic(err) + } + updateConfig(body) +} + +func writeConfig() { + body, err := json.Marshal(config) + if err != nil { + panic(err) + } + if err = writeFile(configFile, body); err != nil { + panic(err) + } +} + +func updateConfig(body []byte) { dupe := duplicate(config) if err := json.Unmarshal(body, dupe); err != nil { log.Println("Failed to update config") diff --git a/confection_test.go b/confection_test.go index b169327..e4b269d 100644 --- a/confection_test.go +++ b/confection_test.go @@ -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" {