diff --git a/cmd/limactl/edit.go b/cmd/limactl/edit.go index 9c57e18a8a0..6474d5e8bef 100644 --- a/cmd/limactl/edit.go +++ b/cmd/limactl/edit.go @@ -121,7 +121,7 @@ func editAction(cmd *cobra.Command, args []string) error { return saveRejectedYAML(yBytes, err) } - if err := limayaml.ValidateYAMLAgainstLatestConfig(yBytes, yContent); err != nil { + if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil { return saveRejectedYAML(yBytes, err) } diff --git a/pkg/limayaml/load.go b/pkg/limayaml/load.go index 13f71e8d92e..3929d7bac8a 100644 --- a/pkg/limayaml/load.go +++ b/pkg/limayaml/load.go @@ -63,7 +63,7 @@ func load(b []byte, filePath string, warn bool) (*LimaYAML, error) { } // It should be called before the `y` parameter is passed to FillDefault() that execute template. - if err := ValidateParamIsUsed(&y); err != nil { + if err := validateParamIsUsed(&y); err != nil { return nil, err } diff --git a/pkg/limayaml/validate.go b/pkg/limayaml/validate.go index 443eaff02f7..8e349a2c199 100644 --- a/pkg/limayaml/validate.go +++ b/pkg/limayaml/validate.go @@ -29,28 +29,6 @@ import ( "github.com/lima-vm/lima/pkg/version/versionutil" ) -func validateFileObject(f File, fieldName string) error { - var errs error - if !strings.Contains(f.Location, "://") { - if _, err := localpathutil.Expand(f.Location); err != nil { - errs = errors.Join(errs, fmt.Errorf("field `%s.location` refers to an invalid local file path: %q: %w", fieldName, f.Location, err)) - } - // f.Location does NOT need to be accessible, so we do NOT check os.Stat(f.Location) - } - if !slices.Contains(ArchTypes, f.Arch) { - errs = errors.Join(errs, fmt.Errorf("field `arch` must be one of %v; got %q", ArchTypes, f.Arch)) - } - if f.Digest != "" { - if !f.Digest.Algorithm().Available() { - errs = errors.Join(errs, fmt.Errorf("field `%s.digest` refers to an unavailable digest algorithm", fieldName)) - } - if err := f.Digest.Validate(); err != nil { - errs = errors.Join(errs, fmt.Errorf("field `%s.digest` is invalid: %s: %w", fieldName, f.Digest.String(), err)) - } - } - return errs -} - func Validate(y *LimaYAML, warn bool) error { var errs error @@ -441,6 +419,28 @@ func Validate(y *LimaYAML, warn bool) error { return errs } +func validateFileObject(f File, fieldName string) error { + var errs error + if !strings.Contains(f.Location, "://") { + if _, err := localpathutil.Expand(f.Location); err != nil { + errs = errors.Join(errs, fmt.Errorf("field `%s.location` refers to an invalid local file path: %q: %w", fieldName, f.Location, err)) + } + // f.Location does NOT need to be accessible, so we do NOT check os.Stat(f.Location) + } + if !slices.Contains(ArchTypes, f.Arch) { + errs = errors.Join(errs, fmt.Errorf("field `arch` must be one of %v; got %q", ArchTypes, f.Arch)) + } + if f.Digest != "" { + if !f.Digest.Algorithm().Available() { + errs = errors.Join(errs, fmt.Errorf("field `%s.digest` refers to an unavailable digest algorithm", fieldName)) + } + if err := f.Digest.Validate(); err != nil { + errs = errors.Join(errs, fmt.Errorf("field `%s.digest` is invalid: %s: %w", fieldName, f.Digest.String(), err)) + } + } + return errs +} + func validateNetwork(y *LimaYAML) error { var errs error interfaceName := make(map[string]int) @@ -518,9 +518,9 @@ func validateNetwork(y *LimaYAML) error { return errs } -// ValidateParamIsUsed checks if the keys in the `param` field are used in any script, probe, copyToHost, or portForward. +// validateParamIsUsed checks if the keys in the `param` field are used in any script, probe, copyToHost, or portForward. // It should be called before the `y` parameter is passed to FillDefault() that execute template. -func ValidateParamIsUsed(y *LimaYAML) error { +func validateParamIsUsed(y *LimaYAML) error { for key := range y.Param { re, err := regexp.Compile(`{{[^}]*\.Param\.` + key + `[^}]*}}|\bPARAM_` + key + `\b`) if err != nil { @@ -611,9 +611,9 @@ func warnExperimental(y *LimaYAML) { } } -// ValidateYAMLAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML. +// ValidateAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML. // This validates configuration rules that disallow certain changes, such as shrinking the disk. -func ValidateYAMLAgainstLatestConfig(yNew, yLatest []byte) error { +func ValidateAgainstLatestConfig(yNew, yLatest []byte) error { var n LimaYAML // Load the latest YAML and fill in defaults diff --git a/pkg/limayaml/validate_test.go b/pkg/limayaml/validate_test.go index e136c6cbe06..6d45941707c 100644 --- a/pkg/limayaml/validate_test.go +++ b/pkg/limayaml/validate_test.go @@ -266,7 +266,7 @@ provision: "field `provision[1].path` must not be empty when mode is \"data\"") } -func TestValidateYAMLAgainstLatestConfig(t *testing.T) { +func TestValidateAgainstLatestConfig(t *testing.T) { tests := []struct { name string yNew string @@ -308,7 +308,7 @@ func TestValidateYAMLAgainstLatestConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := ValidateYAMLAgainstLatestConfig([]byte(tt.yNew), []byte(tt.yLatest)) + err := ValidateAgainstLatestConfig([]byte(tt.yNew), []byte(tt.yLatest)) if tt.wantErr == nil { assert.NilError(t, err) } else {