Skip to content

Commit aa81751

Browse files
committed
Fix noctx linter errors
``` Error: cmd/lima-guestagent/daemon_linux.go:89:29: net.Listen must not be called. use (*net.ListenConfig).Listen (noctx) socketL, err := net.Listen("unix", socket) ^ Error: cmd/lima-guestagent/install_systemd_linux.go:69:22: os/exec.Command must not be called. use os/exec.CommandContext (noctx) cmd := exec.Command("systemctl", append([]string{"--system"}, args...)...) ... ^ ``` Signed-off-by: Akihiro Suda <[email protected]>
1 parent e0e64cd commit aa81751

File tree

33 files changed

+125
-60
lines changed

33 files changed

+125
-60
lines changed

cmd/lima-guestagent/daemon_linux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func newDaemonCommand() *cobra.Command {
3232
}
3333

3434
func daemonAction(cmd *cobra.Command, _ []string) error {
35+
ctx := cmd.Context()
3536
socket := "/run/lima-guestagent.sock"
3637
tick, err := cmd.Flags().GetDuration("tick")
3738
if err != nil {
@@ -86,7 +87,8 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
8687
l = vsockL
8788
logrus.Infof("serving the guest agent on vsock port: %d", vSockPort)
8889
} else {
89-
socketL, err := net.Listen("unix", socket)
90+
var lc net.ListenConfig
91+
socketL, err := lc.Listen(ctx, "unix", socket)
9092
if err != nil {
9193
return err
9294
}

cmd/lima-guestagent/install_systemd_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func newInstallSystemdCommand() *cobra.Command {
3030
}
3131

3232
func installSystemdAction(cmd *cobra.Command, _ []string) error {
33+
ctx := cmd.Context()
3334
vsockPort, err := cmd.Flags().GetInt("vsock-port")
3435
if err != nil {
3536
return err
@@ -66,7 +67,7 @@ func installSystemdAction(cmd *cobra.Command, _ []string) error {
6667
{"try-restart", "lima-guestagent.service"},
6768
}
6869
for _, args := range args {
69-
cmd := exec.Command("systemctl", append([]string{"--system"}, args...)...)
70+
cmd := exec.CommandContext(ctx, "systemctl", append([]string{"--system"}, args...)...)
7071
cmd.Stdout = os.Stdout
7172
cmd.Stderr = os.Stderr
7273
logrus.Infof("Executing: %s", strings.Join(cmd.Args, " "))

cmd/limactl/copy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func newCopyCommand() *cobra.Command {
4848
}
4949

5050
func copyAction(cmd *cobra.Command, args []string) error {
51+
ctx := cmd.Context()
5152
recursive, err := cmd.Flags().GetBool("recursive")
5253
if err != nil {
5354
return err
@@ -161,7 +162,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
161162
}
162163
sshArgs := sshutil.SSHArgsFromOpts(sshOpts)
163164

164-
sshCmd := exec.Command(arg0, append(sshArgs, scpArgs...)...)
165+
sshCmd := exec.CommandContext(ctx, arg0, append(sshArgs, scpArgs...)...)
165166
sshCmd.Stdin = cmd.InOrStdin()
166167
sshCmd.Stdout = cmd.OutOrStdout()
167168
sshCmd.Stderr = cmd.ErrOrStderr()

cmd/limactl/hostagent.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func newHostagentCommand() *cobra.Command {
4040
}
4141

4242
func hostagentAction(cmd *cobra.Command, args []string) error {
43+
ctx := cmd.Context()
4344
pidfile, err := cmd.Flags().GetString("pidfile")
4445
if err != nil {
4546
return err
@@ -117,7 +118,8 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
117118
if err != nil {
118119
return err
119120
}
120-
l, err := net.Listen("unix", socket)
121+
var lc net.ListenConfig
122+
l, err := lc.Listen(ctx, "unix", socket)
121123
logrus.Infof("hostagent socket created at %s", socket)
122124
if err != nil {
123125
return err

cmd/limactl/shell.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func newShellCommand() *cobra.Command {
6060
}
6161

6262
func shellAction(cmd *cobra.Command, args []string) error {
63+
ctx := cmd.Context()
6364
// simulate the behavior of double dash
6465
newArg := []string{}
6566
if len(args) >= 2 && args[1] == "--" {
@@ -254,7 +255,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
254255
"--",
255256
script,
256257
}...)
257-
sshCmd := exec.Command(sshExe.Exe, sshArgs...)
258+
sshCmd := exec.CommandContext(ctx, sshExe.Exe, sshArgs...)
258259
sshCmd.Stdin = os.Stdin
259260
sshCmd.Stdout = os.Stdout
260261
sshCmd.Stderr = os.Stderr

cmd/limactl/tunnel.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func newTunnelCommand() *cobra.Command {
4747
}
4848

4949
func tunnelAction(cmd *cobra.Command, args []string) error {
50+
ctx := cmd.Context()
5051
flags := cmd.Flags()
5152
tunnelType, err := flags.GetString("type")
5253
if err != nil {
@@ -108,7 +109,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
108109
"-p", strconv.Itoa(inst.SSHLocalPort),
109110
inst.SSHAddress,
110111
}...)
111-
sshCmd := exec.Command(sshExe.Exe, sshArgs...)
112+
sshCmd := exec.CommandContext(ctx, sshExe.Exe, sshArgs...)
112113
sshCmd.Stdout = stderr
113114
sshCmd.Stderr = stderr
114115
logrus.Debugf("executing ssh (may take a long)): %+v", sshCmd.Args)

pkg/autostart/autostart.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package autostart
66

77
import (
8+
"context"
89
_ "embed"
910
"errors"
1011
"fmt"
@@ -78,6 +79,7 @@ func GetFilePath(hostOS, instName string) string {
7879
}
7980

8081
func enableDisableService(action, hostOS, serviceWithPath string) error {
82+
ctx := context.TODO()
8183
// Get filename without extension
8284
filename := strings.TrimSuffix(path.Base(serviceWithPath), filepath.Ext(path.Base(serviceWithPath)))
8385

@@ -97,7 +99,7 @@ func enableDisableService(action, hostOS, serviceWithPath string) error {
9799
filename,
98100
}...)
99101
}
100-
cmd := exec.Command(args[0], args[1:]...)
102+
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
101103
cmd.Stdout = os.Stdout
102104
cmd.Stderr = os.Stderr
103105
return cmd.Run()

pkg/downloader/downloader_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,12 @@ func TestDownloadCompressed(t *testing.T) {
316316
}
317317

318318
t.Run("gzip", func(t *testing.T) {
319+
ctx := t.Context()
319320
localPath := filepath.Join(t.TempDir(), t.Name())
320321
localFile := filepath.Join(t.TempDir(), "test-file")
321322
testDownloadCompressedContents := []byte("TestDownloadCompressed")
322323
assert.NilError(t, os.WriteFile(localFile, testDownloadCompressedContents, 0o644))
323-
assert.NilError(t, exec.Command("gzip", localFile).Run())
324+
assert.NilError(t, exec.CommandContext(ctx, "gzip", localFile).Run())
324325
localFile += ".gz"
325326
testLocalFileURL := "file://" + localFile
326327

@@ -334,11 +335,12 @@ func TestDownloadCompressed(t *testing.T) {
334335
})
335336

336337
t.Run("bzip2", func(t *testing.T) {
338+
ctx := t.Context()
337339
localPath := filepath.Join(t.TempDir(), t.Name())
338340
localFile := filepath.Join(t.TempDir(), "test-file")
339341
testDownloadCompressedContents := []byte("TestDownloadCompressed")
340342
assert.NilError(t, os.WriteFile(localFile, testDownloadCompressedContents, 0o644))
341-
assert.NilError(t, exec.Command("bzip2", localFile).Run())
343+
assert.NilError(t, exec.CommandContext(ctx, "bzip2", localFile).Run())
342344
localFile += ".bz2"
343345
testLocalFileURL := "file://" + localFile
344346

pkg/driver/external/client/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func NewDriverClient(socketPath string, logger *logrus.Logger) (*DriverClient, e
2525
opts := []grpc.DialOption{
2626
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(16 << 20)),
2727
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(16 << 20)),
28-
grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) {
29-
return net.Dial("unix", socketPath)
28+
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
29+
var dialer net.Dialer
30+
return dialer.DialContext(ctx, "unix", socketPath)
3031
}),
3132
grpc.WithTransportCredentials(insecure.NewCredentials()),
3233
}

pkg/driver/external/server/methods.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func (s *DriverServer) GuestAgentConn(ctx context.Context, _ *emptypb.Empty) (*e
7878
if connType != "unix" {
7979
proxySocketPath := filepath.Join(s.driver.Info().InstanceDir, filenames.GuestAgentSock)
8080

81-
listener, err := net.Listen("unix", proxySocketPath)
81+
var lc net.ListenConfig
82+
listener, err := lc.Listen(ctx, "unix", proxySocketPath)
8283
if err != nil {
8384
logrus.Errorf("Failed to create proxy socket: %v", err)
8485
return nil, err

0 commit comments

Comments
 (0)