diff options
| -rw-r--r-- | corecfg/utils.go | 15 | ||||
| -rw-r--r-- | corecfg/utils_test.go | 12 |
2 files changed, 15 insertions, 12 deletions
diff --git a/corecfg/utils.go b/corecfg/utils.go index f6018b75b9..c666d139d1 100644 --- a/corecfg/utils.go +++ b/corecfg/utils.go @@ -32,15 +32,18 @@ var rx = regexp.MustCompile(`^[ \t]*(#?)[ \t#]*([a-z_]+)=(.*)$`) // updateKeyValueStream updates simple key=value files with comments. // Example for such formats are: /etc/environment or /boot/uboot/config.txt // -// An r io.Reader and a configuration "patch" is taken as input, the r is -// read line-by-line and any line and any required configuration change from -// the "config" input is applied. If changes need to be written a []string +// An r io.Reader, map of supported config keys and a configuration +// "patch" is taken as input, the r is read line-by-line and any line +// and any required configuration change from the "config" input is +// applied. +// +// If changes need to be written a []string // that contains the full file is returned. On error an error is returned. -func updateKeyValueStream(r io.Reader, allConfig map[string]bool, newConfig map[string]string) (toWrite []string, err error) { +func updateKeyValueStream(r io.Reader, supportedConfigKeys map[string]bool, newConfig map[string]string) (toWrite []string, err error) { cfgKeys := make([]string, len(newConfig)) i := 0 for k := range newConfig { - if !allConfig[k] { + if !supportedConfigKeys[k] { return nil, fmt.Errorf("cannot set unsupported configuration value %q", k) } cfgKeys[i] = k @@ -55,7 +58,7 @@ func updateKeyValueStream(r io.Reader, allConfig map[string]bool, newConfig map[ for scanner.Scan() { line := scanner.Text() matches := rx.FindStringSubmatch(line) - if len(matches) > 0 && allConfig[matches[2]] { + if len(matches) > 0 && supportedConfigKeys[matches[2]] { wasComment := (matches[1] == "#") key := matches[2] oldValue := matches[3] diff --git a/corecfg/utils_test.go b/corecfg/utils_test.go index e13a8eaf0f..7283b4a1fe 100644 --- a/corecfg/utils_test.go +++ b/corecfg/utils_test.go @@ -34,9 +34,9 @@ var _ = Suite(&utilsSuite{}) func (s *utilsSuite) TestUpdateKeyValueStreamNoNewConfig(c *C) { in := bytes.NewBufferString("foo=bar") newConfig := map[string]string{} - allConfig := map[string]bool{} + supportedConfigKeys := map[string]bool{} - toWrite, err := corecfg.UpdateKeyValueStream(in, allConfig, newConfig) + toWrite, err := corecfg.UpdateKeyValueStream(in, supportedConfigKeys, newConfig) c.Check(err, IsNil) c.Check(toWrite, IsNil) } @@ -44,22 +44,22 @@ func (s *utilsSuite) TestUpdateKeyValueStreamNoNewConfig(c *C) { func (s *utilsSuite) TestUpdateKeyValueStreamConfigNotInAllConfig(c *C) { in := bytes.NewBufferString("") newConfig := map[string]string{"unsupported-options": "cannot be set"} - allConfig := map[string]bool{ + supportedConfigKeys := map[string]bool{ "foo": true, } - _, err := corecfg.UpdateKeyValueStream(in, allConfig, newConfig) + _, err := corecfg.UpdateKeyValueStream(in, supportedConfigKeys, newConfig) c.Check(err, ErrorMatches, `cannot set unsupported configuration value \"unsupported-options"`) } func (s *utilsSuite) TestUpdateKeyValueStreamOneChange(c *C) { in := bytes.NewBufferString("foo=bar") newConfig := map[string]string{"foo": "baz"} - allConfig := map[string]bool{ + supportedConfigKeys := map[string]bool{ "foo": true, } - toWrite, err := corecfg.UpdateKeyValueStream(in, allConfig, newConfig) + toWrite, err := corecfg.UpdateKeyValueStream(in, supportedConfigKeys, newConfig) c.Check(err, IsNil) c.Check(toWrite, DeepEquals, []string{"foo=baz"}) } |
