Skip to content

Commit 11eb750

Browse files
authored
Merge pull request #2726 from jandubois/lima-marshal
Refactor limayaml.Save and store.SaveYAML → limayaml.Marshal
2 parents 46aa594 + 2edcf1c commit 11eb750

File tree

7 files changed

+77
-82
lines changed

7 files changed

+77
-82
lines changed

cmd/limactl/validate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/lima-vm/lima/cmd/limactl/guessarg"
7+
"github.com/lima-vm/lima/pkg/limayaml"
78
"github.com/lima-vm/lima/pkg/store"
89
"github.com/spf13/cobra"
910

@@ -38,7 +39,7 @@ func validateAction(cmd *cobra.Command, args []string) error {
3839
}
3940
logrus.Infof("%q: OK", f)
4041
if fill {
41-
b, err := store.SaveYAML(y, len(args) > 1)
42+
b, err := limayaml.Marshal(y, len(args) > 1)
4243
if err != nil {
4344
return err
4445
}

pkg/limayaml/limayaml_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const emptyYAML = "images: []\n"
2121
func TestEmptyYAML(t *testing.T) {
2222
var y LimaYAML
2323
t.Log(dumpJSON(t, y))
24-
b, err := marshalYAML(y)
24+
b, err := Marshal(&y, false)
2525
assert.NilError(t, err)
2626
assert.Equal(t, string(b), emptyYAML)
2727
}
@@ -32,12 +32,12 @@ func TestDefaultYAML(t *testing.T) {
3232
bytes, err := os.ReadFile("default.yaml")
3333
assert.NilError(t, err)
3434
var y LimaYAML
35-
err = unmarshalYAML(bytes, &y, "")
35+
err = Unmarshal(bytes, &y, "")
3636
assert.NilError(t, err)
3737
y.Images = nil // remove default images
3838
y.Mounts = nil // remove default mounts
3939
t.Log(dumpJSON(t, y))
40-
b, err := marshalYAML(y)
40+
b, err := Marshal(&y, false)
4141
assert.NilError(t, err)
4242
assert.Equal(t, string(b), defaultYAML)
4343
}

pkg/limayaml/load.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,18 @@ import (
66
"os"
77
"path/filepath"
88

9-
"github.com/goccy/go-yaml"
109
"github.com/lima-vm/lima/pkg/store/dirnames"
1110
"github.com/lima-vm/lima/pkg/store/filenames"
12-
"github.com/lima-vm/lima/pkg/yqutil"
1311
"github.com/sirupsen/logrus"
1412
)
1513

16-
func unmarshalDisk(dst *Disk, b []byte) error {
17-
var s string
18-
if err := yaml.Unmarshal(b, &s); err == nil {
19-
*dst = Disk{Name: s}
20-
return nil
21-
}
22-
return yaml.Unmarshal(b, dst)
23-
}
24-
25-
func unmarshalYAML(data []byte, v interface{}, comment string) error {
26-
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
27-
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
28-
}
29-
// the go-yaml library doesn't catch all markup errors, unfortunately
30-
// make sure to get a "second opinion", using the same library as "yq"
31-
if err := yqutil.ValidateContent(data); err != nil {
32-
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
33-
}
34-
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
35-
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML is deprecated and will be unsupported in a future version of Lima")
36-
// Non-strict YAML is known to be used by Rancher Desktop:
37-
// https://github.com/rancher-sandbox/rancher-desktop/blob/c7ea7508a0191634adf16f4675f64c73198e8d37/src/backend/lima.ts#L114-L117
38-
}
39-
return nil
40-
}
41-
4214
// Load loads the yaml and fulfills unspecified fields with the default values.
4315
//
4416
// Load does not validate. Use Validate for validation.
4517
func Load(b []byte, filePath string) (*LimaYAML, error) {
4618
var y, d, o LimaYAML
4719

48-
if err := unmarshalYAML(b, &y, fmt.Sprintf("main file %q", filePath)); err != nil {
20+
if err := Unmarshal(b, &y, fmt.Sprintf("main file %q", filePath)); err != nil {
4921
return nil, err
5022
}
5123
configDir, err := dirnames.LimaConfigDir()
@@ -57,7 +29,7 @@ func Load(b []byte, filePath string) (*LimaYAML, error) {
5729
bytes, err := os.ReadFile(defaultPath)
5830
if err == nil {
5931
logrus.Debugf("Mixing %q into %q", defaultPath, filePath)
60-
if err := unmarshalYAML(bytes, &d, fmt.Sprintf("default file %q", defaultPath)); err != nil {
32+
if err := Unmarshal(bytes, &d, fmt.Sprintf("default file %q", defaultPath)); err != nil {
6133
return nil, err
6234
}
6335
} else if !errors.Is(err, os.ErrNotExist) {
@@ -68,7 +40,7 @@ func Load(b []byte, filePath string) (*LimaYAML, error) {
6840
bytes, err = os.ReadFile(overridePath)
6941
if err == nil {
7042
logrus.Debugf("Mixing %q into %q", overridePath, filePath)
71-
if err := unmarshalYAML(bytes, &o, fmt.Sprintf("override file %q", overridePath)); err != nil {
43+
if err := Unmarshal(bytes, &o, fmt.Sprintf("override file %q", overridePath)); err != nil {
7244
return nil, err
7345
}
7446
} else if !errors.Is(err, os.ErrNotExist) {

pkg/limayaml/marshal.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package limayaml
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goccy/go-yaml"
7+
"github.com/lima-vm/lima/pkg/yqutil"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
func marshalString(s string) ([]byte, error) {
12+
if s == "null" || s == "~" {
13+
// work around go-yaml bugs
14+
return []byte("\"" + s + "\""), nil
15+
}
16+
return yaml.Marshal(s)
17+
}
18+
19+
const (
20+
documentStart = "---\n"
21+
documentEnd = "...\n"
22+
)
23+
24+
// Marshal the struct as a YAML document, optionally as a stream.
25+
func Marshal(y *LimaYAML, stream bool) ([]byte, error) {
26+
options := []yaml.EncodeOption{yaml.CustomMarshaler[string](marshalString)}
27+
b, err := yaml.MarshalWithOptions(y, options...)
28+
if err != nil {
29+
return nil, err
30+
}
31+
if stream {
32+
doc := documentStart + string(b) + documentEnd
33+
b = []byte(doc)
34+
}
35+
return b, nil
36+
}
37+
38+
func unmarshalDisk(dst *Disk, b []byte) error {
39+
var s string
40+
if err := yaml.Unmarshal(b, &s); err == nil {
41+
*dst = Disk{Name: s}
42+
return nil
43+
}
44+
return yaml.Unmarshal(b, dst)
45+
}
46+
47+
func Unmarshal(data []byte, v interface{}, comment string) error {
48+
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
49+
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
50+
}
51+
// the go-yaml library doesn't catch all markup errors, unfortunately
52+
// make sure to get a "second opinion", using the same library as "yq"
53+
if err := yqutil.ValidateContent(data); err != nil {
54+
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
55+
}
56+
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
57+
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML is deprecated and will be unsupported in a future version of Lima")
58+
// Non-strict YAML is known to be used by Rancher Desktop:
59+
// https://github.com/rancher-sandbox/rancher-desktop/blob/c7ea7508a0191634adf16f4675f64c73198e8d37/src/backend/lima.ts#L114-L117
60+
}
61+
return nil
62+
}

pkg/limayaml/save_test.go renamed to pkg/limayaml/marshal_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@ import (
77
"gotest.tools/v3/assert"
88
)
99

10-
func TestSaveEmpty(t *testing.T) {
11-
_, err := Save(&LimaYAML{})
10+
func TestMarshalEmpty(t *testing.T) {
11+
_, err := Marshal(&LimaYAML{}, false)
1212
assert.NilError(t, err)
1313
}
1414

15-
func TestSaveTilde(t *testing.T) {
15+
func TestMarshalTilde(t *testing.T) {
1616
y := LimaYAML{
1717
Mounts: []Mount{
1818
{Location: "~", Writable: ptr.Of(false)},
1919
{Location: "/tmp/lima", Writable: ptr.Of(true)},
2020
{Location: "null"},
2121
},
2222
}
23-
b, err := Save(&y)
23+
b, err := Marshal(&y, true)
2424
assert.NilError(t, err)
2525
// yaml will load ~ (or null) as null
2626
// make sure that it is always quoted
27-
assert.Equal(t, string(b), `images: []
27+
assert.Equal(t, string(b), `---
28+
images: []
2829
mounts:
2930
- location: "~"
3031
writable: false
3132
- location: /tmp/lima
3233
writable: true
3334
- location: "null"
35+
...
3436
`)
3537
}

pkg/limayaml/save.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

pkg/store/store.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,3 @@ func LoadYAMLByFilePath(filePath string) (*limayaml.LimaYAML, error) {
133133
}
134134
return y, nil
135135
}
136-
137-
const documentStart = "---\n"
138-
139-
const documentEnd = "...\n"
140-
141-
// SaveYAML saves the yaml, optionally as a stream.
142-
func SaveYAML(y *limayaml.LimaYAML, stream bool) ([]byte, error) {
143-
b, err := limayaml.Save(y)
144-
if err != nil {
145-
return nil, err
146-
}
147-
if stream {
148-
doc := documentStart + string(b) + documentEnd
149-
b = []byte(doc)
150-
}
151-
return b, nil
152-
}

0 commit comments

Comments
 (0)