Skip to content

Skip empty shadow values when saving to file #294

@nandra

Description

@nandra

With below test store ini file is invalid:

[Match]
Name = "eth0"

[Network]
DNS=
DNS="1.2.3.4"
DNS="5.6.7.8"

Test method

func TestWriteSingleDnsSettings(t *testing.T) {
	const testPath = "/tmp/network.conf"

	ioutil.WriteFile(testPath, []byte{}, 0600)
	cfg, e := ini.LoadSources(ini.LoadOptions{AllowShadows: true}, testPath)
	if e != nil {
		fmt.Println("Failed to open config")
	}

	// add configs
	cfg.Section("Match").Key("Name").SetValue("eth0")

	for _, v := range []string{"1.2.3.4", "5.6.7.8"} {
		cfg.Section("Network").Key("DNS").AddShadow(v)
	}

	if e = cfg.SaveTo(testPath); e != nil {
		fmt.Println("Failed to write to config", e)
	}
}

And my fix to that is:

diff --git a/base/system-status/vendor/gopkg.in/ini.v1/key.go b/base/system-status/vendor/gopkg.in/ini.v1/key.go
index 8baafd9..0a28a04 100644
--- a/base/system-status/vendor/gopkg.in/ini.v1/key.go
+++ b/base/system-status/vendor/gopkg.in/ini.v1/key.go
@@ -113,12 +113,22 @@ func (k *Key) ValueWithShadows() []string {
        if len(k.shadows) == 0 {
                return []string{k.value}
        }
+
        vals := make([]string, len(k.shadows)+1)
        vals[0] = k.value
        for i := range k.shadows {
                vals[i+1] = k.shadows[i].value
        }
-       return vals
+
+       ret := make([]string, 0)
+
+       for _, v := range vals {
+               if v != "" {
+                       ret = append(ret, v)
+               }
+       }
+
+       return ret
 }

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions