-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2726 from jandubois/lima-marshal
Refactor limayaml.Save and store.SaveYAML → limayaml.Marshal
- Loading branch information
Showing
7 changed files
with
77 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package limayaml | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/goccy/go-yaml" | ||
"github.com/lima-vm/lima/pkg/yqutil" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func marshalString(s string) ([]byte, error) { | ||
if s == "null" || s == "~" { | ||
// work around go-yaml bugs | ||
return []byte("\"" + s + "\""), nil | ||
} | ||
return yaml.Marshal(s) | ||
} | ||
|
||
const ( | ||
documentStart = "---\n" | ||
documentEnd = "...\n" | ||
) | ||
|
||
// Marshal the struct as a YAML document, optionally as a stream. | ||
func Marshal(y *LimaYAML, stream bool) ([]byte, error) { | ||
options := []yaml.EncodeOption{yaml.CustomMarshaler[string](marshalString)} | ||
b, err := yaml.MarshalWithOptions(y, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if stream { | ||
doc := documentStart + string(b) + documentEnd | ||
b = []byte(doc) | ||
} | ||
return b, nil | ||
} | ||
|
||
func unmarshalDisk(dst *Disk, b []byte) error { | ||
var s string | ||
if err := yaml.Unmarshal(b, &s); err == nil { | ||
*dst = Disk{Name: s} | ||
return nil | ||
} | ||
return yaml.Unmarshal(b, dst) | ||
} | ||
|
||
func Unmarshal(data []byte, v interface{}, comment string) error { | ||
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil { | ||
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err) | ||
} | ||
// the go-yaml library doesn't catch all markup errors, unfortunately | ||
// make sure to get a "second opinion", using the same library as "yq" | ||
if err := yqutil.ValidateContent(data); err != nil { | ||
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err) | ||
} | ||
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil { | ||
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML is deprecated and will be unsupported in a future version of Lima") | ||
// Non-strict YAML is known to be used by Rancher Desktop: | ||
// https://github.com/rancher-sandbox/rancher-desktop/blob/c7ea7508a0191634adf16f4675f64c73198e8d37/src/backend/lima.ts#L114-L117 | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters