Skip to content

Commit a43f7e6

Browse files
committed
feat(cli): add down command to kill daemon and cleanup
1 parent 3c23c7b commit a43f7e6

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

cmd/devproxy/main.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import (
2323
func main() {
2424
if len(os.Args) < 2 {
2525
fmt.Fprintln(os.Stderr, "usage: devproxy <command>")
26-
fmt.Fprintln(os.Stderr, "commands: daemon, status, cleanup, doctor, windows-setup, windows-cleanup")
26+
fmt.Fprintln(os.Stderr, "commands: daemon, down, status, cleanup, doctor, windows-setup, windows-cleanup")
2727
os.Exit(1)
2828
}
2929

3030
switch os.Args[1] {
3131
case "daemon":
3232
runDaemon()
33+
case "down":
34+
runDown()
3335
case "status":
3436
runStatus()
3537
case "cleanup":
@@ -67,6 +69,42 @@ func runDaemon() {
6769
}
6870
}
6971

72+
func runDown() {
73+
// Find and kill all devproxy daemon processes
74+
out, err := exec.Command("pgrep", "-f", "devproxy daemon").Output()
75+
if err != nil {
76+
fmt.Println("No devproxy daemon running")
77+
return
78+
}
79+
80+
pids := strings.Fields(strings.TrimSpace(string(out)))
81+
myPid := fmt.Sprintf("%d", os.Getpid())
82+
83+
killed := 0
84+
for _, pid := range pids {
85+
if pid == myPid {
86+
continue
87+
}
88+
if err := exec.Command("kill", "-9", pid).Run(); err != nil {
89+
fmt.Fprintf(os.Stderr, "failed to kill PID %s: %v\n", pid, err)
90+
} else {
91+
fmt.Printf("killed devproxy daemon (PID %s)\n", pid)
92+
killed++
93+
}
94+
}
95+
96+
if killed == 0 {
97+
fmt.Println("No devproxy daemon running")
98+
return
99+
}
100+
101+
// Cleanup stale state
102+
s := state.New("")
103+
m := ipman.New(s)
104+
m.CleanupLoopbackIPs()
105+
fmt.Println("Cleaned up loopback IPs")
106+
}
107+
70108
func runStatus() {
71109
client := unixClient("/run/devproxy/devproxy.sock")
72110
resp, err := client.Get("http://devproxy/status")

0 commit comments

Comments
 (0)