diff --git a/scripts/bot_cli.sh b/scripts/bot_cli.sh new file mode 100755 index 0000000..6528f79 --- /dev/null +++ b/scripts/bot_cli.sh @@ -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 [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 "$@" diff --git a/scripts/dev.sh b/scripts/dev.sh index 41910ac..88cab5f 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -28,6 +28,18 @@ SUDO="sudo" # shellcheck disable=SC2206 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 echo "ERROR: .env not found at repo root. Run: cp envs/dev/.env.example .env (then edit)." >&2 exit 2 @@ -56,14 +68,6 @@ case "${1:-}" in exit 1 fi ;; - -h|--help|help) - usage - exit 0 - ;; - "") - usage >&2 - exit 1 - ;; *) echo "unknown command: $1" >&2 usage >&2