Skip to content

Commit

Permalink
move proxy cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kahnwong committed Jun 18, 2024
1 parent 6b0ca31 commit cee69a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
15 changes: 1 addition & 14 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cmd

import (
"fmt"
"log"
"os"
"os/exec"
"syscall"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -60,22 +58,11 @@ var connectCmd = &cobra.Command{

// clean up proxy PID
if connInfo.ProxyKind != "" {
cleanup(proxyCmd)
killProxyPid(proxyCmd)
}
},
}

func init() {
rootCmd.AddCommand(connectCmd)
}

// functions
func cleanup(cmd *exec.Cmd) {
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
err = syscall.Kill(-pgid, syscall.SIGKILL)
if err != nil {
log.Fatal(err)
}
}
}
11 changes: 11 additions & 0 deletions cmd/utils_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"
"math/rand"
"os"
"os/exec"
Expand Down Expand Up @@ -46,3 +47,13 @@ func CreateProxy(c Connection) (*exec.Cmd, int) {

return cmd, port
}

func killProxyPid(cmd *exec.Cmd) {
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
err = syscall.Kill(-pgid, syscall.SIGKILL)
if err != nil {
log.Fatal(err)
}
}
}

0 comments on commit cee69a4

Please sign in to comment.