GCoordinator/scripts/cli.sh

95 lines
2.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# shellcheck source=scripts/_compose.sh
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)/_compose.sh"
usage() {
cat <<'EOF'
Usage: ./scripts/cli.sh [wrapper options] <gcoord subcommand & args...>
Run GCoordinator's CLI (`dist/cli/gcoord.js`) inside the `gcoordinator` container.
Uses `docker compose exec` if running, otherwise `docker compose run --rm --no-deps`.
Wrapper options (must appear before the gcoord subcommand):
--lan Use LAN compose configuration
--cloudflare Use Cloudflare compose configuration
--mode <lan|cloudflare> Explicitly select deployment mode
--dry-run Print the compose command without executing it
-h, --help Show this help message
Examples:
./scripts/cli.sh status --json
./scripts/cli.sh panic-lock
./scripts/cli.sh reset-2fa --disable
./scripts/cli.sh backup --out /data/manual-backup.gcoord.enc
EOF
}
REQ_MODE=""
while [[ $# -gt 0 ]]; do
case "$1" in
--lan)
REQ_MODE="lan"
shift
;;
--cloudflare)
REQ_MODE="cloudflare"
shift
;;
--mode)
[[ $# -ge 2 ]] || die "--mode requires an argument."
REQ_MODE="$2"
shift 2
;;
--mode=*)
REQ_MODE="${1#*=}"
shift
;;
--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
*)
break
;;
esac
done
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
MODE="$(resolve_mode "$REQ_MODE")"
detect_compose
build_compose_args "$MODE"
container_id=""
is_running="false"
if [[ "$DRY_RUN" != "1" ]] && command -v docker >/dev/null 2>&1; then
container_id="$("${COMPOSE_CMD[@]}" "${COMPOSE_ARGS[@]}" ps -q gcoordinator 2>/dev/null || true)"
if [[ -n "$container_id" ]]; then
is_running="$(docker inspect -f '{{.State.Running}}' "$container_id" 2>/dev/null || echo false)"
fi
fi
tty_args=()
if [[ ! -t 0 ]]; then
tty_args+=(-T)
fi
if [[ "$is_running" == "true" ]]; then
run_compose "$MODE" exec "${tty_args[@]}" gcoordinator node dist/cli/gcoord.js "$@"
else
run_compose "$MODE" run --rm --no-deps "${tty_args[@]}" gcoordinator node dist/cli/gcoord.js "$@"
fi