From f87bd4f80dd4b5802c9ebe1a6fe19bb731553355 Mon Sep 17 00:00:00 2001 From: Tiffany Jernigan Date: Fri, 12 Feb 2016 11:04:04 -0800 Subject: [PATCH] Removed reading of config file multiple times --- control/config.go | 29 +++++++------------- control/config_test.go | 5 +++- pkg/globalconfig/config.go | 50 ++++++++++++++++++++++++++++++++++ pkg/globalconfig/flags.go | 29 +++++++------------- pkg/globalconfig/flags_test.go | 4 ++- snapd.go | 5 ++-- 6 files changed, 80 insertions(+), 42 deletions(-) create mode 100644 pkg/globalconfig/config.go diff --git a/control/config.go b/control/config.go index 2218f9dda..e74d565d9 100644 --- a/control/config.go +++ b/control/config.go @@ -23,7 +23,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "reflect" "strconv" @@ -81,24 +80,16 @@ func newPluginConfig() *pluginConfig { } } -func (p *config) LoadConfig(path string) { - b, err := ioutil.ReadFile(path) - if err != nil { - log.WithFields(log.Fields{ - "block": "LoadConfig", - "_module": "control-config", - "error": err.Error(), - "path": path, - }).Fatal("unable to read config") - } - err = json.Unmarshal(b, p) - if err != nil { - log.WithFields(log.Fields{ - "block": "LoadConfig", - "_module": "control-config", - "error": err.Error(), - "path": path, - }).Fatal("invalid config") +func (p *config) LoadConfig(b []byte, cfg map[string]interface{}) { + if _, ok := cfg["plugins"]; ok { + err := json.Unmarshal(b, p) + if err != nil { + log.WithFields(log.Fields{ + "block": "LoadConfig", + "_module": "control-config", + "error": err.Error(), + }).Fatal("invalid config") + } } } diff --git a/control/config_test.go b/control/config_test.go index 8ae958476..b09947665 100644 --- a/control/config_test.go +++ b/control/config_test.go @@ -25,6 +25,7 @@ import ( "github.com/intelsdi-x/snap/core" "github.com/intelsdi-x/snap/core/cdata" "github.com/intelsdi-x/snap/core/ctypes" + "github.com/intelsdi-x/snap/pkg/globalconfig" . "github.com/smartystreets/goconvey/convey" ) @@ -59,7 +60,9 @@ func TestPluginConfig(t *testing.T) { Convey("Provided a config in JSON we are able to unmarshal it into a valid config", t, func() { cfg := NewConfig() - cfg.LoadConfig("../examples/configs/snap-config-sample.json") + path := "../examples/configs/snap-config-sample.json" + b, config := globalconfig.Read(path) + cfg.LoadConfig(b, config) So(cfg.Plugins, ShouldNotBeNil) So(cfg.Plugins.All, ShouldNotBeNil) So(cfg.Plugins.All.Table()["password"], ShouldResemble, ctypes.ConfigValueStr{Value: "p@ssw0rd"}) diff --git a/pkg/globalconfig/config.go b/pkg/globalconfig/config.go new file mode 100644 index 000000000..de1f86b1d --- /dev/null +++ b/pkg/globalconfig/config.go @@ -0,0 +1,50 @@ +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 2015 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package globalconfig + +import ( + "encoding/json" + "io/ioutil" + + log "github.com/Sirupsen/logrus" +) + +// Read reads the JSON file and unmarshals it into a map[string]interface{} +func Read(path string) ([]byte, map[string]interface{}) { + b, err := ioutil.ReadFile(path) + if err != nil { + log.WithFields(log.Fields{ + "block": "Read", + "_module": "globalconfig", + "error": err.Error(), + "path": path, + }).Fatal("unable to read config") + } + var cfg map[string]interface{} + err = json.Unmarshal(b, &cfg) + if err != nil { + log.WithFields(log.Fields{ + "block": "Read", + "_module": "globalconfig", + "error": err.Error(), + }).Fatal("invalid config") + } + return b, cfg +} diff --git a/pkg/globalconfig/flags.go b/pkg/globalconfig/flags.go index fc2d62fc4..385b6bf5c 100644 --- a/pkg/globalconfig/flags.go +++ b/pkg/globalconfig/flags.go @@ -21,7 +21,6 @@ package globalconfig import ( "encoding/json" - "io/ioutil" log "github.com/Sirupsen/logrus" @@ -59,24 +58,16 @@ func NewConfig() *config { return &config{} } -func (f *config) LoadConfig(path string) { - b, err := ioutil.ReadFile(path) - if err != nil { - log.WithFields(log.Fields{ - "block": "LoadConfig", - "_module": "flags", - "error": err.Error(), - "path": path, - }).Fatal("unable to read config") - } - err = json.Unmarshal(b, &f) - if err != nil { - log.WithFields(log.Fields{ - "block": "LoadConfig", - "_module": "flags", - "error": err.Error(), - "path": path, - }).Fatal("invalid config") +func (f *config) LoadConfig(b []byte, cfg map[string]interface{}) { + if _, ok := cfg["flags"]; ok { + err := json.Unmarshal(b, &f) + if err != nil { + log.WithFields(log.Fields{ + "block": "LoadConfig", + "_module": "globalconfig", + "error": err.Error(), + }).Fatal("invalid config") + } } } diff --git a/pkg/globalconfig/flags_test.go b/pkg/globalconfig/flags_test.go index 3b11bf3b3..eb92b6bc0 100644 --- a/pkg/globalconfig/flags_test.go +++ b/pkg/globalconfig/flags_test.go @@ -28,7 +28,9 @@ import ( func TestFlags(t *testing.T) { Convey("Provided a config in JSON we are able to unmarshal it into a valid config", t, func() { cfg := NewConfig() - cfg.LoadConfig("../../examples/configs/snap-config-sample.json") + path := "../../examples/configs/snap-config-sample.json" + b, config := Read(path) + cfg.LoadConfig(b, config) So(cfg.Flags, ShouldNotBeNil) So(cfg.Flags.LogLevel, ShouldNotBeNil) So(cfg.Flags.PluginTrust, ShouldNotBeNil) diff --git a/snapd.go b/snapd.go index 97b752184..86a55378d 100644 --- a/snapd.go +++ b/snapd.go @@ -197,8 +197,9 @@ func action(ctx *cli.Context) { ccfg := control.NewConfig() fpath := ctx.String("config") if fpath != "" { - fcfg.LoadConfig(fpath) - ccfg.LoadConfig(fpath) + b, cfg := globalconfig.Read(fpath) + fcfg.LoadConfig(b, cfg) + ccfg.LoadConfig(b, cfg) } // Get flag values