refactor: make command exit when not procs found via port search and not return the header from ss
This commit is contained in:
parent
e5a858c56d
commit
1e3545e090
@ -19,10 +19,10 @@ func commandExists(cmd string) (string, bool) {
|
||||
return path, true
|
||||
}
|
||||
|
||||
func ss(p int) {
|
||||
func ss(p *int) {
|
||||
cmdStr, ok := commandExists("ss")
|
||||
if !ok {
|
||||
fmt.Printf("ss command not found")
|
||||
fmt.Printf(`command "ss" not found`)
|
||||
}
|
||||
|
||||
output, err := exec.Command(cmdStr, "-tulpn").Output()
|
||||
@ -30,6 +30,10 @@ func ss(p int) {
|
||||
fmt.Printf("failed to execute ss command: %v", err)
|
||||
return
|
||||
}
|
||||
if p == nil {
|
||||
fmt.Println(string(output))
|
||||
return
|
||||
}
|
||||
|
||||
reader := strings.NewReader(string(output))
|
||||
scanner := bufio.NewScanner(bufio.NewReader(reader))
|
||||
@ -41,13 +45,17 @@ func ss(p int) {
|
||||
matches = append(matches, scanner.Text())
|
||||
}
|
||||
|
||||
if strings.Contains(scanner.Text(), ":"+strconv.Itoa(p)) {
|
||||
if strings.Contains(scanner.Text(), ":"+strconv.Itoa(*p)) {
|
||||
matches = append(matches, scanner.Text())
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
if len(matches) == 1 {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(strings.Join(matches, "\n"))
|
||||
}
|
||||
|
||||
@ -61,7 +69,7 @@ func dockerPs(compose bool) {
|
||||
|
||||
cmd, ok := commandExists(s)
|
||||
if !ok {
|
||||
fmt.Printf("%s not found", s)
|
||||
fmt.Printf(`command "%s" not found`, s)
|
||||
}
|
||||
|
||||
output, err := exec.Command(cmd, "ps").Output()
|
||||
@ -79,18 +87,19 @@ const (
|
||||
|
||||
func main() {
|
||||
port := func(args []string) {
|
||||
if len(args) == 0 {
|
||||
ss(nil)
|
||||
return
|
||||
}
|
||||
|
||||
fs := flag.NewFlagSet("port", flag.ContinueOnError)
|
||||
port := fs.Int("p", 0, "non-zero port to search for")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if *port == 0 {
|
||||
fmt.Println("Cannot search port 0")
|
||||
return
|
||||
}
|
||||
|
||||
ss(*port)
|
||||
ss(port)
|
||||
}
|
||||
|
||||
docker := func(args []string) {
|
||||
@ -115,11 +124,3 @@ func main() {
|
||||
fmt.Println("Unknown command type. Supported types are 'port'.")
|
||||
}
|
||||
}
|
||||
|
||||
var port int
|
||||
|
||||
func init() {
|
||||
flag.IntVar(&port, "port", 0, "search procs running on this port")
|
||||
|
||||
flag.Parse()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user