feat(scripts): add bot_cli.sh wrapper, fix dev.sh help routing

This commit is contained in:
yiekheng 2026-05-02 17:02:49 +08:00
parent 48e5adbccd
commit 23c697d6fe
2 changed files with 74 additions and 8 deletions

62
scripts/bot_cli.sh Executable file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env bash
# Run the bot CLI in the local venv. With no args, drops into the TUI menu.
# Requires: dev stack up (run scripts/dev.sh up first), .venv with deps.
set -euo pipefail
usage() {
cat <<'EOF'
Run the bot CLI (app.bot_cli) in the local venv.
Usage:
scripts/bot_cli.sh Drop into the TUI menu.
scripts/bot_cli.sh <subcommand> [args] One-shot subcommand. Try --help.
Examples:
scripts/bot_cli.sh register
scripts/bot_cli.sh credit 13c1234 abc12345
scripts/bot_cli.sh monitor-once --target 5
Environment:
NO_SUDO=1 Skip 'sudo' when checking 'dev.sh status'.
PYTHON_BIN Override the python interpreter (default: .venv/bin/python).
EOF
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT_DIR}"
# E2: bail if the dev stack is not running.
if ! NO_SUDO="${NO_SUDO:-0}" bash scripts/dev.sh status >/dev/null 2>&1; then
echo "ERROR: dev stack not running. Run 'scripts/dev.sh up' first." >&2
exit 2
fi
if [[ ! -f .env ]]; then
echo "ERROR: .env not found. cp envs/dev/.env.example .env (then edit)." >&2
exit 2
fi
# Load .env into the environment (export everything between 'set -a' and 'set +a').
set -a
# shellcheck disable=SC1091
source .env
set +a
# Override DB host/port for the local CLI: docker mysql is published on
# 127.0.0.1:3306, even though api-server in-network reaches it as mysql:3306.
export DB_HOST=127.0.0.1
export DB_PORT=3306
PYTHON_BIN="${PYTHON_BIN:-${ROOT_DIR}/.venv/bin/python}"
if [[ ! -x "${PYTHON_BIN}" ]]; then
echo "ERROR: ${PYTHON_BIN} not found." >&2
echo "Create the venv: python3 -m venv .venv && .venv/bin/pip install -r requirements.txt" >&2
exit 2
fi
exec "${PYTHON_BIN}" -m app.bot_cli "$@"

View File

@ -28,6 +28,18 @@ SUDO="sudo"
# shellcheck disable=SC2206 # shellcheck disable=SC2206
COMPOSE=(${SUDO} docker compose -f docker-compose.yml -f docker-compose.override.yml) COMPOSE=(${SUDO} docker compose -f docker-compose.yml -f docker-compose.override.yml)
# Help and empty-arg cases don't need .env. Handle them first.
case "${1:-}" in
-h|--help|help)
usage
exit 0
;;
"")
usage >&2
exit 1
;;
esac
if [[ ! -f .env ]]; then if [[ ! -f .env ]]; then
echo "ERROR: .env not found at repo root. Run: cp envs/dev/.env.example .env (then edit)." >&2 echo "ERROR: .env not found at repo root. Run: cp envs/dev/.env.example .env (then edit)." >&2
exit 2 exit 2
@ -56,14 +68,6 @@ case "${1:-}" in
exit 1 exit 1
fi fi
;; ;;
-h|--help|help)
usage
exit 0
;;
"")
usage >&2
exit 1
;;
*) *)
echo "unknown command: $1" >&2 echo "unknown command: $1" >&2
usage >&2 usage >&2