Initial commit
This commit is contained in:
commit
cdd0519c19
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build/
|
30
Makefile
Normal file
30
Makefile
Normal file
@ -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)
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# OS Helpers
|
||||
|
||||
Just some handy tools that I wanted to migrate from shell scripts to a higher-level language.
|
125
cmd/procs/procs.go
Normal file
125
cmd/procs/procs.go
Normal file
@ -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()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user