Files
docker-sbx-build/build-sbx.sh

93 lines
2.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Build and optionally configure a Docker sandbox interactively.
set -o errexit
set -o nounset
set -o pipefail
readonly KIT_PATH="$HOME/.sbx/kits/hypoport.ai"
readonly TEMPLATE='127.0.0.1:62735/finmas.de/mux033/opencode-ops:latest'
require_command() {
local command_name="$1"
local message="$2"
if ! command -v "$command_name" >/dev/null 2>&1; then
printf '%s\n' "$message" >&2
exit 1
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 "$@"