Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip empty shadow values when saving to file #294

Closed
nandra opened this issue Jul 8, 2021 · 3 comments · Fixed by #316
Closed

Skip empty shadow values when saving to file #294

nandra opened this issue Jul 8, 2021 · 3 comments · Fixed by #316
Assignees
Labels
bug Something isn't working

Comments

@nandra
Copy link

nandra commented Jul 8, 2021

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
 }
@unknwon unknwon added the bug Something isn't working label Nov 12, 2021
@dwmunster
Copy link
Contributor

dwmunster commented Feb 7, 2022

This should now be handled by the LoadOption AllowDuplicateShadowValues.

func main() {
	cfg:= ini.Empty(ini.LoadOptions{AllowDuplicateShadowValues: true, AllowShadows: true})
	// 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)
	}

	var b bytes.Buffer
	if _, err := cfg.WriteTo(&b); err != nil {
		fmt.Println("Failed to write to config", err)
	}
	fmt.Println(b.String())
}

outputs

[Match]
Name = eth0

[Network]
DNS =
DNS = 1.2.3.4
DNS = 5.6.7.8

@unknwon
Copy link
Member

unknwon commented Feb 10, 2022

Hey @dwmunster, not sure how AllowDuplicateShadowValues is helping here...

I think the problem here is that the line DNS = should not be saved to the file (i.e. skip keys with empty values). I'm going to apply a modified version of what @nandra had proposed in the description shortly.

@unknwon unknwon self-assigned this Feb 10, 2022
@unknwon unknwon changed the title Issue with shadow values when store to file Skip empty shadow values when saving to file Feb 10, 2022
@unknwon unknwon linked a pull request Feb 10, 2022 that will close this issue
3 tasks
@unknwon
Copy link
Member

unknwon commented Feb 10, 2022

https://github.com/go-ini/ini/releases/tag/v1.66.4 has been tagged for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants