@@ -2,7 +2,6 @@ package driverutil
2
2
3
3
import (
4
4
"fmt"
5
- "sort"
6
5
7
6
"github.com/lima-vm/lima/v2/pkg/limatype"
8
7
"github.com/lima-vm/lima/v2/pkg/registry"
@@ -11,46 +10,37 @@ import (
11
10
12
11
func ResolveVMType (y * limatype.LimaYAML , filePath string ) error {
13
12
if y .VMType != nil && * y .VMType != "" {
14
- vmType := * y .VMType
15
- _ , intDriver , exists := registry .Get (vmType )
16
- if ! exists {
17
- return fmt .Errorf ("specified vmType %q is not a registered driver" , vmType )
13
+ if err := validateConfigAgainstDriver (y , filePath , * y .VMType ); err != nil {
14
+ return err
18
15
}
19
- if intDriver == nil {
20
- // For now we only support internal drivers.
21
- return fmt .Errorf ("specified vmType %q is not an internal driver" , vmType )
22
- }
23
- if err := intDriver .AcceptConfig (y , filePath ); err != nil {
24
- return fmt .Errorf ("vmType %q is not compatible with the configuration: %w" , vmType , err )
25
- }
26
- if err := intDriver .FillConfig (y , filePath ); err != nil {
27
- return fmt .Errorf ("unable to fill config for vmType %q: %w" , vmType , err )
28
- }
29
- logrus .Debugf ("ResolveVMType: using explicitly specified VMType %q" , vmType )
16
+ logrus .Debugf ("Using specified vmType %q for %q" , * y .VMType , filePath )
30
17
return nil
31
18
}
32
19
33
- // If VMType is not specified, we try to resolve it by checking config with all the registered drivers.
34
- candidates := registry .List ()
35
- vmtypes := make ([]string , 0 , len (candidates ))
36
- for vmtype := range candidates {
37
- vmtypes = append (vmtypes , vmtype )
20
+ // If VMType is not specified, we go with the default platform driver.
21
+ vmType := limatype .DefaultDriver ()
22
+ if err := validateConfigAgainstDriver (y , filePath , vmType ); err == nil {
23
+ return nil
24
+ } else {
25
+ return err
38
26
}
39
- sort . Strings ( vmtypes )
27
+ }
40
28
41
- for _ , vmType := range vmtypes {
42
- // For now we only support internal drivers.
43
- if registry .CheckInternalOrExternal (vmType ) == registry .Internal {
44
- _ , intDriver , _ := registry .Get (vmType )
45
- if err := intDriver .AcceptConfig (y , filePath ); err == nil {
46
- logrus .Debugf ("ResolveVMType: resolved VMType %q" , vmType )
47
- if err := intDriver .FillConfig (y , filePath ); err != nil {
48
- return fmt .Errorf ("unable to fill config for VMType %q: %w" , vmType , err )
49
- }
50
- return nil
51
- }
52
- }
29
+ func validateConfigAgainstDriver (y * limatype.LimaYAML , filePath , vmType string ) error {
30
+ _ , intDriver , exists := registry .Get (vmType )
31
+ if ! exists {
32
+ return fmt .Errorf ("vmType %q is not a registered driver" , vmType )
33
+ }
34
+ // For now we only support internal drivers.
35
+ if intDriver == nil {
36
+ return fmt .Errorf ("vmType %q is not an internal driver" , vmType )
37
+ }
38
+ if err := intDriver .AcceptConfig (y , filePath ); err != nil {
39
+ return err
40
+ }
41
+ if err := intDriver .FillConfig (y , filePath ); err != nil {
42
+ return err
53
43
}
54
44
55
- return fmt . Errorf ( "no VMType found for %q" , filePath )
45
+ return nil
56
46
}
0 commit comments