Skip to content

Commit 1daec3a

Browse files
authored
Merge pull request #3877 from zyfy29/parse-todo-context-by-parameter
refactor: pass context.TODO() by parameter
2 parents e52cd77 + aa5a42c commit 1daec3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+397
-380
lines changed

cmd/lima-driver-qemu/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package main
55

66
import (
7+
"context"
8+
79
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
810
"github.com/lima-vm/lima/v2/pkg/driver/qemu"
911
)
1012

1113
// To be used as an external driver for Lima.
1214
func main() {
13-
server.Serve(qemu.New())
15+
server.Serve(context.Background(), qemu.New())
1416
}

cmd/lima-driver-vz/main_darwin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package main
55

66
import (
7+
"context"
8+
79
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
810
"github.com/lima-vm/lima/v2/pkg/driver/vz"
911
)
1012

1113
// To be used as an external driver for Lima.
1214
func main() {
13-
server.Serve(vz.New())
15+
server.Serve(context.Background(), vz.New())
1416
}

cmd/lima-driver-wsl2/main_windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package main
55

66
import (
7+
"context"
8+
79
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
810
"github.com/lima-vm/lima/v2/pkg/driver/wsl2"
911
)
1012

1113
// To be used as an external driver for Lima.
1214
func main() {
13-
server.Serve(wsl2.New())
15+
server.Serve(context.Background(), wsl2.New())
1416
}

cmd/lima-guestagent/daemon_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
6262
return ticker.C, ticker.Stop
6363
}
6464

65-
agent, err := guestagent.New(newTicker, tick*20)
65+
agent, err := guestagent.New(ctx, newTicker, tick*20)
6666
if err != nil {
6767
return err
6868
}

cmd/limactl/clone.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func cloneAction(cmd *cobra.Command, args []string) error {
4646
}
4747

4848
oldInstName, newInstName := args[0], args[1]
49-
oldInst, err := store.Inspect(oldInstName)
49+
oldInst, err := store.Inspect(ctx, oldInstName)
5050
if err != nil {
5151
if errors.Is(err, os.ErrNotExist) {
5252
return fmt.Errorf("instance %q not found", oldInstName)
@@ -75,20 +75,20 @@ func cloneAction(cmd *cobra.Command, args []string) error {
7575
if err != nil {
7676
return err
7777
}
78-
y, err := limayaml.LoadWithWarnings(yBytes, filePath)
78+
y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
7979
if err != nil {
8080
return err
8181
}
8282
if err := limayaml.Validate(y, true); err != nil {
8383
return saveRejectedYAML(yBytes, err)
8484
}
85-
if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil {
85+
if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
8686
return saveRejectedYAML(yBytes, err)
8787
}
8888
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
8989
return err
9090
}
91-
newInst, err = store.Inspect(newInst.Name)
91+
newInst, err = store.Inspect(ctx, newInst.Name)
9292
if err != nil {
9393
return err
9494
}

cmd/limactl/copy.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ func copyAction(cmd *cobra.Command, args []string) error {
8989
if err != nil {
9090
return err
9191
}
92-
legacySSH := sshutil.DetectOpenSSHVersion(sshExe).LessThan(*semver.New("8.0.0"))
92+
legacySSH := sshutil.DetectOpenSSHVersion(ctx, sshExe).LessThan(*semver.New("8.0.0"))
9393
for _, arg := range args {
9494
if runtime.GOOS == "windows" {
9595
if filepath.IsAbs(arg) {
96-
arg, err = ioutilx.WindowsSubsystemPath(arg)
96+
arg, err = ioutilx.WindowsSubsystemPath(ctx, arg)
9797
if err != nil {
9898
return err
9999
}
@@ -107,7 +107,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
107107
scpArgs = append(scpArgs, arg)
108108
case 2:
109109
instName := path[0]
110-
inst, err := store.Inspect(instName)
110+
inst, err := store.Inspect(ctx, instName)
111111
if err != nil {
112112
if errors.Is(err, os.ErrNotExist) {
113113
return fmt.Errorf("instance %q does not exist, run `limactl create %s` to create a new instance", instName, instName)
@@ -144,7 +144,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
144144
if err != nil {
145145
return err
146146
}
147-
sshOpts, err = sshutil.SSHOpts(sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false)
147+
sshOpts, err = sshutil.SSHOpts(ctx, sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false)
148148
if err != nil {
149149
return err
150150
}
@@ -155,7 +155,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
155155
if err != nil {
156156
return err
157157
}
158-
sshOpts, err = sshutil.CommonOpts(sshExe, false)
158+
sshOpts, err = sshutil.CommonOpts(ctx, sshExe, false)
159159
if err != nil {
160160
return err
161161
}

cmd/limactl/delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ func newDeleteCommand() *cobra.Command {
3333
}
3434

3535
func deleteAction(cmd *cobra.Command, args []string) error {
36+
ctx := cmd.Context()
3637
force, err := cmd.Flags().GetBool("force")
3738
if err != nil {
3839
return err
3940
}
4041
for _, instName := range args {
41-
inst, err := store.Inspect(instName)
42+
inst, err := store.Inspect(ctx, instName)
4243
if err != nil {
4344
if errors.Is(err, os.ErrNotExist) {
4445
logrus.Warnf("Ignoring non-existent instance %q", instName)
@@ -50,7 +51,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
5051
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
5152
}
5253
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
53-
deleted, err := autostart.DeleteStartAtLoginEntry(runtime.GOOS, instName)
54+
deleted, err := autostart.DeleteStartAtLoginEntry(ctx, runtime.GOOS, instName)
5455
if err != nil && !errors.Is(err, os.ErrNotExist) {
5556
logrus.WithError(err).Warnf("The autostart file for instance %q does not exist", instName)
5657
} else if deleted {

cmd/limactl/disk.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ $ limactl disk create DISK --size SIZE [--format qcow2]
7171
}
7272

7373
func diskCreateAction(cmd *cobra.Command, args []string) error {
74+
ctx := cmd.Context()
7475
size, err := cmd.Flags().GetString("size")
7576
if err != nil {
7677
return err
@@ -112,8 +113,8 @@ func diskCreateAction(cmd *cobra.Command, args []string) error {
112113

113114
// qemu may not be available, use it only if needed.
114115
dataDisk := filepath.Join(diskDir, filenames.DataDisk)
115-
diskUtil := proxyimgutil.NewDiskUtil()
116-
err = diskUtil.CreateDisk(dataDisk, diskSize)
116+
diskUtil := proxyimgutil.NewDiskUtil(ctx)
117+
err = diskUtil.CreateDisk(ctx, dataDisk, diskSize)
117118
if err != nil {
118119
rerr := os.RemoveAll(diskDir)
119120
if rerr != nil {
@@ -232,6 +233,7 @@ $ limactl disk delete DISK1 DISK2 ...
232233
}
233234

234235
func diskDeleteAction(cmd *cobra.Command, args []string) error {
236+
ctx := cmd.Context()
235237
force, err := cmd.Flags().GetBool("force")
236238
if err != nil {
237239
return err
@@ -243,7 +245,7 @@ func diskDeleteAction(cmd *cobra.Command, args []string) error {
243245
}
244246
var instances []*store.Instance
245247
for _, instName := range instNames {
246-
inst, err := store.Inspect(instName)
248+
inst, err := store.Inspect(ctx, instName)
247249
if err != nil {
248250
continue
249251
}
@@ -319,7 +321,8 @@ $ limactl disk unlock DISK1 DISK2 ...
319321
return diskUnlockCommand
320322
}
321323

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

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

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

407411
// qemu may not be available, use it only if needed.
408412
dataDisk := filepath.Join(disk.Dir, filenames.DataDisk)
409-
diskUtil := proxyimgutil.NewDiskUtil()
410-
err = diskUtil.ResizeDisk(dataDisk, diskSize)
413+
diskUtil := proxyimgutil.NewDiskUtil(ctx)
414+
err = diskUtil.ResizeDisk(ctx, dataDisk, diskSize)
411415
if err != nil {
412416
return fmt.Errorf("failed to resize disk %q: %w", diskName, err)
413417
}

cmd/limactl/edit.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func newEditCommand() *cobra.Command {
3838
}
3939

4040
func editAction(cmd *cobra.Command, args []string) error {
41+
ctx := cmd.Context()
4142
var arg string
4243
if len(args) > 0 {
4344
arg = args[0]
@@ -51,7 +52,7 @@ func editAction(cmd *cobra.Command, args []string) error {
5152
arg = DefaultInstanceName
5253
}
5354
if err := store.ValidateInstName(arg); err == nil {
54-
inst, err = store.Inspect(arg)
55+
inst, err = store.Inspect(ctx, arg)
5556
if err != nil {
5657
if errors.Is(err, os.ErrNotExist) {
5758
return fmt.Errorf("instance %q not found", arg)
@@ -100,7 +101,7 @@ func editAction(cmd *cobra.Command, args []string) error {
100101
hdr += "# and an empty file will abort the edit.\n"
101102
hdr += "\n"
102103
hdr += editutil.GenerateEditorWarningHeader()
103-
yBytes, err = editutil.OpenEditor(yContent, hdr)
104+
yBytes, err = editutil.OpenEditor(ctx, yContent, hdr)
104105
if err != nil {
105106
return err
106107
}
@@ -113,15 +114,15 @@ func editAction(cmd *cobra.Command, args []string) error {
113114
logrus.Info("Aborting, no changes made to the instance")
114115
return nil
115116
}
116-
y, err := limayaml.LoadWithWarnings(yBytes, filePath)
117+
y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
117118
if err != nil {
118119
return err
119120
}
120121
if err := limayaml.Validate(y, true); err != nil {
121122
return saveRejectedYAML(yBytes, err)
122123
}
123124

124-
if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil {
125+
if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
125126
return saveRejectedYAML(yBytes, err)
126127
}
127128

@@ -148,15 +149,14 @@ func editAction(cmd *cobra.Command, args []string) error {
148149
if !startNow {
149150
return nil
150151
}
151-
ctx := cmd.Context()
152152
err = networks.Reconcile(ctx, inst.Name)
153153
if err != nil {
154154
return err
155155
}
156156

157157
// store.Inspect() syncs values between inst.YAML and the store.
158158
// This call applies the validated template to the store.
159-
inst, err = store.Inspect(inst.Name)
159+
inst, err = store.Inspect(ctx, inst.Name)
160160
if err != nil {
161161
return err
162162
}

cmd/limactl/factory-reset.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ func newFactoryResetCommand() *cobra.Command {
3030
return resetCommand
3131
}
3232

33-
func factoryResetAction(_ *cobra.Command, args []string) error {
33+
func factoryResetAction(cmd *cobra.Command, args []string) error {
34+
ctx := cmd.Context()
3435
instName := DefaultInstanceName
3536
if len(args) > 0 {
3637
instName = args[0]
3738
}
3839

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

0 commit comments

Comments
 (0)