Skip to content

refactor: move driver specific code to drivers #3770

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

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
6 changes: 5 additions & 1 deletion cmd/limactl/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/limayaml"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
"github.com/lima-vm/lima/v2/pkg/yqutil"
)

Expand Down Expand Up @@ -79,6 +80,9 @@ func cloneAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err := driverutil.ResolveVMType(y, filePath); err != nil {
return fmt.Errorf("failed to accept config for %q: %w", filePath, err)
}
if err := limayaml.Validate(y, true); err != nil {
return saveRejectedYAML(yBytes, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/ioutilx"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/sshutil"
"github.com/lima-vm/lima/v2/pkg/store"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
instances := make(map[string]*store.Instance)
instances := make(map[string]*limatype.Instance)
scpFlags := []string{}
scpArgs := []string{}
debug, err := cmd.Flags().GetBool("debug")
Expand Down Expand Up @@ -109,7 +110,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
}
return err
}
if inst.Status == store.StatusStopped {
if inst.Status == limatype.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
if legacySSH {
Expand Down
9 changes: 5 additions & 4 deletions cmd/limactl/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/imgutil/proxyimgutil"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
)

func newDiskCommand() *cobra.Command {
Expand Down Expand Up @@ -241,7 +242,7 @@ func diskDeleteAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
var instances []*store.Instance
var instances []*limatype.Instance
for _, instName := range instNames {
inst, err := store.Inspect(instName)
if err != nil {
Expand Down Expand Up @@ -341,7 +342,7 @@ func diskUnlockAction(_ *cobra.Command, args []string) error {
diskName, disk.Instance, inst.Errors)
continue
}
if inst.Status == store.StatusRunning {
if inst.Status == limatype.StatusRunning {
logrus.Warnf("Cannot unlock disk %q used by running instance %q", diskName, disk.Instance)
continue
}
Expand Down Expand Up @@ -398,7 +399,7 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
if disk.Instance != "" {
inst, err := store.Inspect(disk.Instance)
if err == nil {
if inst.Status == store.StatusRunning {
if inst.Status == limatype.StatusRunning {
return fmt.Errorf("cannot resize disk %q used by running instance %q. Please stop the VM instance", diskName, disk.Instance)
}
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/editutil"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/limayaml"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
"github.com/lima-vm/lima/v2/pkg/uiutil"
"github.com/lima-vm/lima/v2/pkg/yqutil"
)
Expand All @@ -45,20 +48,20 @@ func editAction(cmd *cobra.Command, args []string) error {

var filePath string
var err error
var inst *store.Instance
var inst *limatype.Instance

if arg == "" {
arg = DefaultInstanceName
}
if err := store.ValidateInstName(arg); err == nil {
if err := dirnames.ValidateInstName(arg); err == nil {
inst, err = store.Inspect(arg)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q not found", arg)
}
return err
}
if inst.Status == store.StatusRunning {
if inst.Status == limatype.StatusRunning {
return errors.New("cannot edit a running instance")
}
filePath = filepath.Join(inst.Dir, filenames.LimaYAML)
Expand Down Expand Up @@ -117,6 +120,9 @@ func editAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err := driverutil.ResolveVMType(y, filePath); err != nil {
return fmt.Errorf("failed to accept config for %q: %w", filePath, err)
}
if err := limayaml.Validate(y, true); err != nil {
return saveRejectedYAML(yBytes, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/factory-reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/lima-vm/lima/v2/pkg/cidata"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
)

func newFactoryResetCommand() *cobra.Command {
Expand Down
12 changes: 6 additions & 6 deletions cmd/limactl/genschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
orderedmap "github.com/wk8/go-ordered-map/v2"

"github.com/lima-vm/lima/v2/pkg/jsonschemautil"
"github.com/lima-vm/lima/v2/pkg/limayaml"
"github.com/lima-vm/lima/v2/pkg/limatype"
)

func newGenSchemaCommand() *cobra.Command {
Expand Down Expand Up @@ -52,7 +52,7 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
return err
}

schema := jsonschema.Reflect(&limayaml.LimaYAML{})
schema := jsonschema.Reflect(&limatype.LimaYAML{})
// allow Disk to be either string (name) or object (struct)
schema.Definitions["Disk"].Type = "" // was: "object"
schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{
Expand All @@ -72,10 +72,10 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
{Type: "object"},
}
properties := schema.Definitions["LimaYAML"].Properties
getProp(properties, "os").Enum = toAny(limayaml.OSTypes)
getProp(properties, "arch").Enum = toAny(limayaml.ArchTypes)
getProp(properties, "mountType").Enum = toAny(limayaml.MountTypes)
getProp(properties, "vmType").Enum = toAny(limayaml.VMTypes)
getProp(properties, "os").Enum = toAny(limatype.OSTypes)
getProp(properties, "arch").Enum = toAny(limatype.ArchTypes)
getProp(properties, "mountType").Enum = toAny(limatype.MountTypes)
getProp(properties, "vmType").Enum = toAny(limatype.VMTypes)
j, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/store"
)

Expand Down Expand Up @@ -171,7 +172,7 @@ func listAction(cmd *cobra.Command, args []string) error {
}

// get the state and config for all the requested instances
var instances []*store.Instance
var instances []*limatype.Instance
for _, instanceName := range instanceNames {
instance, err := store.Inspect(instanceName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/lima-vm/lima/v2/pkg/debugutil"
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/fsutil"
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/osutil"
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
"github.com/lima-vm/lima/v2/pkg/version"
)

Expand Down
14 changes: 10 additions & 4 deletions cmd/limactl/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
package main

import (
"fmt"
"maps"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/downloader"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limayaml"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/templatestore"
Expand Down Expand Up @@ -62,8 +65,8 @@ func pruneAction(cmd *cobra.Command, _ []string) error {
return nil
}

func knownLocations() (map[string]limayaml.File, error) {
locations := make(map[string]limayaml.File)
func knownLocations() (map[string]limatype.File, error) {
locations := make(map[string]limatype.File)

// Collect locations from instances
instances, err := store.Instances()
Expand Down Expand Up @@ -92,13 +95,16 @@ func knownLocations() (map[string]limayaml.File, error) {
if err != nil {
return nil, err
}
if err := driverutil.ResolveVMType(y, t.Name); err != nil {
return nil, fmt.Errorf("failed to accept config for %q: %w", t.Name, err)
}
maps.Copy(locations, locationsFromLimaYAML(y))
}
return locations, nil
}

func locationsFromLimaYAML(y *limayaml.LimaYAML) map[string]limayaml.File {
locations := make(map[string]limayaml.File)
func locationsFromLimaYAML(y *limatype.LimaYAML) map[string]limatype.File {
locations := make(map[string]limatype.File)
for _, f := range y.Images {
locations[downloader.CacheKey(f.Location)] = f.File
if f.Kernel != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/ioutilx"
"github.com/lima-vm/lima/v2/pkg/limayaml"
"github.com/lima-vm/lima/v2/pkg/limatype"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/sshutil"
"github.com/lima-vm/lima/v2/pkg/store"
Expand Down Expand Up @@ -82,7 +82,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
}
return err
}
if inst.Status == store.StatusStopped {
if inst.Status == limatype.StatusStopped {
startNow, err := askWhetherToStart()
if err != nil {
return err
Expand Down Expand Up @@ -140,7 +140,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
if workDir != "" {
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
// FIXME: check whether y.Mounts contains the home, not just len > 0
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limayaml.WSL2 {
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limatype.WSL2 {
hostCurrentDir, err := os.Getwd()
if err == nil && runtime.GOOS == "windows" {
hostCurrentDir, err = mountDirFromWindowsDir(inst, hostCurrentDir)
Expand Down Expand Up @@ -245,8 +245,8 @@ func shellAction(cmd *cobra.Command, args []string) error {
return sshCmd.Run()
}

func mountDirFromWindowsDir(inst *store.Instance, dir string) (string, error) {
if inst.VMType == limayaml.WSL2 {
func mountDirFromWindowsDir(inst *limatype.Instance, dir string) (string, error) {
if inst.VMType == limatype.WSL2 {
distroName := "lima-" + inst.Name
return ioutilx.WindowsSubsystemPathForLinux(dir, distroName)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/limactl/show-ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/sshutil"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
)

const showSSHExample = `
Expand Down
24 changes: 15 additions & 9 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import (
"github.com/spf13/cobra"

"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
"github.com/lima-vm/lima/v2/pkg/driverutil"
"github.com/lima-vm/lima/v2/pkg/editutil"
"github.com/lima-vm/lima/v2/pkg/instance"
"github.com/lima-vm/lima/v2/pkg/limatmpl"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
"github.com/lima-vm/lima/v2/pkg/limayaml"
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
"github.com/lima-vm/lima/v2/pkg/registry"
"github.com/lima-vm/lima/v2/pkg/store"
"github.com/lima-vm/lima/v2/pkg/store/filenames"
"github.com/lima-vm/lima/v2/pkg/templatestore"
"github.com/lima-vm/lima/v2/pkg/uiutil"
"github.com/lima-vm/lima/v2/pkg/yqutil"
Expand Down Expand Up @@ -102,7 +105,7 @@ See the examples in 'limactl create --help'.
return startCommand
}

func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*store.Instance, error) {
func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*limatype.Instance, error) {
var arg string // can be empty
if len(args) > 0 {
arg = args[0]
Expand All @@ -121,7 +124,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
return nil, err
}
if name != "" {
err := store.ValidateInstName(name)
err := dirnames.ValidateInstName(name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,7 +159,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
tty = false
}
var tmpl *limatmpl.Template
if err := store.ValidateInstName(arg); arg == "" || err == nil {
if err := dirnames.ValidateInstName(arg); arg == "" || err == nil {
tmpl = &limatmpl.Template{Name: name}
if arg == "" {
if name == "" {
Expand Down Expand Up @@ -211,7 +214,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
if _, err := store.Inspect(tmpl.Name); err == nil {
return nil, fmt.Errorf("instance %q already exists", tmpl.Name)
}
} else if err := store.ValidateInstName(tmpl.Name); err != nil {
} else if err := dirnames.ValidateInstName(tmpl.Name); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -240,7 +243,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
return instance.Create(cmd.Context(), tmpl.Name, tmpl.Bytes, saveBrokenYAML)
}

func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*store.Instance, error) {
func applyYQExpressionToExistingInstance(inst *limatype.Instance, yq string) (*limatype.Instance, error) {
if strings.TrimSpace(yq) == "" {
return inst, nil
}
Expand All @@ -258,6 +261,9 @@ func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*stor
if err != nil {
return nil, err
}
if err := driverutil.ResolveVMType(y, filePath); err != nil {
return nil, fmt.Errorf("failed to accept config for %q: %w", filePath, err)
}
if err := limayaml.Validate(y, true); err != nil {
rejectedYAML := "lima.REJECTED.yaml"
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {
Expand Down Expand Up @@ -461,15 +467,15 @@ func startAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("errors inspecting instance: %+v", inst.Errors)
}
switch inst.Status {
case store.StatusRunning:
case limatype.StatusRunning:
logrus.Infof("The instance %q is already running. Run `%s` to open the shell.",
inst.Name, instance.LimactlShellCmd(inst.Name))
// Not an error
return nil
case store.StatusStopped:
case limatype.StatusStopped:
// NOP
default:
logrus.Warnf("expected status %q, got %q", store.StatusStopped, inst.Status)
logrus.Warnf("expected status %q, got %q", limatype.StatusStopped, inst.Status)
}
ctx := cmd.Context()
err = networks.Reconcile(ctx, inst.Name)
Expand Down
Loading
Loading