Skip to content

Commit ca47c5c

Browse files
authored
Merge pull request #3660 from alexandear-org/refactor/limayaml-validate
refactor: Limayaml validation functions
2 parents 0098f15 + 5276974 commit ca47c5c

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func editAction(cmd *cobra.Command, args []string) error {
121121
return saveRejectedYAML(yBytes, err)
122122
}
123123

124-
if err := limayaml.ValidateYAMLAgainstLatestConfig(yBytes, yContent); err != nil {
124+
if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil {
125125
return saveRejectedYAML(yBytes, err)
126126
}
127127

pkg/limayaml/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func load(b []byte, filePath string, warn bool) (*LimaYAML, error) {
6363
}
6464

6565
// It should be called before the `y` parameter is passed to FillDefault() that execute template.
66-
if err := ValidateParamIsUsed(&y); err != nil {
66+
if err := validateParamIsUsed(&y); err != nil {
6767
return nil, err
6868
}
6969

pkg/limayaml/validate.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,6 @@ import (
2929
"github.com/lima-vm/lima/pkg/version/versionutil"
3030
)
3131

32-
func validateFileObject(f File, fieldName string) error {
33-
var errs error
34-
if !strings.Contains(f.Location, "://") {
35-
if _, err := localpathutil.Expand(f.Location); err != nil {
36-
errs = errors.Join(errs, fmt.Errorf("field `%s.location` refers to an invalid local file path: %q: %w", fieldName, f.Location, err))
37-
}
38-
// f.Location does NOT need to be accessible, so we do NOT check os.Stat(f.Location)
39-
}
40-
if !slices.Contains(ArchTypes, f.Arch) {
41-
errs = errors.Join(errs, fmt.Errorf("field `arch` must be one of %v; got %q", ArchTypes, f.Arch))
42-
}
43-
if f.Digest != "" {
44-
if !f.Digest.Algorithm().Available() {
45-
errs = errors.Join(errs, fmt.Errorf("field `%s.digest` refers to an unavailable digest algorithm", fieldName))
46-
}
47-
if err := f.Digest.Validate(); err != nil {
48-
errs = errors.Join(errs, fmt.Errorf("field `%s.digest` is invalid: %s: %w", fieldName, f.Digest.String(), err))
49-
}
50-
}
51-
return errs
52-
}
53-
5432
func Validate(y *LimaYAML, warn bool) error {
5533
var errs error
5634

@@ -441,6 +419,28 @@ func Validate(y *LimaYAML, warn bool) error {
441419
return errs
442420
}
443421

422+
func validateFileObject(f File, fieldName string) error {
423+
var errs error
424+
if !strings.Contains(f.Location, "://") {
425+
if _, err := localpathutil.Expand(f.Location); err != nil {
426+
errs = errors.Join(errs, fmt.Errorf("field `%s.location` refers to an invalid local file path: %q: %w", fieldName, f.Location, err))
427+
}
428+
// f.Location does NOT need to be accessible, so we do NOT check os.Stat(f.Location)
429+
}
430+
if !slices.Contains(ArchTypes, f.Arch) {
431+
errs = errors.Join(errs, fmt.Errorf("field `arch` must be one of %v; got %q", ArchTypes, f.Arch))
432+
}
433+
if f.Digest != "" {
434+
if !f.Digest.Algorithm().Available() {
435+
errs = errors.Join(errs, fmt.Errorf("field `%s.digest` refers to an unavailable digest algorithm", fieldName))
436+
}
437+
if err := f.Digest.Validate(); err != nil {
438+
errs = errors.Join(errs, fmt.Errorf("field `%s.digest` is invalid: %s: %w", fieldName, f.Digest.String(), err))
439+
}
440+
}
441+
return errs
442+
}
443+
444444
func validateNetwork(y *LimaYAML) error {
445445
var errs error
446446
interfaceName := make(map[string]int)
@@ -518,9 +518,9 @@ func validateNetwork(y *LimaYAML) error {
518518
return errs
519519
}
520520

521-
// ValidateParamIsUsed checks if the keys in the `param` field are used in any script, probe, copyToHost, or portForward.
521+
// validateParamIsUsed checks if the keys in the `param` field are used in any script, probe, copyToHost, or portForward.
522522
// It should be called before the `y` parameter is passed to FillDefault() that execute template.
523-
func ValidateParamIsUsed(y *LimaYAML) error {
523+
func validateParamIsUsed(y *LimaYAML) error {
524524
for key := range y.Param {
525525
re, err := regexp.Compile(`{{[^}]*\.Param\.` + key + `[^}]*}}|\bPARAM_` + key + `\b`)
526526
if err != nil {
@@ -611,9 +611,9 @@ func warnExperimental(y *LimaYAML) {
611611
}
612612
}
613613

614-
// ValidateYAMLAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML.
614+
// ValidateAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML.
615615
// This validates configuration rules that disallow certain changes, such as shrinking the disk.
616-
func ValidateYAMLAgainstLatestConfig(yNew, yLatest []byte) error {
616+
func ValidateAgainstLatestConfig(yNew, yLatest []byte) error {
617617
var n LimaYAML
618618

619619
// Load the latest YAML and fill in defaults

pkg/limayaml/validate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ provision:
266266
"field `provision[1].path` must not be empty when mode is \"data\"")
267267
}
268268

269-
func TestValidateYAMLAgainstLatestConfig(t *testing.T) {
269+
func TestValidateAgainstLatestConfig(t *testing.T) {
270270
tests := []struct {
271271
name string
272272
yNew string
@@ -308,7 +308,7 @@ func TestValidateYAMLAgainstLatestConfig(t *testing.T) {
308308

309309
for _, tt := range tests {
310310
t.Run(tt.name, func(t *testing.T) {
311-
err := ValidateYAMLAgainstLatestConfig([]byte(tt.yNew), []byte(tt.yLatest))
311+
err := ValidateAgainstLatestConfig([]byte(tt.yNew), []byte(tt.yLatest))
312312
if tt.wantErr == nil {
313313
assert.NilError(t, err)
314314
} else {

0 commit comments

Comments
 (0)