Skip to content

refactor: pass context.TODO() by parameter #3877

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

Merged
merged 1 commit into from
Aug 21, 2025
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
4 changes: 3 additions & 1 deletion cmd/lima-driver-qemu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package main

import (
"context"

"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/driver/qemu"
)

// To be used as an external driver for Lima.
func main() {
server.Serve(qemu.New())
server.Serve(context.Background(), qemu.New())
}
4 changes: 3 additions & 1 deletion cmd/lima-driver-vz/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package main

import (
"context"

"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/driver/vz"
)

// To be used as an external driver for Lima.
func main() {
server.Serve(vz.New())
server.Serve(context.Background(), vz.New())
}
4 changes: 3 additions & 1 deletion cmd/lima-driver-wsl2/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package main

import (
"context"

"github.com/lima-vm/lima/v2/pkg/driver/external/server"
"github.com/lima-vm/lima/v2/pkg/driver/wsl2"
)

// To be used as an external driver for Lima.
func main() {
server.Serve(wsl2.New())
server.Serve(context.Background(), wsl2.New())
}
2 changes: 1 addition & 1 deletion cmd/lima-guestagent/daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
return ticker.C, ticker.Stop
}

agent, err := guestagent.New(newTicker, tick*20)
agent, err := guestagent.New(ctx, newTicker, tick*20)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/limactl/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func cloneAction(cmd *cobra.Command, args []string) error {
}

oldInstName, newInstName := args[0], args[1]
oldInst, err := store.Inspect(oldInstName)
oldInst, err := store.Inspect(ctx, oldInstName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q not found", oldInstName)
Expand Down Expand Up @@ -75,20 +75,20 @@ func cloneAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
y, err := limayaml.LoadWithWarnings(yBytes, filePath)
y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
if err != nil {
return err
}
if err := limayaml.Validate(y, true); err != nil {
return saveRejectedYAML(yBytes, err)
}
if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil {
if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
return saveRejectedYAML(yBytes, err)
}
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
return err
}
newInst, err = store.Inspect(newInst.Name)
newInst, err = store.Inspect(ctx, newInst.Name)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func copyAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
legacySSH := sshutil.DetectOpenSSHVersion(sshExe).LessThan(*semver.New("8.0.0"))
legacySSH := sshutil.DetectOpenSSHVersion(ctx, sshExe).LessThan(*semver.New("8.0.0"))
for _, arg := range args {
if runtime.GOOS == "windows" {
if filepath.IsAbs(arg) {
arg, err = ioutilx.WindowsSubsystemPath(arg)
arg, err = ioutilx.WindowsSubsystemPath(ctx, arg)
if err != nil {
return err
}
Expand All @@ -107,7 +107,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
scpArgs = append(scpArgs, arg)
case 2:
instName := path[0]
inst, err := store.Inspect(instName)
inst, err := store.Inspect(ctx, instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q does not exist, run `limactl create %s` to create a new instance", instName, instName)
Expand Down Expand Up @@ -144,7 +144,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
sshOpts, err = sshutil.SSHOpts(sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false)
sshOpts, err = sshutil.SSHOpts(ctx, sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false)
if err != nil {
return err
}
Expand All @@ -155,7 +155,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
sshOpts, err = sshutil.CommonOpts(sshExe, false)
sshOpts, err = sshutil.CommonOpts(ctx, sshExe, false)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/limactl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ func newDeleteCommand() *cobra.Command {
}

func deleteAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
for _, instName := range args {
inst, err := store.Inspect(instName)
inst, err := store.Inspect(ctx, instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
logrus.Warnf("Ignoring non-existent instance %q", instName)
Expand All @@ -50,7 +51,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
}
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
deleted, err := autostart.DeleteStartAtLoginEntry(runtime.GOOS, instName)
deleted, err := autostart.DeleteStartAtLoginEntry(ctx, runtime.GOOS, instName)
if err != nil && !errors.Is(err, os.ErrNotExist) {
logrus.WithError(err).Warnf("The autostart file for instance %q does not exist", instName)
} else if deleted {
Expand Down
20 changes: 12 additions & 8 deletions cmd/limactl/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ $ limactl disk create DISK --size SIZE [--format qcow2]
}

func diskCreateAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
size, err := cmd.Flags().GetString("size")
if err != nil {
return err
Expand Down Expand Up @@ -112,8 +113,8 @@ func diskCreateAction(cmd *cobra.Command, args []string) error {

// qemu may not be available, use it only if needed.
dataDisk := filepath.Join(diskDir, filenames.DataDisk)
diskUtil := proxyimgutil.NewDiskUtil()
err = diskUtil.CreateDisk(dataDisk, diskSize)
diskUtil := proxyimgutil.NewDiskUtil(ctx)
err = diskUtil.CreateDisk(ctx, dataDisk, diskSize)
if err != nil {
rerr := os.RemoveAll(diskDir)
if rerr != nil {
Expand Down Expand Up @@ -232,6 +233,7 @@ $ limactl disk delete DISK1 DISK2 ...
}

func diskDeleteAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
Expand All @@ -243,7 +245,7 @@ func diskDeleteAction(cmd *cobra.Command, args []string) error {
}
var instances []*store.Instance
for _, instName := range instNames {
inst, err := store.Inspect(instName)
inst, err := store.Inspect(ctx, instName)
if err != nil {
continue
}
Expand Down Expand Up @@ -319,7 +321,8 @@ $ limactl disk unlock DISK1 DISK2 ...
return diskUnlockCommand
}

func diskUnlockAction(_ *cobra.Command, args []string) error {
func diskUnlockAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
for _, diskName := range args {
disk, err := store.InspectDisk(diskName)
if err != nil {
Expand All @@ -334,7 +337,7 @@ func diskUnlockAction(_ *cobra.Command, args []string) error {
continue
}
// if store.Inspect throws an error, the instance does not exist, and it is safe to unlock
inst, err := store.Inspect(disk.Instance)
inst, err := store.Inspect(ctx, disk.Instance)
if err == nil {
if len(inst.Errors) > 0 {
logrus.Warnf("Cannot unlock disk %q, attached instance %q has errors: %+v",
Expand Down Expand Up @@ -371,6 +374,7 @@ $ limactl disk resize DISK --size SIZE`,
}

func diskResizeAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
size, err := cmd.Flags().GetString("size")
if err != nil {
return err
Expand All @@ -396,7 +400,7 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
}

if disk.Instance != "" {
inst, err := store.Inspect(disk.Instance)
inst, err := store.Inspect(ctx, disk.Instance)
if err == nil {
if inst.Status == store.StatusRunning {
return fmt.Errorf("cannot resize disk %q used by running instance %q. Please stop the VM instance", diskName, disk.Instance)
Expand All @@ -406,8 +410,8 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {

// qemu may not be available, use it only if needed.
dataDisk := filepath.Join(disk.Dir, filenames.DataDisk)
diskUtil := proxyimgutil.NewDiskUtil()
err = diskUtil.ResizeDisk(dataDisk, diskSize)
diskUtil := proxyimgutil.NewDiskUtil(ctx)
err = diskUtil.ResizeDisk(ctx, dataDisk, diskSize)
if err != nil {
return fmt.Errorf("failed to resize disk %q: %w", diskName, err)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func newEditCommand() *cobra.Command {
}

func editAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
var arg string
if len(args) > 0 {
arg = args[0]
Expand All @@ -51,7 +52,7 @@ func editAction(cmd *cobra.Command, args []string) error {
arg = DefaultInstanceName
}
if err := store.ValidateInstName(arg); err == nil {
inst, err = store.Inspect(arg)
inst, err = store.Inspect(ctx, arg)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q not found", arg)
Expand Down Expand Up @@ -100,7 +101,7 @@ func editAction(cmd *cobra.Command, args []string) error {
hdr += "# and an empty file will abort the edit.\n"
hdr += "\n"
hdr += editutil.GenerateEditorWarningHeader()
yBytes, err = editutil.OpenEditor(yContent, hdr)
yBytes, err = editutil.OpenEditor(ctx, yContent, hdr)
if err != nil {
return err
}
Expand All @@ -113,15 +114,15 @@ func editAction(cmd *cobra.Command, args []string) error {
logrus.Info("Aborting, no changes made to the instance")
return nil
}
y, err := limayaml.LoadWithWarnings(yBytes, filePath)
y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
if err != nil {
return err
}
if err := limayaml.Validate(y, true); err != nil {
return saveRejectedYAML(yBytes, err)
}

if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil {
if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
return saveRejectedYAML(yBytes, err)
}

Expand All @@ -148,15 +149,14 @@ func editAction(cmd *cobra.Command, args []string) error {
if !startNow {
return nil
}
ctx := cmd.Context()
err = networks.Reconcile(ctx, inst.Name)
if err != nil {
return err
}

// store.Inspect() syncs values between inst.YAML and the store.
// This call applies the validated template to the store.
inst, err = store.Inspect(inst.Name)
inst, err = store.Inspect(ctx, inst.Name)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/limactl/factory-reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ func newFactoryResetCommand() *cobra.Command {
return resetCommand
}

func factoryResetAction(_ *cobra.Command, args []string) error {
func factoryResetAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
instName := DefaultInstanceName
if len(args) > 0 {
instName = args[0]
}

inst, err := store.Inspect(instName)
inst, err := store.Inspect(ctx, instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
logrus.Infof("Instance %q not found", instName)
Expand Down Expand Up @@ -69,7 +70,7 @@ func factoryResetAction(_ *cobra.Command, args []string) error {
}
}
// Regenerate the cloud-config.yaml, to reflect any changes to the global _config
if err := cidata.GenerateCloudConfig(inst.Dir, instName, inst.Config); err != nil {
if err := cidata.GenerateCloudConfig(ctx, inst.Dir, instName, inst.Config); err != nil {
logrus.Error(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
if showProgress {
opts = append(opts, hostagent.WithCloudInitProgress(showProgress))
}
ha, err := hostagent.New(instName, stdout, signalCh, opts...)
ha, err := hostagent.New(ctx, instName, stdout, signalCh, opts...)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/limactl/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func newInfoCommand() *cobra.Command {
}

func infoAction(cmd *cobra.Command, _ []string) error {
info, err := limainfo.New()
ctx := cmd.Context()
info, err := limainfo.New(ctx)
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 @@ -91,6 +91,7 @@ func (unmatchedInstancesError) ExitCode() int {
}

func listAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
Expand Down Expand Up @@ -173,7 +174,7 @@ func listAction(cmd *cobra.Command, args []string) error {
// get the state and config for all the requested instances
var instances []*store.Instance
for _, instanceName := range instanceNames {
instance, err := store.Inspect(instanceName)
instance, err := store.Inspect(ctx, instanceName)
if err != nil {
return fmt.Errorf("unable to load instance %s: %w", instanceName, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/limactl/protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ The instance is not being protected against removal via '/bin/rm', Finder, etc.`
return protectCommand
}

func protectAction(_ *cobra.Command, args []string) error {
func protectAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
var errs []error
for _, instName := range args {
inst, err := store.Inspect(instName)
inst, err := store.Inspect(ctx, instName)
if err != nil {
errs = append(errs, fmt.Errorf("failed to inspect instance %q: %w", instName, err))
continue
Expand Down
Loading