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

Add save functionality to limayaml #2338

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cmd/limactl/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@ func newValidateCommand() *cobra.Command {
RunE: validateAction,
GroupID: advancedCommand,
}
validateCommand.Flags().Bool("fill", false, "fill defaults")
return validateCommand
}

func validateAction(_ *cobra.Command, args []string) error {
func validateAction(cmd *cobra.Command, args []string) error {
fill, err := cmd.Flags().GetBool("fill")
if err != nil {
return err
}

for _, f := range args {
_, err := store.LoadYAMLByFilePath(f)
y, err := store.LoadYAMLByFilePath(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
return err
}
logrus.Infof("%q: OK", f)
if fill {
b, err := store.SaveYAML(y, len(args) > 1)
if err != nil {
return err
}
fmt.Fprint(cmd.OutOrStdout(), string(b))
}
}

return nil
Expand Down
25 changes: 25 additions & 0 deletions pkg/limayaml/save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package limayaml

import (
"github.com/goccy/go-yaml"
)

func marshalString(s string) ([]byte, error) {
if s == "null" || s == "~" {
// work around go-yaml bugs
return []byte("\"" + s + "\""), nil
}
return yaml.Marshal(s)
}

func marshalYAML(v interface{}) ([]byte, error) {
options := []yaml.EncodeOption{yaml.CustomMarshaler[string](marshalString)}
return yaml.MarshalWithOptions(v, options...)
}

// Save saves the yaml.
//
// Save does not fill defaults. Use FillDefaults.
func Save(y *LimaYAML) ([]byte, error) {
return marshalYAML(y)
}
35 changes: 35 additions & 0 deletions pkg/limayaml/save_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package limayaml

import (
"testing"

"github.com/lima-vm/lima/pkg/ptr"
"gotest.tools/v3/assert"
)

func TestSaveEmpty(t *testing.T) {
_, err := Save(&LimaYAML{})
assert.NilError(t, err)
}

func TestSaveTilde(t *testing.T) {
y := LimaYAML{
Mounts: []Mount{
{Location: "~", Writable: ptr.Of(false)},
{Location: "/tmp/lima", Writable: ptr.Of(true)},
{Location: "null"},
},
}
b, err := Save(&y)
assert.NilError(t, err)
// yaml will load ~ (or null) as null
// make sure that it is always quoted
assert.Equal(t, string(b), `images: []
mounts:
- location: "~"
writable: false
- location: /tmp/lima
writable: true
- location: "null"
`)
}
17 changes: 17 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ func LoadYAMLByFilePath(filePath string) (*limayaml.LimaYAML, error) {
}
return y, nil
}

const documentStart = "---\n"

const documentEnd = "...\n"

// SaveYAML saves the yaml, optionally as a stream.
func SaveYAML(y *limayaml.LimaYAML, stream bool) ([]byte, error) {
b, err := limayaml.Save(y)
if err != nil {
return nil, err
}
if stream {
doc := documentStart + string(b) + documentEnd
b = []byte(doc)
}
return b, nil
}
Loading