Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionUsedPorts: support windows #1913

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions pkg/actions/net/port.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net

import (
"os/exec"
"runtime"
"strings"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -52,26 +52,59 @@ func ActionKnownPorts() carapace.Action {
// 8000 (used by: python)
func ActionUsedPorts() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO support other tools
if _, err := exec.LookPath("lsof"); err == nil {
return carapace.ActionExecCommand("lsof", "-i", "-P", "-n")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
switch runtime.GOOS {
case "darwin", "linux":
return actionUsedPortsLsof()
case "windows":
return actionUsedPortsNetstat()
default:
return carapace.ActionValues()
}
}).Tag("used ports")
}

func actionUsedPortsLsof() carapace.Action {
return carapace.ActionExecCommand("lsof", "-i", "-P", "-n")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for _, line := range lines {
if fields := strings.Fields(line); len(fields) > 9 {
if fields[len(fields)-1] != "(LISTEN)" {
continue
}

vals := make([]string, 0)
for _, line := range lines {
if fields := strings.Fields(line); len(fields) > 9 {
if fields[len(fields)-1] != "(LISTEN)" {
continue
}
if splitted := strings.Split(fields[len(fields)-2], ":"); len(splitted) > 1 {
vals = append(vals, splitted[len(splitted)-1], "used by: "+fields[0])
}
}
}
return carapace.ActionValuesDescribed(vals...).Style(style.Carapace.KeywordNegative)
})
}

if splitted := strings.Split(fields[len(fields)-2], ":"); len(splitted) > 1 {
vals = append(vals, splitted[len(splitted)-1], "used by: "+fields[0])
}
func actionUsedPortsNetstat() carapace.Action {
return carapace.ActionExecCommand("netstat", "-abon")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\r\n")
vals := make([]string, 0)
for index, line := range lines[:len(lines)-1] {
if fields := strings.Fields(line); len(fields) > 3 {
switch {
case fields[3] != "LISTENING":
continue
case fields[0] != "TCP" && fields[0] != "UDP":
continue
}

if splitted := strings.Split(fields[1], ":"); len(splitted) > 1 {
service := strings.TrimSpace(lines[index+1])
if strings.HasPrefix(service, "[") {
service = service[1 : len(service)-1]
}
vals = append(vals, splitted[len(splitted)-1], "used by: "+service)
}
return carapace.ActionValuesDescribed(vals...).Style(style.Carapace.KeywordNegative)
})
}
}
return carapace.ActionValues()
}).Tag("used ports")
return carapace.ActionValuesDescribed(vals...).Style(style.Carapace.KeywordNegative)
})
}