Skip to content

Commit f08ff3e

Browse files
authored
refactor: phase 1 shell dedup foundation (#151)
1 parent 94790e3 commit f08ff3e

16 files changed

Lines changed: 782 additions & 329 deletions

bin/baudbot

Lines changed: 110 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -327,74 +327,122 @@ else
327327
cmd_attach() { echo "❌ Missing CLI runtime helper. Run: sudo baudbot deploy"; exit 1; }
328328
fi
329329

330-
case "${1:-}" in
330+
require_systemd_runtime() {
331+
local message="$1"
332+
if ! has_systemd; then
333+
echo "$message"
334+
exit 1
335+
fi
336+
}
337+
338+
cmd_systemctl_start() {
339+
require_systemd_runtime "systemd not available. Use: baudbot start --direct"
340+
exec systemctl start baudbot "$@"
341+
}
342+
343+
cmd_systemctl_stop() {
344+
require_systemd_runtime "systemd not available. Kill the agent process manually."
345+
exec systemctl stop baudbot "$@"
346+
}
347+
348+
cmd_systemctl_restart() {
349+
require_systemd_runtime "systemd not available."
350+
exec systemctl restart baudbot "$@"
351+
}
352+
353+
declare -A CMD_KIND=()
354+
declare -A CMD_TARGET=()
355+
declare -A CMD_REQUIRE_ROOT=()
356+
declare -A CMD_REQUIRE_SYSTEMD=()
357+
declare -A CMD_RUNTIME_ERROR=()
358+
359+
register_command() {
360+
local name="$1"
361+
local kind="$2"
362+
local target="$3"
363+
local require_root_flag="$4"
364+
local require_systemd_flag="$5"
365+
local runtime_error="$6"
366+
367+
CMD_KIND["$name"]="$kind"
368+
CMD_TARGET["$name"]="$target"
369+
CMD_REQUIRE_ROOT["$name"]="$require_root_flag"
370+
CMD_REQUIRE_SYSTEMD["$name"]="$require_systemd_flag"
371+
CMD_RUNTIME_ERROR["$name"]="$runtime_error"
372+
}
373+
374+
dispatch_registered_command() {
375+
local command_name="$1"
376+
shift || true
377+
378+
local kind="${CMD_KIND[$command_name]:-}"
379+
[ -n "$kind" ] || return 1
380+
381+
local target="${CMD_TARGET[$command_name]}"
382+
local require_root_flag="${CMD_REQUIRE_ROOT[$command_name]}"
383+
local require_systemd_flag="${CMD_REQUIRE_SYSTEMD[$command_name]}"
384+
local runtime_error="${CMD_RUNTIME_ERROR[$command_name]}"
385+
386+
if [ "$require_root_flag" = "1" ]; then
387+
require_root "$command_name"
388+
fi
389+
390+
if [ "$require_systemd_flag" = "1" ]; then
391+
require_systemd_runtime "$runtime_error"
392+
fi
393+
394+
case "$kind" in
395+
exec)
396+
exec "$target" "$@"
397+
;;
398+
function)
399+
"$target" "$@"
400+
;;
401+
*)
402+
echo "❌ invalid command registration: $command_name"
403+
exit 1
404+
;;
405+
esac
406+
}
407+
408+
register_command "start" "function" "cmd_systemctl_start" "1" "0" ""
409+
register_command "stop" "function" "cmd_systemctl_stop" "1" "0" ""
410+
register_command "restart" "function" "cmd_systemctl_restart" "1" "0" ""
411+
register_command "status" "function" "cmd_status" "0" "0" ""
412+
register_command "logs" "function" "cmd_logs" "0" "0" ""
413+
register_command "sessions" "function" "cmd_sessions" "0" "0" ""
414+
register_command "attach" "function" "cmd_attach" "0" "0" ""
415+
register_command "config" "exec" "$BAUDBOT_ROOT/bin/config.sh" "0" "0" ""
416+
register_command "env" "exec" "$BAUDBOT_ROOT/bin/env.sh" "0" "0" ""
417+
register_command "deploy" "exec" "$BAUDBOT_ROOT/bin/deploy.sh" "1" "0" ""
418+
register_command "audit" "exec" "$BAUDBOT_ROOT/bin/security-audit.sh" "0" "0" ""
419+
register_command "test" "exec" "$BAUDBOT_ROOT/bin/test.sh" "0" "0" ""
420+
register_command "update" "exec" "$BAUDBOT_ROOT/bin/update-release.sh" "1" "0" ""
421+
register_command "rollback" "exec" "$BAUDBOT_ROOT/bin/rollback-release.sh" "1" "0" ""
422+
register_command "uninstall" "exec" "$BAUDBOT_ROOT/bin/uninstall.sh" "1" "0" ""
423+
register_command "doctor" "exec" "$BAUDBOT_ROOT/bin/doctor.sh" "0" "0" ""
424+
425+
COMMAND_NAME="${1:-}"
426+
if [ -n "$COMMAND_NAME" ]; then
427+
shift
428+
fi
429+
430+
case "$COMMAND_NAME" in
331431
install)
332-
shift
333432
bootstrap_install "$@"
334433
;;
335434

336435
start)
337-
shift
338436
if [ "${1:-}" = "--direct" ]; then
339437
# Foreground mode: run start.sh directly (for dev/CI/debugging)
340438
shift
341439
require_root "start --direct"
342440
exec sudo -u baudbot_agent "$BAUDBOT_ROOT/start.sh" "$@"
343-
else
344-
require_root "start"
345-
if has_systemd; then
346-
exec systemctl start baudbot "$@"
347-
else
348-
echo "systemd not available. Use: baudbot start --direct"
349-
exit 1
350-
fi
351-
fi
352-
;;
353-
354-
stop)
355-
shift
356-
require_root "stop"
357-
if has_systemd; then
358-
exec systemctl stop baudbot "$@"
359-
else
360-
echo "systemd not available. Kill the agent process manually."
361-
exit 1
362-
fi
363-
;;
364-
365-
restart)
366-
shift
367-
require_root "restart"
368-
if has_systemd; then
369-
exec systemctl restart baudbot "$@"
370-
else
371-
echo "systemd not available."
372-
exit 1
373441
fi
374-
;;
375-
376-
status)
377-
shift
378-
cmd_status "$@"
379-
;;
380-
381-
logs)
382-
shift
383-
cmd_logs "$@"
384-
;;
385-
386-
sessions)
387-
shift
388-
cmd_sessions "$@"
389-
;;
390-
391-
attach)
392-
shift
393-
cmd_attach "$@"
442+
dispatch_registered_command "start" "$@"
394443
;;
395444

396445
setup)
397-
shift
398446
if [ "${1:-}" = "--slack-broker" ]; then
399447
shift
400448
require_root "broker register"
@@ -411,18 +459,7 @@ case "${1:-}" in
411459
exec "$BAUDBOT_ROOT/setup.sh" "$@"
412460
;;
413461

414-
config)
415-
shift
416-
exec "$BAUDBOT_ROOT/bin/config.sh" "$@"
417-
;;
418-
419-
env)
420-
shift
421-
exec "$BAUDBOT_ROOT/bin/env.sh" "$@"
422-
;;
423-
424462
broker)
425-
shift
426463
case "${1:-}" in
427464
register)
428465
shift
@@ -446,51 +483,7 @@ case "${1:-}" in
446483
esac
447484
;;
448485

449-
deploy)
450-
shift
451-
require_root "deploy"
452-
exec "$BAUDBOT_ROOT/bin/deploy.sh" "$@"
453-
;;
454-
455-
audit)
456-
shift
457-
exec "$BAUDBOT_ROOT/bin/security-audit.sh" "$@"
458-
;;
459-
460-
test)
461-
shift
462-
exec "$BAUDBOT_ROOT/bin/test.sh" "$@"
463-
;;
464-
465-
update)
466-
shift
467-
require_root "update"
468-
exec "$BAUDBOT_ROOT/bin/update-release.sh" "$@"
469-
;;
470-
471-
rollback)
472-
shift
473-
require_root "rollback"
474-
exec "$BAUDBOT_ROOT/bin/rollback-release.sh" "$@"
475-
;;
476-
477-
uninstall)
478-
shift
479-
require_root "uninstall"
480-
exec "$BAUDBOT_ROOT/bin/uninstall.sh" "$@"
481-
;;
482-
483-
doctor)
484-
shift
485-
exec "$BAUDBOT_ROOT/bin/doctor.sh" "$@"
486-
;;
487-
488-
version)
489-
shift
490-
echo "baudbot $(version_display)"
491-
;;
492-
493-
--version|-v)
486+
version|--version|-v)
494487
echo "baudbot $(version_display)"
495488
;;
496489

@@ -499,9 +492,12 @@ case "${1:-}" in
499492
;;
500493

501494
*)
502-
echo "Unknown command: $1"
503-
echo ""
504-
usage
505-
exit 1
495+
if [ -z "${CMD_KIND[$COMMAND_NAME]:-}" ]; then
496+
echo "Unknown command: $COMMAND_NAME"
497+
echo ""
498+
usage
499+
exit 1
500+
fi
501+
dispatch_registered_command "$COMMAND_NAME" "$@"
506502
;;
507503
esac

0 commit comments

Comments
 (0)