diff --git a/config.go b/config.go index 14fd2b3..5b7f739 100644 --- a/config.go +++ b/config.go @@ -38,6 +38,22 @@ func NewConfig() Config { return cfg } +func (c Config) keyToGlobalOptions(key string) GlobalOption { + return GlobalOption(key) +} + +func (c Config) convertGlobals() { + if globals, ok := c[Globals]; ok { + if settings, ok := globals.(map[string]interface{}); ok { + validGlobals := map[GlobalOption]string{} + for k, v := range settings { + validGlobals[c.keyToGlobalOptions(k)] = fmt.Sprintf("%v", v) + } + c[Globals] = validGlobals + } + } +} + // ReadFrom implements the io.ReaderFrom interface. This // should be used with io.Reader to load configuration from //file or from string etc. @@ -49,6 +65,7 @@ func (c Config) ReadFrom(r io.Reader) (int64, error) { if err = json.Unmarshal(data, &c); err != nil { return int64(len(data)), err } + c.convertGlobals() return int64(len(data)), nil } @@ -87,7 +104,6 @@ func (c Config) GetGlobal(option GlobalOption) (string, error) { } } return "", fmt.Errorf("no global config options found") - } // SetGlobal associates a value with a global configuration option diff --git a/config_test.go b/config_test.go index 3bc14d6..5ae4911 100644 --- a/config_test.go +++ b/config_test.go @@ -2,6 +2,7 @@ package gosec_test import ( "bytes" + "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -106,6 +107,36 @@ var _ = Describe("Configuration", func() { Expect(err).Should(BeNil()) Expect(enabled).Should(BeTrue()) }) - }) + It("should parse the global settings of type string from file", func() { + config := ` + { + "global": { + "nosec": "enabled" + } + }` + cfg := gosec.NewConfig() + _, err := cfg.ReadFrom(strings.NewReader(config)) + Expect(err).Should(BeNil()) + + value, err := cfg.GetGlobal(gosec.Nosec) + Expect(err).Should(BeNil()) + Expect(value).Should(Equal("enabled")) + }) + It("should parse the global settings of other types from file", func() { + config := ` + { + "global": { + "nosec": true + } + }` + cfg := gosec.NewConfig() + _, err := cfg.ReadFrom(strings.NewReader(config)) + Expect(err).Should(BeNil()) + + value, err := cfg.GetGlobal(gosec.Nosec) + Expect(err).Should(BeNil()) + Expect(value).Should(Equal("true")) + }) + }) })