Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edd595c

Browse files
committed
fix: Ensure gitssh cleans up temporary file on interrupt
1 parent 1a5d3ea commit edd595c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cli/gitssh.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"os/signal"
78
"strings"
89

910
"github.com/spf13/cobra"
@@ -18,11 +19,18 @@ func gitssh() *cobra.Command {
1819
Hidden: true,
1920
Short: `Wraps the "ssh" command and uses the coder gitssh key for authentication`,
2021
RunE: func(cmd *cobra.Command, args []string) error {
22+
ctx := cmd.Context()
23+
24+
// Catch interrupt signals as a best-effort attempt to clean
25+
// up the temporary key file.
26+
ctx, stop := signal.NotifyContext(ctx, interruptSignals...)
27+
defer stop()
28+
2129
client, err := createAgentClient(cmd)
2230
if err != nil {
2331
return xerrors.Errorf("create agent client: %w", err)
2432
}
25-
key, err := client.AgentGitSSHKey(cmd.Context())
33+
key, err := client.AgentGitSSHKey(ctx)
2634
if err != nil {
2735
return xerrors.Errorf("get agent git ssh token: %w", err)
2836
}
@@ -45,7 +53,7 @@ func gitssh() *cobra.Command {
4553
}
4654

4755
args = append([]string{"-i", privateKeyFile.Name()}, args...)
48-
c := exec.CommandContext(cmd.Context(), "ssh", args...)
56+
c := exec.CommandContext(ctx, "ssh", args...)
4957
c.Stderr = cmd.ErrOrStderr()
5058
c.Stdout = cmd.OutOrStdout()
5159
c.Stdin = cmd.InOrStdin()

0 commit comments

Comments
 (0)