From 74dc289e0908a8724ddfa1b38fd5cd0b40f3b576 Mon Sep 17 00:00:00 2001 From: John Brooks Date: Tue, 2 Aug 2016 17:04:39 -0600 Subject: [PATCH] core: Prototype config parser --- core/config.go | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/core/config.go b/core/config.go index aa9a84e..63e13a8 100644 --- a/core/config.go +++ b/core/config.go @@ -1,12 +1,48 @@ package core import ( - "errors" + "encoding/json" + "io/ioutil" + "log" + "path/filepath" ) type Config struct { } -func LoadConfig(configPath string) (*Config, error) { - return nil, errors.New("Not implemented") +type ConfigRoot struct { + Contacts map[string]ConfigContact + Identity ConfigIdentity +} + +type ConfigContact struct { + Hostname string + LastConnected string + Nickname string + WhenCreated string +} + +type ConfigIdentity struct { + DataDirectory string + HostnameBlacklist []string + ServiceKey string +} + +func LoadConfig(configPath string) (*Config, error) { + configFile := filepath.Join(configPath, "ricochet.json") + configData, err := ioutil.ReadFile(configFile) + if err != nil { + log.Printf("Config read error from %s: %v", configFile, err) + return nil, err + } + + var root ConfigRoot + if err := json.Unmarshal(configData, &root); err != nil { + log.Printf("Config parse error: %v", err) + return nil, err + } + + log.Printf("Config: %v", root) + + return &Config{}, nil }