Skip to content

Commit fffdae1

Browse files
ashwat287Ashwat KUMAR SINGH
authored andcommitted
refactor: reduce context.Background()
Signed-off-by: nerdylucifer <[email protected]>
1 parent 86a7a5a commit fffdae1

34 files changed

+174
-147
lines changed

cmd/limactl/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func cloneAction(cmd *cobra.Command, args []string) error {
7272
if err != nil {
7373
return err
7474
}
75-
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
75+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
7676
if err != nil {
7777
return err
7878
}

cmd/limactl/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
4747
}
4848
return err
4949
}
50-
if err := instance.Delete(cmd.Context(), inst, force); err != nil {
50+
if err := instance.Delete(ctx, inst, force); err != nil {
5151
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
5252
}
5353
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
@@ -60,7 +60,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
6060
}
6161
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
6262
}
63-
return networks.Reconcile(cmd.Context(), "")
63+
return networks.Reconcile(ctx, "")
6464
}
6565

6666
func deleteBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func editAction(cmd *cobra.Command, args []string) error {
9090
var yBytes []byte
9191
if len(yqExprs) > 0 {
9292
yq := yqutil.Join(yqExprs)
93-
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
93+
yBytes, err = yqutil.EvaluateExpression(ctx, yq, yContent)
9494
if err != nil {
9595
return err
9696
}

cmd/limactl/hostagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
130130
}
131131
}()
132132
defer srv.Close()
133-
return ha.Run(cmd.Context())
133+
return ha.Run(ctx)
134134
}
135135

136136
// syncer is implemented by *os.File.

cmd/limactl/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func listAction(cmd *cobra.Command, args []string) error {
287287
}
288288
} else {
289289
var res []byte
290-
if res, err = yqutil.EvaluateExpression(yqExpr, buf.Bytes()); err != nil {
290+
if res, err = yqutil.EvaluateExpression(ctx, yqExpr, buf.Bytes()); err != nil {
291291
return err
292292
}
293293
str = string(res)

cmd/limactl/network.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -174,6 +175,8 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
174175
return err
175176
}
176177

178+
ctx := cmd.Context()
179+
177180
switch mode {
178181
case networks.ModeBridged:
179182
if gateway != "" {
@@ -183,7 +186,7 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
183186
return fmt.Errorf("network mode %q requires specifying interface", mode)
184187
}
185188
yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"interface":%q}`, name, mode, intf)
186-
return networkApplyYQ(yq)
189+
return networkApplyYQ(ctx, yq)
187190
default:
188191
if gateway == "" {
189192
return fmt.Errorf("network mode %q requires specifying gateway", mode)
@@ -208,11 +211,11 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
208211
// TODO: check IP range collision
209212

210213
yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"gateway":%q,"netmask":%q,"interface":%q}`, name, mode, gwIP.String(), gwMaskStr, intf)
211-
return networkApplyYQ(yq)
214+
return networkApplyYQ(ctx, yq)
212215
}
213216
}
214217

215-
func networkApplyYQ(yq string) error {
218+
func networkApplyYQ(ctx context.Context, yq string) error {
216219
filePath, err := networks.ConfigFile()
217220
if err != nil {
218221
return err
@@ -221,7 +224,7 @@ func networkApplyYQ(yq string) error {
221224
if err != nil {
222225
return err
223226
}
224-
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
227+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
225228
if err != nil {
226229
return err
227230
}
@@ -263,7 +266,7 @@ func networkDeleteAction(cmd *cobra.Command, args []string) error {
263266
yq += " | "
264267
}
265268
}
266-
return networkApplyYQ(yq)
269+
return networkApplyYQ(cmd.Context(), yq)
267270
}
268271

269272
func networkBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/start.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
307307
return nil, err
308308
}
309309
} else {
310-
tmpl, err = limatmpl.Read(cmd.Context(), name, arg)
310+
tmpl, err = limatmpl.Read(ctx, name, arg)
311311
if err != nil {
312312
return nil, err
313313
}
@@ -321,7 +321,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
321321
}
322322
}
323323

324-
if err := tmpl.Embed(cmd.Context(), true, true); err != nil {
324+
if err := tmpl.Embed(ctx, true, true); err != nil {
325325
return nil, err
326326
}
327327
yqExprs, err := editflags.YQExpressions(flags, true)
@@ -331,18 +331,18 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
331331
yq := yqutil.Join(yqExprs)
332332
if tty {
333333
var err error
334-
tmpl, err = chooseNextCreatorState(cmd.Context(), tmpl, yq)
334+
tmpl, err = chooseNextCreatorState(ctx, tmpl, yq)
335335
if err != nil {
336336
return nil, err
337337
}
338338
} else {
339339
logrus.Info("Terminal is not available, proceeding without opening an editor")
340-
if err := modifyInPlace(tmpl, yq); err != nil {
340+
if err := modifyInPlace(ctx, tmpl, yq); err != nil {
341341
return nil, err
342342
}
343343
}
344344
saveBrokenYAML := tty
345-
return instance.Create(cmd.Context(), tmpl.Name, tmpl.Bytes, saveBrokenYAML)
345+
return instance.Create(ctx, tmpl.Name, tmpl.Bytes, saveBrokenYAML)
346346
}
347347

348348
func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Instance, yq string) (*limatype.Instance, error) {
@@ -355,7 +355,7 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Ins
355355
return nil, err
356356
}
357357
logrus.Debugf("Applying yq expression %q to an existing instance %q", yq, inst.Name)
358-
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
358+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
359359
if err != nil {
360360
return nil, err
361361
}
@@ -381,8 +381,8 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Ins
381381
return store.Inspect(ctx, inst.Name)
382382
}
383383

384-
func modifyInPlace(st *limatmpl.Template, yq string) error {
385-
out, err := yqutil.EvaluateExpression(yq, st.Bytes)
384+
func modifyInPlace(ctx context.Context, st *limatmpl.Template, yq string) error {
385+
out, err := yqutil.EvaluateExpression(ctx, yq, st.Bytes)
386386
if err != nil {
387387
return err
388388
}
@@ -407,7 +407,7 @@ func (exitSuccessError) ExitCode() int {
407407

408408
func chooseNextCreatorState(ctx context.Context, tmpl *limatmpl.Template, yq string) (*limatmpl.Template, error) {
409409
for {
410-
if err := modifyInPlace(tmpl, yq); err != nil {
410+
if err := modifyInPlace(ctx, tmpl, yq); err != nil {
411411
logrus.WithError(err).Warn("Failed to evaluate yq expression")
412412
return tmpl, err
413413
}

cmd/limactl/template.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func templateCopyAction(cmd *cobra.Command, args []string) error {
125125
if embed && verbatim {
126126
return errors.New("--verbatim cannot be used with any of --embed, --embed-all, or --fill")
127127
}
128-
tmpl, err := limatmpl.Read(cmd.Context(), "", source)
128+
tmpl, err := limatmpl.Read(ctx, "", source)
129129
if err != nil {
130130
return err
131131
}
@@ -135,11 +135,11 @@ func templateCopyAction(cmd *cobra.Command, args []string) error {
135135
if !verbatim {
136136
if embed {
137137
// Embed default base.yaml only when fill is true.
138-
if err := tmpl.Embed(cmd.Context(), embedAll, fill); err != nil {
138+
if err := tmpl.Embed(ctx, embedAll, fill); err != nil {
139139
return err
140140
}
141141
} else {
142-
if err := tmpl.UseAbsLocators(); err != nil {
142+
if err := tmpl.UseAbsLocators(ctx); err != nil {
143143
return err
144144
}
145145
}
@@ -197,14 +197,14 @@ func templateYQAction(cmd *cobra.Command, args []string) error {
197197
ctx := cmd.Context()
198198
locator := args[0]
199199
expr := args[1]
200-
tmpl, err := limatmpl.Read(cmd.Context(), "", locator)
200+
tmpl, err := limatmpl.Read(ctx, "", locator)
201201
if err != nil {
202202
return err
203203
}
204204
if len(tmpl.Bytes) == 0 {
205205
return fmt.Errorf("don't know how to interpret %q as a template locator", locator)
206206
}
207-
if err := tmpl.Embed(cmd.Context(), true, true); err != nil {
207+
if err := tmpl.Embed(ctx, true, true); err != nil {
208208
return err
209209
}
210210
if err := fillDefaults(ctx, tmpl); err != nil {
@@ -241,7 +241,7 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
241241
}
242242

243243
for _, arg := range args {
244-
tmpl, err := limatmpl.Read(cmd.Context(), "", arg)
244+
tmpl, err := limatmpl.Read(ctx, "", arg)
245245
if err != nil {
246246
return err
247247
}
@@ -252,7 +252,7 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
252252
return fmt.Errorf("can't determine instance name from template locator %q", arg)
253253
}
254254
// Embed default base.yaml only when fill is true.
255-
if err := tmpl.Embed(cmd.Context(), true, fill); err != nil {
255+
if err := tmpl.Embed(ctx, true, fill); err != nil {
256256
return err
257257
}
258258
// Load() will merge the template with override.yaml and default.yaml via FillDefaults().

pkg/driver/driver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Lifecycle interface {
4646
type GUI interface {
4747
// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates
4848
// It returns error if there are any failures
49-
RunGUI() error
49+
RunGUI(context.Context) error
5050

5151
ChangeDisplayPassword(ctx context.Context, password string) error
5252
DisplayConnection(ctx context.Context) (string, error)
@@ -63,7 +63,7 @@ type SnapshotManager interface {
6363
// GuestAgent defines operations for the guest agent.
6464
type GuestAgent interface {
6565
// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent.
66-
ForwardGuestAgent() bool
66+
ForwardGuestAgent(context.Context) bool
6767

6868
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
6969
GuestAgentConn(_ context.Context) (net.Conn, string, error)
@@ -76,12 +76,12 @@ type Driver interface {
7676
SnapshotManager
7777
GuestAgent
7878

79-
Info() Info
79+
Info(context.Context) Info
8080

8181
// Configure sets the configuration for the instance.
8282
// TODO: merge Configure and FillConfig?
8383
// Or come up with a better name to clarify the difference.
84-
Configure(inst *limatype.Instance) *ConfiguredDriver
84+
Configure(_ context.Context, inst *limatype.Instance) *ConfiguredDriver
8585

8686
// FillConfig fills and validates the configuration for the instance.
8787
// The config is not set to the instance.

pkg/driver/external/client/methods.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ func (d *DriverClient) FillConfig(_ context.Context, _ *limatype.LimaYAML, _ str
119119
return errors.New("pre-configured driver action not implemented in client driver")
120120
}
121121

122-
func (d *DriverClient) RunGUI() error {
122+
func (d *DriverClient) RunGUI(ctx context.Context) error {
123123
d.logger.Debug("Running GUI for the driver instance")
124124

125-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
125+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
126126
defer cancel()
127127

128128
_, err := d.DriverSvc.RunGUI(ctx, &emptypb.Empty{})
@@ -221,10 +221,10 @@ func (d *DriverClient) ListSnapshots(ctx context.Context) (string, error) {
221221
return resp.Snapshots, nil
222222
}
223223

224-
func (d *DriverClient) ForwardGuestAgent() bool {
224+
func (d *DriverClient) ForwardGuestAgent(ctx context.Context) bool {
225225
d.logger.Debug("Checking if guest agent needs to be forwarded")
226226

227-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
227+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
228228
defer cancel()
229229

230230
resp, err := d.DriverSvc.ForwardGuestAgent(ctx, &emptypb.Empty{})
@@ -247,10 +247,10 @@ func (d *DriverClient) GuestAgentConn(ctx context.Context) (net.Conn, string, er
247247
return nil, "", nil
248248
}
249249

250-
func (d *DriverClient) Info() driver.Info {
250+
func (d *DriverClient) Info(ctx context.Context) driver.Info {
251251
d.logger.Debug("Getting driver info")
252252

253-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
253+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
254254
defer cancel()
255255

256256
resp, err := d.DriverSvc.Info(ctx, &emptypb.Empty{})
@@ -269,7 +269,7 @@ func (d *DriverClient) Info() driver.Info {
269269
return info
270270
}
271271

272-
func (d *DriverClient) Configure(inst *limatype.Instance) *driver.ConfiguredDriver {
272+
func (d *DriverClient) Configure(ctx context.Context, inst *limatype.Instance) *driver.ConfiguredDriver {
273273
d.logger.Debugf("Setting config for instance %s with SSH local port %d", inst.Name, inst.SSHLocalPort)
274274

275275
instJSON, err := inst.MarshalJSON()
@@ -278,7 +278,7 @@ func (d *DriverClient) Configure(inst *limatype.Instance) *driver.ConfiguredDriv
278278
return nil
279279
}
280280

281-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
281+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
282282
defer cancel()
283283

284284
_, err = d.DriverSvc.Configure(ctx, &pb.SetConfigRequest{

0 commit comments

Comments
 (0)