From dde17f23394b99a089c900ae3e2956947daad6bd Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Fri, 28 Aug 2026 09:31:39 +0200 Subject: [PATCH] feat(workspace): add interactive build choices --- build-sbx.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/build-sbx.sh b/build-sbx.sh index 5a1a9e9..fffcc5d 100755 --- a/build-sbx.sh +++ b/build-sbx.sh @@ -19,9 +19,74 @@ require_command() { fi } +ask_yes_no() { + local prompt="$1" + local answer + + while true; do + read -r -p "$prompt [y/n] " answer || exit 0 + case "$answer" in + [Yy]) return 0 ;; + [Nn]) return 1 ;; + *) printf 'Please answer yes or no.\n' ;; + esac + done +} + +prompt_component() { + local result_var="$1" + local label="$2" + shift 2 + local options=("$@" custom) + local choice custom_value + + while true; do + printf '%s:\n' "$label" + select choice in "${options[@]}"; do + case "$choice" in + custom) + read -r -p "Custom $label: " custom_value || exit 0 + if [[ -n "$custom_value" ]]; then + printf -v "$result_var" '%s' "$custom_value" + return 0 + fi + printf 'The custom value cannot be empty.\n' + ;; + '') printf 'Please select one of the listed options.\n' ;; + *) + printf -v "$result_var" '%s' "$choice" + return 0 + ;; + esac + done + done +} + main() { require_command sbx 'We cannot build a Docker sbx image because sbx does not seem to be installed.' require_command jq 'We cannot list existing Docker sbx sandboxes because jq does not seem to be installed.' + + local build_from_scratch + if ask_yes_no 'Build from scratch?'; then + build_from_scratch=true + else + build_from_scratch=false + fi + + local sandbox_json + if ! sandbox_json=$(sbx ls --json); then + printf 'Unable to retrieve the existing Docker sbx sandboxes.\n' >&2 + exit 1 + fi + + if ! jq -s -e 'length == 1 and (.[0].sandboxes | type == "array")' \ + <<< "$sandbox_json" >/dev/null; then + printf 'The output from sbx ls --json was not the expected JSON format.\n' >&2 + exit 1 + fi + + local -a sandbox_names + mapfile -t sandbox_names < <(jq -r '.sandboxes[].name' <<< "$sandbox_json") } main "$@"