From f33ccec5c49f2295353a5c205879ef7f88247963 Mon Sep 17 00:00:00 2001
From: CyberL1 <mateusz.hf18@gmail.com>
Date: Sun, 16 Feb 2025 09:30:02 +0100
Subject: [PATCH] feat: add command to stop running holesail processes

---
 cmd/down.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 cmd/up.go   |  5 ++++-
 go.mod      |  6 ++++++
 go.sum      | 13 +++++++++++++
 4 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 cmd/down.go

diff --git a/cmd/down.go b/cmd/down.go
new file mode 100644
index 0000000..8630909
--- /dev/null
+++ b/cmd/down.go
@@ -0,0 +1,46 @@
+package cmd
+
+import (
+	"fmt"
+	"strings"
+	"syscall"
+
+	"github.com/shirou/gopsutil/process"
+	"github.com/spf13/cobra"
+)
+
+func init() {
+	rootCmd.AddCommand(downCmd)
+}
+
+var downCmd = &cobra.Command{
+	Use:   "down",
+	Short: "Stop running holesail processes",
+	Args:  cobra.NoArgs,
+	Run: func(cmd *cobra.Command, args []string) {
+		processes, err := process.Processes()
+		if err != nil {
+			fmt.Println(err)
+			return
+		}
+
+		var stoppedProcesses int
+
+		for _, process := range processes {
+			processName, _ := process.Name()
+			cmdLine, _ := process.Cmdline()
+
+			if processName == "node" {
+				cmdLineSplitted := strings.Split(cmdLine, "/")
+				cmdLineWithoutBloat := strings.Join(cmdLineSplitted[len(cmdLineSplitted)-3:], "/")
+
+				if strings.HasPrefix(cmdLineWithoutBloat, "node_modules/holesail/index.js") {
+					process.SendSignal(syscall.SIGINT)
+					stoppedProcesses++
+				}
+			}
+		}
+
+		fmt.Printf("Stopped %v holesail processes\n", stoppedProcesses)
+	},
+}
diff --git a/cmd/up.go b/cmd/up.go
index ccc43f1..843d9fe 100644
--- a/cmd/up.go
+++ b/cmd/up.go
@@ -84,7 +84,10 @@ var upCmd = &cobra.Command{
 			httputil.NewSingleHostReverseProxy(appUrl).ServeHTTP(w, r)
 		})
 
-		http.ListenAndServe(":80", nil)
+		err := http.ListenAndServe(":80", nil)
+		if err != nil {
+			fmt.Println(err)
+		}
 	},
 }
 
diff --git a/go.mod b/go.mod
index 7859964..3630d8b 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,12 @@ go 1.23.5
 require github.com/spf13/cobra v1.8.1
 
 require (
+	github.com/go-ole/go-ole v1.2.6 // indirect
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
+	github.com/shirou/gopsutil v3.21.11+incompatible // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
+	github.com/tklauser/go-sysconf v0.3.14 // indirect
+	github.com/tklauser/numcpus v0.8.0 // indirect
+	github.com/yusufpapurcu/wmi v1.2.4 // indirect
+	golang.org/x/sys v0.30.0 // indirect
 )
diff --git a/go.sum b/go.sum
index 912390a..2591daa 100644
--- a/go.sum
+++ b/go.sum
@@ -1,10 +1,23 @@
 github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
+github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
 github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
+github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
 github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
 github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
+github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
+github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
+github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
+github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
+github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
+golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
+golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=