Skip to content

Commit

Permalink
Use the yaml formatter to preserve newlines
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Oct 26, 2024
1 parent 3b023d5 commit f384b28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 11 additions & 1 deletion pkg/emrichenutil/emrichenutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"io"

"github.com/lima-vm/lima/pkg/yamlutil"

"github.com/go-go-golems/go-emrichen/pkg/emrichen"
"gopkg.in/yaml.v3"
)
Expand All @@ -16,6 +18,14 @@ func EvaluateTemplate(data []byte) ([]byte, error) {
if err != nil {
return nil, err
}
formatter, err := yamlutil.NewFormatter()
if err != nil {
return nil, err
}
data, err = formatter.Before(data)
if err != nil {
return nil, err
}
decoder := yaml.NewDecoder(bytes.NewReader(data))
var resultNode *yaml.Node
for {
Expand All @@ -42,5 +52,5 @@ func EvaluateTemplate(data []byte) ([]byte, error) {
if err != nil {
return nil, err
}
return output, nil
return formatter.Format(output)
}
8 changes: 1 addition & 7 deletions pkg/emrichenutil/emrichenutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ cpus: 2
# Memory size
memory: 2GiB
`
// Note: emrichen currently removes empty lines, but not comments
expected :=
`# CPUs
cpus: 2
# Memory size
memory: 2GiB
`
expected := content
out, err := EvaluateTemplate([]byte(content))
assert.NilError(t, err)
assert.Equal(t, expected, string(out))
Expand Down

0 comments on commit f384b28

Please sign in to comment.