From a711c424837cca536a617cb32f33d82b92a6c370 Mon Sep 17 00:00:00 2001 From: Tianyong Qiu Date: Mon, 10 Feb 2025 15:53:06 +0800 Subject: [PATCH] Update for change script to support bash 3 --- devbox/devbox.local/devbox | 491 ++++++++++++++++++++++++------------- 1 file changed, 315 insertions(+), 176 deletions(-) diff --git a/devbox/devbox.local/devbox b/devbox/devbox.local/devbox index 35b42b9..8e83d7b 100644 --- a/devbox/devbox.local/devbox +++ b/devbox/devbox.local/devbox @@ -3,8 +3,8 @@ # Modifying it manually is not recommended # :wrapper.bash3_bouncer -if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then - printf "bash version 4 or higher is required\n" >&2 +if [[ "${BASH_VERSINFO:-0}" -lt 3 ]]; then + printf "bash version 3 or higher is required\n" >&2 exit 1 fi @@ -15,6 +15,40 @@ version_command() { echo "$version" } +upper() { + echo "$1" | tr '[:lower:]' '[:upper:]' +} + +lower() { + echo "$1" | tr '[:upper:]' '[:lower:]' +} + + +# Add a key-value pair to the args array +add_arg() { + local key="$1" + local value="$2" + args_keys+=("$key") + args_values+=("$value") +} + +# Get the value of a key from the args array + +get_arg() { + local key="$1" + local default="${2:-}" + local i + for i in "${!args_keys[@]}"; do + if [ "${args_keys[$i]}" = "$key" ]; then + echo "${args_values[$i]}" + return 0 + fi + done + echo "$default" + return 1 +} + + # :command.usage devbox_usage() { printf "devbox - DevBox Command Line Tool\n\n" @@ -492,35 +526,56 @@ normalize_input() { # :command.inspect_args inspect_args() { - if ((${#args[@]})); then - readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) - echo args: - for k in "${sorted_keys[@]}"; do - echo "- \${args[$k]} = ${args[$k]}" + # Check and output the simulated args associative array (using args_keys and args_values) + if [ ${#args_keys[@]} -gt 0 ]; then + # 利用 printf 和 sort 对键进行排序 + sorted_keys=$(printf "%s\n" "${args_keys[@]}" | sort) + echo "args:" + for key in $sorted_keys; do + value="" + # Find the value based on the key + for i in `seq 0 $((${#args_keys[@]} - 1))`; do + if [ "${args_keys[$i]}" = "$key" ]; then + value="${args_values[$i]}" + break + fi + done + done else - echo args: none + echo "args: none" fi - if ((${#deps[@]})); then - readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort) + # Check and output the simulated deps associative array (using deps_keys and deps_values) + if [ ${#deps_keys[@]} -gt 0 ]; then + sorted_keys=$(printf "%s\n" "${deps_keys[@]}" | sort) echo - echo deps: - for k in "${sorted_keys[@]}"; do - echo "- \${deps[$k]} = ${deps[$k]}" + echo "deps:" + for key in $sorted_keys; do + value="" + for i in `seq 0 $((${#deps_keys[@]} - 1))`; do + if [ "${deps_keys[$i]}" = "$key" ]; then + value="${deps_values[$i]}" + break + fi + done + echo "- \$deps[$key] = $value" done fi - if ((${#env_var_names[@]})); then - readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort) + # Check and output the simulated env_vars associative array (using env_var_names) + if [ ${#env_var_names[@]} -gt 0 ]; then + sorted_names=$(printf "%s\n" "${env_var_names[@]}" | sort) echo echo "environment variables:" - for k in "${sorted_names[@]}"; do - echo "- \$$k = ${!k:-}" + for name in $sorted_names; do + # Look up the value based on the name + echo "- \$${name} = ${!name:-}" done fi } + install_docker() { echo "[INFO] Checking Docker installation..." @@ -730,6 +785,23 @@ start_local_rabbitMQ(){ echo "[INFO] Completed RabbitMQ container..." } +# 定义键和值数组 +local_components_ports_keys=("devsvc" "payment" "content" "central_storage" "authentication") +local_components_ports_values=("8007" "8006" "8013" "8005" "8004") + +# 根据组件名查找对应的端口 +get_port() { + local comp="$1" + local port="" + for i in "${!local_components_ports_keys[@]}"; do + if [ "${local_components_ports_keys[i]}" = "$comp" ]; then + port="${local_components_ports_values[i]}" + break + fi + done + echo "$port" +} + # :command.command_functions # :command.function devbox_init_command() { @@ -779,45 +851,46 @@ devbox_init_command() { # --force flag to overwrite existing resources local FORCE_INIT="${args_force}" - local OS="${args['--os']}" - local ARCH="${args['--arch']}" - local DEVBOX_NAME="${args['--devbox-container-name']}" - local DEVBOX_PORT="${args['--devbox-container-port']}" - local DEVBOX_FRONTEND_PORT="${args['--devbox-frontend-port']}" - local DEVBOX_BACKEND_PORT="${args['--devbox-backend-port']}" - local DEVBOX_REPO="${args['--devbox-image-repo']}" - local DEVBOX_IMAGE="${args['--devbox-image-name']}" - local DEVBOX_TAG="${args['--devbox-image-tag']}" - local WORKING_HOME="${args['--working-home']:-${WORKING_HOME:-${HOME}/.devbox}}" - local FREELEAPS_USERNAME="${args['--freeleaps-username']}" - local FREELEAPS_PASSWORD="${args['--freeleaps-password']}" - local USE_LOCAL_COMPONENT="${args['--use-local-component']}" - local DEVSVC_REPO="${args['--devsvc-image-repo']}" - local DEVSVC_IMAGE="${args['--devsvc-image-name']}" - local DEVSVC_TAG="${args['--devsvc-image-tag']}" - local PAYMENT_REPO="${args['--payment-image-repo']}" - local PAYMENT_IMAGE="${args['--payment-image-name']}" - local PAYMENT_TAG="${args['--payment-image-tag']}" - local CONTENT_REPO="${args['--content-image-repo']}" - local CONTENT_IMAGE="${args['--content-image-name']}" - local CONTENT_TAG="${args['--content-image-tag']}" - local CENTRAL_STORAGE_REPO="${args['--central_storage-image-repo']}" - local CENTRAL_STORAGE_IMAGE="${args['--central_storage-image-name']}" - local CENTRAL_STORAGE_TAG="${args['--central_storage-image-tag']}" - local AUTHENTICATION_REPO="${args['--authentication-image-repo']}" - local AUTHENTICATION_IMAGE="${args['--authentication-image-name']}" - local AUTHENTICATION_TAG="${args['--authentication-image-tag']}" - local FORCE_INIT="${args['--force']}" + local OS="$(get_arg '--os')" + local ARCH="$(get_arg '--arch')" + local DEVBOX_NAME="$(get_arg '--devbox-container-name')" + local DEVBOX_PORT="$(get_arg '--devbox-container-port')" + local DEVBOX_FRONTEND_PORT="$(get_arg '--devbox-frontend-port')" + local DEVBOX_BACKEND_PORT="$(get_arg '--devbox-backend-port')" + local DEVBOX_REPO="$(get_arg '--devbox-image-repo')" + local DEVBOX_IMAGE="$(get_arg '--devbox-image-name')" + local DEVBOX_TAG="$(get_arg '--devbox-image-tag')" + local WORKING_HOME="$(get_arg '--working-home' "${WORKING_HOME:-${HOME}/.devbox}")" + local FREELEAPS_USERNAME="$(get_arg '--freeleaps-username')" + local FREELEAPS_PASSWORD="$(get_arg '--freeleaps-password')" + local USE_LOCAL_COMPONENT="$(get_arg '--use-local-component')" + local DEVSVC_REPO="$(get_arg '--devsvc-image-repo')" + local DEVSVC_IMAGE="$(get_arg '--devsvc-image-name')" + local DEVSVC_TAG="$(get_arg '--devsvc-image-tag')" + local PAYMENT_REPO="$(get_arg '--payment-image-repo')" + local PAYMENT_IMAGE="$(get_arg '--payment-image-name')" + local PAYMENT_TAG="$(get_arg '--payment-image-tag')" + local CONTENT_REPO="$(get_arg '--content-image-repo')" + local CONTENT_IMAGE="$(get_arg '--content-image-name')" + local CONTENT_TAG="$(get_arg '--content-image-tag')" + local CENTRAL_STORAGE_REPO="$(get_arg '--central_storage-image-repo')" + local CENTRAL_STORAGE_IMAGE="$(get_arg '--central_storage-image-name')" + local CENTRAL_STORAGE_TAG="$(get_arg '--central_storage-image-tag')" + local AUTHENTICATION_REPO="$(get_arg '--authentication-image-repo')" + local AUTHENTICATION_IMAGE="$(get_arg '--authentication-image-name')" + local AUTHENTICATION_TAG="$(get_arg '--authentication-image-tag')" + local FORCE_INIT="$(get_arg '--force')" local is_pull_all_components=true local components=("devsvc" "payment" "content" "central_storage" "authentication") echo "==> Checking parameters..." for component in "${components[@]}"; do - echo "==> Checking ${component} image repo...value: ${args["${component}_image_repo"]}" + echo "==> Checking ${component} image repo...value: $(get_arg "--${component}-image-repo")" # if any component image repo is provided, then don't pull all components - if [[ -n "${args["${component}_image_repo"]}" ]]; then + + if [[ -n "$(get_arg "--${component}-image-repo")" ]]; then is_pull_all_components=false break fi @@ -987,9 +1060,10 @@ devbox_init_command() { exit 1 fi - # record container id + # record container id, DEVBOX_FRONTEND_PORT, DEVBOX_BACKEND_PORT echo "$container_id" > "$WORKING_HOME/.devbox-instance" - + echo "$DEVBOX_FRONTEND_PORT" > "$WORKING_HOME/.devbox-frontend-port" + echo "$DEVBOX_BACKEND_PORT" > "$WORKING_HOME/.devbox-backend-port" # ------------------------------------------------------------------- # 6. linbwang: pull and start other components @@ -997,29 +1071,21 @@ devbox_init_command() { echo "==> [INIT] Starting Freeleaps services... $USE_LOCAL_COMPONENT" -if [[ "${USE_LOCAL_COMPONENT,,}" == "true" ]]; then +if [[ "$(lower "$USE_LOCAL_COMPONENT")" == "true" ]]; then echo ' ===> Using local components for Freeleaps services.' - # Define local components ports dictionary - declare -A local_components_ports - local_components_ports["devsvc"]="8007" - local_components_ports["payment"]="8006" - local_components_ports["content"]="8013" - local_components_ports["central_storage"]="8005" - local_components_ports["authentication"]="8004" - # Local components for Freeleaps services (devsvc, payment, content, central_storage, authentication) for component in "${components[@]}"; do - repo_var="${component^^}_REPO" - image_var="${component^^}_IMAGE" - tag_var="${component^^}_TAG" + repo_var="$(upper "$component")_REPO" + image_var="$(upper "$component")_IMAGE" + tag_var="$(upper "$component")_TAG" - # 使用间接展开获取变量的值 + + # Get the component's repo, image, and tag COMPONENT_REPO="${!repo_var}" COMPONENT_IMAGE="${!image_var}" COMPONENT_TAG="${!tag_var}" - # 调试输出,检查变量是否正确 echo "Component: $component" echo " Repo: $COMPONENT_REPO" echo " Image: $COMPONENT_IMAGE" @@ -1057,19 +1123,19 @@ if [[ "${USE_LOCAL_COMPONENT,,}" == "true" ]]; then fi - - echo "==> Creating and starting ${component} container... ${local_components_ports[$component]}" + port=$(get_port $component) + echo "==> Creating and starting ${component} container... ${port}" local component_container_id component_container_id="$( docker run -d \ --name "$component" \ --link "$DEVBOX_NAME" \ - -p "${local_components_ports[$component]}:${local_components_ports[$component]}" \ + -p "${port}:${port}" \ -v /var/run/docker.sock:/var/run/docker.sock \ - -e SERVICE_API_ACCESS_PORT=${local_components_ports[$component]} \ + -e SERVICE_API_ACCESS_PORT=${port} \ -e SERVICE_API_ACCESS_HOST=0.0.0.0 \ "$component_full_image" \ - uvicorn webapi.main:app --reload --port ${local_components_ports[$component]} --host 0.0.0.0 2>/dev/null + uvicorn webapi.main:app --reload --port ${port} --host 0.0.0.0 2>/dev/null )" if [[ -z "$component_container_id" ]]; then echo "WARNING: Failed to create ${component} container. please Check the image and specify it to initialize the component." @@ -1123,7 +1189,7 @@ echo "step 2: Update /home/.devbox/freeleaps/apps/.env" # Get default IP address DEFAULT_IP=\$(ip route | grep default | sed -n 's/.*default via \([^ ]*\).*/\1/p') -if [[ "\${USE_LOCAL_COMPONENT,,}" == "true" ]]; then +if [[ "\$(lower "\$USE_LOCAL_COMPONENT")" == "true" ]]; then echo "==> Using local components" # Local components for Freeleaps services (devsvc, payment, content, central_storage, authentication) cat << 'EOFinner' > /home/.devbox/freeleaps/apps/.env @@ -1249,7 +1315,7 @@ echo '============================================' ./start_webapi.sh > /home/.devbox/logs/backend.logs 2>&1 & BACKEND_PID=\$! -# Save BACKEND_PID to a file \${WORKING_HOME}/.frontend.pid: Stores the process id of frontend process. +# Save BACKEND_PID to a file \${WORKING_HOME}/.backend.pid: Stores the process id of backend process. echo "\$BACKEND_PID" > /home/.devbox/.backend.pid echo '============================================' @@ -1299,10 +1365,9 @@ npm install -g pnpm pnpm install npm run build npm run format -# Start the frontend service with nohup in order to keep it running after the SSH session is closed -nohup npm run dev > /home/.devbox/logs/frontend.logs > /dev/null 2>&1 & -# Save the process ID of the frontend service -FRONTEND_PID=$! +# Start the frontend service with nohup in order to keep it running after the SSH session is closed. Save the process ID of the frontend service +nohup npm run dev > /home/.devbox/logs/frontend.logs 2>&1 & +FRONTEND_PID=\$! echo "npm run dev has been started with PID: \$FRONTEND_PID" echo "\$FRONTEND_PID" > /home/.devbox/.frontend.pid @@ -1366,10 +1431,10 @@ devbox_deinit_command() { # src/deinit_command.sh echo "# It contains the implementation for the 'devbox deinit' command." - local WORKING_HOME="${args['--working-home']:-${HOME}/.devbox}" - local CLEAR_LOGS="${args['--clear-logs']:-false}" - local CLEAR_REPO="${args['--clear-repo']:-false}" - local FORCE="${args['--force']}" + local WORKING_HOME="$(get_arg '--working-home' "${HOME}/.devbox")" + local CLEAR_LOGS="$(get_arg '--clear-logs' 'false')" + local CLEAR_REPO="$(get_arg '--clear-repo' 'false')" + local FORCE="$(get_arg '--force')" # print the parameters echo "==> Deinitialization parameters:" @@ -1412,8 +1477,8 @@ devbox_deinit_command() { # :command.function devbox_start_command() { - local COMPONENT="${args['--component']}" - local WORKING_HOME="${args['--working-home']:-${HOME}/.devbox}" + local COMPONENT="$(get_arg '--component')" + local WORKING_HOME="$(get_arg '--working-home' "${HOME}/.devbox")" # Check if the DevBox container is running local devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" @@ -1426,10 +1491,10 @@ devbox_start_command() { echo "==> Starting DevBox services..." - # 检查 DevBox 容器的状态 + # Check if DevBox container is running if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then echo "==> DevBox container is not running, starting container..." - # 启动容器 + # Start the container if ! docker start "${devbox_container_id}"; then echo "ERROR: Failed to start DevBox container." exit 1 @@ -1439,14 +1504,14 @@ devbox_start_command() { echo "==> DevBox container is already running." fi - # 如果未指定组件,启动所有组件 + # If no component is specified, start all components if [[ -z "$COMPONENT" ]]; then COMPONENTS=("mongodb" "rabbitmq" "backend" "frontend") else COMPONENTS=("$COMPONENT") fi - # 启动指定的组件 + # Start the specified components for comp in "${COMPONENTS[@]}"; do case "$comp" in "mongodb") @@ -1459,7 +1524,7 @@ devbox_start_command() { local mongodb_container_id=$(cat "$mongodb_container_id_file_path") if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${mongodb_container_id}\$"; then echo "==> MongoDB container is not running, starting container..." - # 启动容器 + # Start the container if ! docker start "${mongodb_container_id}"; then echo "ERROR: Failed to start MongoDB container." exit 1 @@ -1480,7 +1545,7 @@ devbox_start_command() { local rabbitmq_container_id=$(cat "$rabbitmq_container_id_file_path") if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${rabbitmq_container_id}\$"; then echo "==> RabbitMQ container is not running, starting container..." - # 启动容器 + # Start the container if ! docker start "${rabbitmq_container_id}"; then echo "ERROR: Failed to start RabbitMQ container." exit 1 @@ -1494,7 +1559,7 @@ devbox_start_command() { "backend") echo "==> Starting backend service..." # start the backend service - docker exec -i "$devbox_container_id" bash <<'EOF' + docker exec -i "$devbox_container_id" bash < /dev/null; then echo "Backend service is already running." else @@ -1533,6 +1602,7 @@ else ATTEMPT=0 # Check if \$DEVBOX_BACKEND_PORT exists + DEVBOX_BACKEND_PORT=\$(cat /home/.devbox/.devbox-backend-port) if [ -z "\$DEVBOX_BACKEND_PORT" ]; then echo "ERROR: DEVBOX_BACKEND_PORT is not set." export DEVBOX_BACKEND_PORT=8002 @@ -1564,23 +1634,31 @@ EOF "frontend") echo "==> Starting frontend service..." # Start the frontend service - docker exec -i "$devbox_container_id" bash <<'EOF' + docker exec -i "$devbox_container_id" bash < /dev/null; then - echo "Frontend service is already running." - exit 0 - else - # Remove the frontend.pid file - rm -f /home/.devbox/.frontend.pid - fi - fi + frontend_pid=\$(cat /home/.devbox/.frontend.pid) + + # Remove empty spaces and new lines in the frontend_pid + frontend_pid=\$(echo "\$frontend_pid" | tr -d '[:space:]') + + # Check if frontend pid is empty + if [ -z "\$frontend_pid" ]; then + echo "Frontend service is not running. " + else + echo '============================================' + if ps -p "\$frontend_pid" > /dev/null; then + echo "Frontend service is already running." + exit 0 + fi + fi + + # Remove the .frontend.pid file before starting the frontend service + rm -f /home/.devbox/.frontend.pid + fi echo '============================================' echo ' Start frontend service locally' echo '============================================' @@ -1604,25 +1682,27 @@ EOF # Start the frontend service with nohup in order to keep it running after the SSH session is closed # Save the process ID of the frontend service - nohup npm run dev > /home/.devbox/logs/frontend.logs > /dev/null 2>&1 & - FRONTEND_PID=$! + + nohup npm run dev > /home/.devbox/logs/frontend.logs 2>&1 & + FRONTEND_PID=\$! echo "npm run dev has been started with PID: \$FRONTEND_PID" echo "\$FRONTEND_PID" > /home/.devbox/.frontend.pid # Wait for the frontend service to start - sleep 30 + sleep 10 # 30 attempts, 10 seconds each, total wait time 5 minutes MAX_ATTEMPTS=30 ATTEMPT=0 - # Check if \$DEVBOX_FRONTEND_PORT exists + DEVBOX_FRONTEND_PORT=\$(cat /home/.devbox/.devbox-frontend-port) + # get DEVBOX_FRONTEND_PORT from environment variables if [ -z "\$DEVBOX_FRONTEND_PORT" ]; then echo "ERROR: DEVBOX_FRONTEND_PORT is not set." export DEVBOX_FRONTEND_PORT=5173 fi - + echo "Waiting for Frontend service to start..." while [ \$ATTEMPT -lt \$MAX_ATTEMPTS ]; do HTTP_CODE=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$DEVBOX_FRONTEND_PORT/") @@ -1661,9 +1741,8 @@ EOF # :command.function devbox_stop_command() { echo "==> Stopping DevBox services..." - - local COMPONENT="${args['--component']}" - local WORKING_HOME="${args['--working-home']:-${HOME}/.devbox}" + local COMPONENT="$(get_arg '--component')" + local WORKING_HOME="$(get_arg '--working-home' "${HOME}/.devbox")" echo "==> Stopping DevBox services..." @@ -1731,8 +1810,8 @@ devbox_stop_command() { devbox_status_command() { echo "==> Checking DevBox services status..." - local COMPONENT="${args['--component']}" - local WORKING_HOME="${args['--working-home']:-${HOME}/.devbox}" + local COMPONENT="$(get_arg '--component')" + local WORKING_HOME="$(get_arg '--working-home' "${HOME}/.devbox")" # Check if .devbox-instance file exists if [[ ! -f "${WORKING_HOME}/.devbox-instance" ]]; then @@ -1830,9 +1909,9 @@ devbox_status_command() { # :command.function devbox_restart_command() { echo "==> Restarting DevBox services..." - local COMPONENT="${args['--component']}" - local WORKING_HOME="${args['--working-home']:-${HOME}/.devbox}" - + local COMPONENT="$(get_arg '--component')" + local WORKING_HOME="$(get_arg '--working-home' "${HOME}/.devbox")" + # Check devbox container file path local devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" if [[ ! -f "$devbox_container_id_file_path" ]]; then @@ -2052,8 +2131,8 @@ EOF # Start the frontend service with nohup in order to keep it running after the SSH session is closed # Save the process ID of the frontend service - nohup npm run dev > /home/.devbox/logs/frontend.logs > /dev/null 2>&1 & - FRONTEND_PID=$! + nohup npm run dev > /home/.devbox/logs/frontend.logs 2>&1 & + FRONTEND_PID=\$! echo "npm run dev has been started with PID: \$FRONTEND_PID" echo "\$FRONTEND_PID" > /home/.devbox/.frontend.pid @@ -2250,7 +2329,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--os']="$2" + add_arg '--os' "$2" shift shift else @@ -2264,7 +2343,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--arch']="$2" + add_arg '--arch' "$2" shift shift else @@ -2278,7 +2357,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devbox-container-name']="$2" + add_arg '--devbox-container-name' "$2" shift shift else @@ -2292,7 +2371,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devbox-container-port']="$2" + add_arg '--devbox-container-port' "$2" shift shift else @@ -2306,7 +2385,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devbox-image-repo']="$2" + add_arg '--devbox-image-repo' "$2" shift shift else @@ -2320,7 +2399,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devbox-image-name']="$2" + add_arg '--devbox-image-name' "$2" shift shift else @@ -2334,7 +2413,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devbox-image-tag']="$2" + add_arg '--devbox-image-tag' "$2" shift shift else @@ -2348,7 +2427,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--working-home']="$2" + add_arg '--working-home' "$2" shift shift else @@ -2362,7 +2441,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--freeleaps-username']="$2" + add_arg '--freeleaps-username' "$2" shift shift else @@ -2376,7 +2455,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--freeleaps-password']="$2" + add_arg '--freeleaps-password' "$2" shift shift else @@ -2388,7 +2467,7 @@ devbox_init_parse_requirements() { # :flag.case --use-local-component) if [[ -n ${2+x} ]]; then - args['--use-local-component']="$2" + add_arg '--use-local-component' "$2" shift 2 else printf "%s\n" "--use-local-component requires an argument: --use-local-component USING_LOCAL_COMPONENT" >&2 @@ -2401,7 +2480,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devsvc-image-repo']="$2" + add_arg '--devsvc-image-repo' "$2" shift shift else @@ -2415,7 +2494,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devsvc-image-name']="$2" + add_arg '--devsvc-image-name' "$2" shift shift else @@ -2429,7 +2508,7 @@ devbox_init_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--devsvc-image-tag']="$2" + add_arg '--devsvc-image-tag' "$2" shift shift else @@ -2441,7 +2520,7 @@ devbox_init_parse_requirements() { # :flag.case --payment-image-repo) if [[ -n ${2+x} ]]; then - args['--payment-image-repo']="$2" + add_arg '--payment-image-repo' "$2" shift 2 else printf "%s\n" "--payment-image-repo requires an argument: --payment-image-repo FREELEAPS_PAYMENT_IMAGE_REPO" >&2 @@ -2452,7 +2531,7 @@ devbox_init_parse_requirements() { # :flag.case --payment-image-name) if [[ -n ${2+x} ]]; then - args['--payment-image-name']="$2" + add_arg '--payment-image-name' "$2" shift 2 else printf "%s\n" "--payment-image-name requires an argument: --payment-image-name FREELEAPS_PAYMENT_IMAGE_NAME" >&2 @@ -2463,7 +2542,7 @@ devbox_init_parse_requirements() { # :flag.case --payment-image-tag) if [[ -n ${2+x} ]]; then - args['--payment-image-tag']="$2" + add_arg '--payment-image-tag' "$2" shift 2 else printf "%s\n" "--payment-image-tag requires an argument: --payment-image-tag FREELEAPS_PAYMENT_IMAGE_TAG" >&2 @@ -2474,7 +2553,7 @@ devbox_init_parse_requirements() { # :flag.case --content-image-repo) if [[ -n ${2+x} ]]; then - args['--content-image-repo']="$2" + add_arg '--content-image-repo' "$2" shift 2 else printf "%s\n" "--content-image-repo requires an argument: --content-image-repo FREELEAPS_CONTENT_IMAGE_REPO" >&2 @@ -2485,7 +2564,7 @@ devbox_init_parse_requirements() { # :flag.case --content-image-name) if [[ -n ${2+x} ]]; then - args['--content-image-name']="$2" + add_arg '--content-image-name' "$2" shift 2 else printf "%s\n" "--content-image-name requires an argument: --content-image-name FREELEAPS_CONTENT_IMAGE_NAME" >&2 @@ -2496,7 +2575,7 @@ devbox_init_parse_requirements() { # :flag.case --content-image-tag) if [[ -n ${2+x} ]]; then - args['--content-image-tag']="$2" + add_arg '--content-image-tag' "$2" shift 2 else printf "%s\n" "--content-image-tag requires an argument: --content-image-tag FREELEAPS_CONTENT_IMAGE_TAG" >&2 @@ -2507,7 +2586,7 @@ devbox_init_parse_requirements() { # :flag.case --central_storage-image-repo) if [[ -n ${2+x} ]]; then - args['--central_storage-image-repo']="$2" + add_arg '--central_storage-image-repo' "$2" shift 2 else printf "%s\n" "--central_storage-image-repo requires an argument: --central_storage-image-repo FREELEAPS_CENTRAL_STORAGE_IMAGE_REPO" >&2 @@ -2518,7 +2597,7 @@ devbox_init_parse_requirements() { # :flag.case --central_storage-image-name) if [[ -n ${2+x} ]]; then - args['--central_storage-image-name']="$2" + add_arg '--central_storage-image-name' "$2" shift 2 else printf "%s\n" "--central_storage-image-name requires an argument: --central_storage-image-name FREELEAPS_CENTRAL_STORAGE_IMAGE_NAME" >&2 @@ -2529,7 +2608,7 @@ devbox_init_parse_requirements() { # :flag.case --central_storage-image-tag) if [[ -n ${2+x} ]]; then - args['--central_storage-image-tag']="$2" + add_arg '--central_storage-image-tag' "$2" shift 2 else printf "%s\n" "--central_storage-image-tag requires an argument: --central_storage-image-tag FREELEAPS_CENTRAL_STORAGE_IMAGE_TAG" >&2 @@ -2540,7 +2619,7 @@ devbox_init_parse_requirements() { # :flag.case --authentication-image-repo) if [[ -n ${2+x} ]]; then - args['--authentication-image-repo']="$2" + add_arg '--authentication-image-repo' "$2" shift 2 else printf "%s\n" "--authentication-image-repo requires an argument: --authentication-image-repo FREELEAPS_AUTHENTICATION_IMAGE_REPO" >&2 @@ -2551,7 +2630,7 @@ devbox_init_parse_requirements() { # :flag.case --authentication-image-name) if [[ -n ${2+x} ]]; then - args['--authentication-image-name']="$2" + add_arg '--authentication-image-name' "$2" shift 2 else printf "%s\n" "--authentication-image-name requires an argument: --authentication-image-name FREELEAPS_AUTHENTICATION_IMAGE_NAME" >&2 @@ -2562,7 +2641,7 @@ devbox_init_parse_requirements() { # :flag.case --authentication-image-tag) if [[ -n ${2+x} ]]; then - args['--authentication-image-tag']="$2" + add_arg '--authentication-image-tag' "$2" shift 2 else printf "%s\n" "--authentication-image-tag requires an argument: --authentication-image-tag FREELEAPS_AUTHENTICATION_IMAGE_TAG" >&2 @@ -2573,7 +2652,7 @@ devbox_init_parse_requirements() { --force | -f) # :flag.case_no_arg - args['--force']=1 + add_arg '--force' '1' shift ;; @@ -2594,33 +2673,82 @@ devbox_init_parse_requirements() { done # :command.required_flags_filter - if [[ -z ${args['--freeleaps-username']+x} ]]; then + + if [[ -z "$(get_arg '--freeleaps-username')" ]]; then + printf "missing required flag: --freeleaps-username FREELEAPS_USERNAME\n" >&2 exit 1 fi - if [[ -z ${args['--freeleaps-password']+x} ]]; then + if [[ -z "$(get_arg '--freeleaps-password')" ]]; then printf "missing required flag: --freeleaps-password FREELEAPS_PASSWORD\n" >&2 exit 1 fi # :command.default_assignments - [[ -n ${args['--os']:-} ]] || args['--os']="auto" - [[ -n ${args['--arch']:-} ]] || args['--arch']="auto" - [[ -n ${args['--devbox-container-name']:-} ]] || args['--devbox-container-name']="devbox" - [[ -n ${args['--devbox-container-port']:-} ]] || args['--devbox-container-port']="22222" - [[ -n ${args['--devbox-frontend-port']:-} ]] || args['--devbox-frontend-port']="5173" - [[ -n ${args['--devbox-backend-port']:-} ]] || args['--devbox-backend-port']="8002" - - [[ -n ${args['--devbox-image-repo']:-} ]] || args['--devbox-image-repo']="docker.io/freeleaps" - [[ -n ${args['--devbox-image-name']:-} ]] || args['--devbox-image-name']="devbox_v1" - [[ -n ${args['--devbox-image-tag']:-} ]] || args['--devbox-image-tag']="devbox_local" - [[ -n ${args['--devsvc-image-tag']:-} ]] || args['--devsvc-image-tag']="latest-linux-arm64" - [[ -n ${args['--payment-image-tag']:-} ]] || args['--payment-image-tag']="latest-linux-arm64" - [[ -n ${args['--content-image-tag']:-} ]] || args['--content-image-tag']="latest-linux-arm64" - [[ -n ${args['--central_storage-image-tag']:-} ]] || args['--central_storage-image-tag']="latest-linux-arm64" - [[ -n ${args['--authentication-image-tag']:-} ]] || args['--authentication-image-tag']="latest-linux-arm64" - [[ -n ${args['--working-home']:-} ]] || args['--working-home']="${HOME}/.devbox" - [[ -n ${args['--use-local-component']:-} ]] || args['--use-local-component']="true" + + if [ -z "$(get_arg '--os')" ]; then + add_arg '--os' "auto" + fi + + if [ -z "$(get_arg '--arch')" ]; then + add_arg '--arch' "auto" + fi + + if [ -z "$(get_arg '--devbox-container-name')" ]; then + add_arg '--devbox-container-name' "devbox" + fi + + if [ -z "$(get_arg '--devbox-container-port')" ]; then + add_arg '--devbox-container-port' "22222" + fi + + if [ -z "$(get_arg '--devbox-frontend-port')" ]; then + add_arg '--devbox-frontend-port' "5173" + fi + + if [ -z "$(get_arg '--devbox-backend-port')" ]; then + add_arg '--devbox-backend-port' "8002" + fi + + if [ -z "$(get_arg '--devbox-image-repo')" ]; then + add_arg '--devbox-image-repo' "docker.io/freeleaps" + fi + + if [ -z "$(get_arg '--devbox-image-name')" ]; then + add_arg '--devbox-image-name' "devbox_v1" + fi + + if [ -z "$(get_arg '--devbox-image-tag')" ]; then + add_arg '--devbox-image-tag' "devbox_local" + fi + + if [ -z "$(get_arg '--devsvc-image-tag')" ]; then + add_arg '--devsvc-image-tag' "latest-linux-arm64" + fi + + if [ -z "$(get_arg '--payment-image-tag')" ]; then + add_arg '--payment-image-tag' "latest-linux-arm64" + fi + + if [ -z "$(get_arg '--content-image-tag')" ]; then + add_arg '--content-image-tag' "latest-linux-arm64" + fi + + if [ -z "$(get_arg '--central_storage-image-tag')" ]; then + add_arg '--central_storage-image-tag' "latest-linux-arm64" + fi + + if [ -z "$(get_arg '--authentication-image-tag')" ]; then + add_arg '--authentication-image-tag' "latest-linux-arm64" + fi + + if [ -z "$(get_arg '--working-home')" ]; then + add_arg '--working-home' "${HOME}/.devbox" + fi + + if [ -z "$(get_arg '--use-local-component')" ]; then + add_arg '--use-local-component' "true" + fi } @@ -2657,7 +2785,7 @@ devbox_deinit_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--working-home']="$2" + add_arg '--working-home' "$2" shift shift else @@ -2671,7 +2799,7 @@ devbox_deinit_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--clear-logs']="$2" + add_arg '--clear-logs' "$2" shift shift else @@ -2685,7 +2813,7 @@ devbox_deinit_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--clear-repo']="$2" + add_arg '--clear-repo' "$2" shift shift else @@ -2711,9 +2839,15 @@ devbox_deinit_parse_requirements() { done # :command.default_assignments - [[ -n ${args['--working-home']:-} ]] || args['--working-home']="${HOME}/.devbox" - [[ -n ${args['--clear-logs']:-} ]] || args['--clear-logs']="true" - [[ -n ${args['--clear-repo']:-} ]] || args['--clear-repo']="false" + if [ -z "$(get_arg '--working-home')" ]; then + add_arg '--working-home' "${HOME}/.devbox" + fi + if [ -z "$(get_arg '--clear-logs')" ]; then + add_arg '--clear-logs' "true" + fi + if [ -z "$(get_arg '--clear-repo')" ]; then + add_arg '--clear-repo' "false" + fi } @@ -2748,7 +2882,7 @@ devbox_start_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--component']="$2" + add_arg '--component' "$2" shift shift else @@ -2806,7 +2940,7 @@ devbox_stop_parse_requirements() { # :flag.case_arg if [[ -n ${2+x} ]]; then - args['--component']="$2" + add_arg '--component' "$2" shift shift else @@ -2923,18 +3057,23 @@ devbox_restart_parse_requirements() { } -# :command.initialize + + initialize() { version="1.0.0" - long_usage='' + long_usage="" set -e - # :command.globals - declare -g -A args=() - declare -g -A deps=() - declare -g -a env_var_names=() - declare -g -a input=() + # Use the following variables to define the environment variables and input parameters + args_keys=() + args_values=() + deps_keys=() + deps_values=() + + # For each environment variable, add a line like the following: + env_var_names=() + input=() } # :command.run