Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Removed reading of config file multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanyfay committed Feb 18, 2016
1 parent bfc2cb7 commit f87bd4f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 42 deletions.
29 changes: 10 additions & 19 deletions control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strconv"

Expand Down Expand Up @@ -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")
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion control/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"})
Expand Down
50 changes: 50 additions & 0 deletions pkg/globalconfig/config.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 10 additions & 19 deletions pkg/globalconfig/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package globalconfig

import (
"encoding/json"
"io/ioutil"

log "github.com/Sirupsen/logrus"

Expand Down Expand Up @@ -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")
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/globalconfig/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f87bd4f

Please sign in to comment.