diff --git a/machine/bootcmdline_test.go b/machine/bootcmdline_test.go index dbf0b29..df17dc0 100644 --- a/machine/bootcmdline_test.go +++ b/machine/bootcmdline_test.go @@ -24,5 +24,16 @@ var _ = Describe("BootCMDLine", func() { Expect(string(b)).To(Equal("baz:\n bar: \"\"\nconfig_url: foo bar\n"), string(b)) }) + It("works if cmdline contains a dash or underscore", func() { + f, err := os.CreateTemp("", "test") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(f.Name()) + + err = os.WriteFile(f.Name(), []byte(`config-url="foo bar" ba_z.bar=""`), os.ModePerm) + Expect(err).ToNot(HaveOccurred()) + + _, err = DotToYAML(f.Name()) + Expect(err).ToNot(HaveOccurred()) + }) }) }) diff --git a/unstructured/yaml.go b/unstructured/yaml.go index 638d019..8252445 100644 --- a/unstructured/yaml.go +++ b/unstructured/yaml.go @@ -3,10 +3,10 @@ package unstructured import ( "errors" "fmt" - "github.com/hashicorp/go-multierror" "github.com/itchyny/gojq" "gopkg.in/yaml.v3" + "strings" ) func YAMLHasKey(query string, content []byte) (bool, error) { @@ -109,12 +109,22 @@ func ToYAML(v map[string]interface{}) ([]byte, error) { var errs error for k, value := range v { - tmpl := ".%s=\"%s\"" + prefix := "." + equal := "=" + tmplKey := "%s" + tmplValue := "\"%s\"" + + // If key has a dash we need to add quotes to it to avoid failure parsing ut + if strings.Contains(k, "-") { + tmplKey = "\"%s\"" + } + // support boolean types if value == "true" || value == "false" { - tmpl = ".%s=%s" + tmplValue = "%s" } - newData, err := jq(fmt.Sprintf(tmpl, k, value), data) + finalTemplate := prefix + tmplKey + equal + tmplValue + newData, err := jq(fmt.Sprintf(finalTemplate, k, value), data) if err != nil { errs = multierror.Append(errs, err) continue