From cdd0519c1967949ab5cc6bf6f869cc23fd1fcc48 Mon Sep 17 00:00:00 2001 From: cerbervs Date: Wed, 3 Sep 2025 11:54:24 -0400 Subject: [PATCH] Initial commit --- .gitignore | 1 + Makefile | 30 +++++++++++ README.md | 3 ++ cmd/procs/procs.go | 125 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ 5 files changed, 162 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/procs/procs.go create mode 100644 go.mod diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c12306b --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +ROOT := $(shell pwd) + +BIN_DIR := $(ROOT)/build/cmd + +CMDS := $(notdir $(wildcard cmd/*)) + +BINS := $(addprefix $(BIN_DIR)/,$(CMDS)) + +INSTALL_DIR := $(HOME)/.local/bin + +.PHONY: all clean install + +# Default target: build all binaries +all: install + +# Rule to build each binary +$(BIN_DIR)/%: cmd/%/* + @mkdir -p $(BIN_DIR) + go build -o $@ ./cmd/$* + +install: $(BINS) + @mkdir -p $(INSTALL_DIR) + @for bin in $(BINS); do \ + cp $$bin $(INSTALL_DIR)/; \ + echo "Installed $$bin to $(INSTALL_DIR)"; \ + done + +# Clean build artifacts +clean: + rm -rf $(BIN_DIR) diff --git a/README.md b/README.md new file mode 100644 index 0000000..8562f8a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# OS Helpers + +Just some handy tools that I wanted to migrate from shell scripts to a higher-level language. diff --git a/cmd/procs/procs.go b/cmd/procs/procs.go new file mode 100644 index 0000000..3517f24 --- /dev/null +++ b/cmd/procs/procs.go @@ -0,0 +1,125 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "os" + "os/exec" + "strconv" + "strings" +) + +func commandExists(cmd string) (string, bool) { + path, err := exec.LookPath(cmd) + if err != nil { + return "", false + } + + return path, true +} + +func ss(p int) { + cmdStr, ok := commandExists("ss") + if !ok { + fmt.Printf("ss command not found") + } + + output, err := exec.Command(cmdStr, "-tulpn").Output() + if err != nil { + fmt.Printf("failed to execute ss command: %v", err) + return + } + + reader := strings.NewReader(string(output)) + scanner := bufio.NewScanner(bufio.NewReader(reader)) + matches := []string{} + + i := 0 + for scanner.Scan() { + if i == 0 { + matches = append(matches, scanner.Text()) + } + + if strings.Contains(scanner.Text(), ":"+strconv.Itoa(p)) { + matches = append(matches, scanner.Text()) + } + + i++ + } + + fmt.Println(strings.Join(matches, "\n")) +} + +func dockerPs(compose bool) { + var s string + if !compose { + s = "docker" + } else { + s = "docker-compose" + } + + cmd, ok := commandExists(s) + if !ok { + fmt.Printf("%s not found", s) + } + + output, err := exec.Command(cmd, "ps").Output() + if err != nil { + fmt.Printf("failed to execute docker command: %v", err) + } + + fmt.Println(string(output)) +} + +const ( + portType string = "port" + dockerType string = "docker" +) + +func main() { + port := func(args []string) { + 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) + } + + docker := func(args []string) { + fs := flag.NewFlagSet("docker", flag.ContinueOnError) + compose := fs.Bool("c", false, "use docker compose instead") + if err := fs.Parse(args); err != nil { + fmt.Println(err) + return + } + + dockerPs(*compose) + } + + args := os.Args + subArgs := args[2:] + switch args[1] { + case portType: + port(subArgs) + case dockerType: + docker(subArgs) + default: + 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() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b8dc14d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.markbailey.dev/os-helpers + +go 1.24.6