|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# This script provides a fallback when rlwrap is not installed, allowing Leiningen 1.x and clj REPLs to function |
| 6 | +# without requiring the buildpack to install rlwrap as a dependency. Using a REPL on Heroku is rare and typically |
| 7 | +# only needed for one-off debugging. Installing rlwrap for all users incurs a cost in build time and slug size |
| 8 | +# for very little benefit to the majority of apps. Users who frequently use Leiningen 1.x or clj REPLs can opt-in |
| 9 | +# to full readline support by installing rlwrap via the APT buildpack. |
| 10 | + |
| 11 | +# Find the first rlwrap binary on PATH that is not this script. Used to make sure that users who have installed |
| 12 | +# rlwrap use the real binary instead of this script. |
| 13 | +RLWRAP_BIN=$(command -v -a rlwrap 2>/dev/null | grep -v "^${BASH_SOURCE[0]}$" | head -1 || true) |
| 14 | + |
| 15 | +if [[ -n "${RLWRAP_BIN}" ]]; then |
| 16 | + exec "${RLWRAP_BIN}" "${@}" |
| 17 | +else |
| 18 | + cat >&2 <<-EOF |
| 19 | + Warning: Using minimal rlwrap replacement. Command history and line editing are not available. |
| 20 | + To enable full readline support, add the APT buildpack and create an Aptfile: |
| 21 | +
|
| 22 | + 1. heroku buildpacks:add --index 1 heroku-community/apt |
| 23 | + 2. echo "rlwrap" > Aptfile |
| 24 | + 3. git add Aptfile && git commit -m "Add rlwrap support" |
| 25 | + 4. git push heroku main |
| 26 | +
|
| 27 | + EOF |
| 28 | + |
| 29 | + # Strip rlwrap options, leaving only the wrapped command. |
| 30 | + while [[ "${1:-}" == -* ]]; do |
| 31 | + # Options that require an argument need an additional shift. |
| 32 | + case "${1:-}" in |
| 33 | + -a | --always-readline | -b | --break-chars | -C | --command-name | \ |
| 34 | + -D | --history-no-dupes | -f | --file | -g | --forget-matching | \ |
| 35 | + -H | --history-filename | -l | --logfile | -O | --only-cook | \ |
| 36 | + -P | --pre-given | -q | --quote-characters | -s | --histsize | \ |
| 37 | + -S | --substitute-prompt | -t | --set-term-name | \ |
| 38 | + -w | --wait-before-prompt | -z | --filter) |
| 39 | + shift |
| 40 | + ;; |
| 41 | + -m | --multi-line | -p | --prompt-colour) |
| 42 | + # Optional argument: only shift if not another option. |
| 43 | + if [[ "${1:-}" != -* ]]; then |
| 44 | + shift |
| 45 | + fi |
| 46 | + ;; |
| 47 | + esac |
| 48 | + |
| 49 | + shift |
| 50 | + done |
| 51 | + |
| 52 | + exec "${@}" |
| 53 | +fi |
0 commit comments