feat(workspace): add interactive build choices
This commit is contained in:
65
build-sbx.sh
65
build-sbx.sh
@@ -19,9 +19,74 @@ require_command() {
|
|||||||
fi
|
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() {
|
main() {
|
||||||
require_command sbx 'We cannot build a Docker sbx image because sbx does not seem to be installed.'
|
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.'
|
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 "$@"
|
main "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user