zscripts-token-savers/bash/zkill
kellymichels 4ebde3ebcf
fix(bash): make the port bash-3.2 / BSD-clean so it runs on stock macOS (#8)
The README advertises macOS support, but the port used constructs that
fail on the userland macOS actually ships:

- `mapfile` (bash 4+) in 10 spots — macOS ships bash 3.2 as /usr/bin/bash,
  so a mac user following the README hit "mapfile: command not found" and
  silently got empty project lists. Replace each with a portable
  `while IFS= read -r` loop (identical arrays; set -u safe on empty input).
- `_z_commafy` used the GNU-only `sed :a;...;ta` label/branch idiom, which
  errors on BSD/macOS sed (the token footer's thousands separators).
  Reimplement with awk (already a dependency).
- README: correct the macOS line — bash 4+ does NOT ship with macOS; the
  stock 3.2 now works, and only jq needs brew.

Also two correctness nits found in the same review:
- zkill/zrestart `--kill-all` was parsed but silently ignored; now it
  prints a "not implemented in the bash port" notice instead of no-op.
- zrepair's smoke-test line said "https://$domain" but probes
  http://$HOST with a Host header; label now matches what it does.

Verified on WSL (bash 5): read-loops produce the same 11 project / 7
domain keys as mapfile; awk commafy matches across 0..1,234,567; empty
producer yields a 0-length array; footer renders. 3.2-compat is by static
analysis — no bash-4-only constructs remain.
2026-07-22 18:41:11 -05:00

65 lines
2.0 KiB
Bash

#!/usr/bin/env bash
# Evomedia.net Token Savers — https://github.com/kellymichels/zscripts-token-savers
# Created by Kelly Michels · dev@evomedia.net
# Licensed under the MIT License. See LICENSE.
#
# zkill — stop local dev-server listeners for a project (bash port of ZKillOnly.ps1).
#
# Usage:
# zkill <project> [<project> ...] [--port N] [--kill-all]
set -uo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/zhelpers.sh"
z_track_start "$@"
z_need_config
z_require lsof
projects=(); port_override=0; kill_all=0
while [ $# -gt 0 ]; do
case "$1" in
-p|--port) port_override="${2:-0}"; shift 2 ;;
--kill-all) kill_all=1; shift ;;
--) shift; break ;;
-*) err "Unknown option: $1"; exit 1 ;;
*) projects+=("$1"); shift ;;
esac
done
if [ "${#projects[@]}" -eq 0 ]; then
warn "Usage: zkill <project> [<project> ...] [--port N] [--kill-all]"
dim " Projects in zconfig.json: $(zproj_csv)"
exit 1
fi
# --kill-all (kill stray project processes beyond the dev-port listener) isn't
# ported yet; announce it rather than silently accepting the flag and doing nothing.
[ "$kill_all" -eq 1 ] && warn "Note: --kill-all isn't implemented in the bash port yet - killing dev-port listeners only."
for key in "${projects[@]}"; do
zproj_require "$key"
label="$(zproj "$key" .label)"; [ -n "$label" ] || label="$key"
if [ "$port_override" -gt 0 ] 2>/dev/null; then
port="$port_override"
else
port="$(zproj "$key" .ports.dev)"
fi
printf '\n'; info "=== zkill ($label) ==="
if [[ "$port" =~ ^[0-9]+$ ]] && [ "$port" -gt 0 ]; then
dim "Port: $port"
killed="$(kill_listeners "$port")"
else
warn "No dev port configured for '$key' - skipping port kill."
killed=0
fi
# TODO (parity with ZKillOnly.ps1 -KillAll): also stop stray project processes.
if [ "${killed:-0}" -gt 0 ]; then
note " $killed process(es) stopped"
else
ok " Nothing to kill"
fi
done
printf '\n'; info "Kill complete."