From 6c14613f5f201e9ed4a5e8e309488af8e9d8b950 Mon Sep 17 00:00:00 2001 From: Jay Rogers Date: Tue, 10 Dec 2024 13:22:18 -0600 Subject: [PATCH] Improve argument parsing. Fixes #136 --- lib/actions/deploy.sh | 75 +++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/lib/actions/deploy.sh b/lib/actions/deploy.sh index 00f5601e..614b5fb4 100755 --- a/lib/actions/deploy.sh +++ b/lib/actions/deploy.sh @@ -99,12 +99,42 @@ action_deploy() { validate_project_setup - # Set deployment environment for first argument that doesn't start with a dash - for arg in "$@"; do - if [[ "$arg" != -* && -z "$deployment_environment" ]]; then - deployment_environment="$arg" - break - fi + # Process arguments + while [[ $# -gt 0 ]]; do + case "$1" in + --user | -u) + ssh_user="$2" + shift 2 + ;; + --compose-file | -c) + if [[ -n "$2" && "$2" != -* ]]; then + compose_files+=("$2") + shift 2 + else + echo "${BOLD}${RED}❌Error: '-c' option requires a Docker Compose file as argument.${RESET}" + exit 1 + fi + ;; + --port | -p) + ssh_port="$2" + shift 2 + ;; + --upgrade|-U) + SPIN_FORCE_INSTALL_GALAXY_DEPS=true + shift + ;; + -*) + echo "${BOLD}${RED}❌Error: Unknown option $1${RESET}" + exit 1 + ;; + *) + # Only set deployment_environment if it hasn't been set yet + if [[ -z "$deployment_environment" ]]; then + deployment_environment="$1" + fi + shift + ;; + esac done # If no environment specified, default to production @@ -162,39 +192,6 @@ action_deploy() { ssh_user="${SPIN_SSH_USER:-"deploy"}" spin_project_name="${SPIN_PROJECT_NAME:-"spin"}" - # Process arguments - while [[ $# -gt 0 ]]; do - case "$1" in - --user | -u) - ssh_user="$2" - shift 2 - ;; - --compose-file | -c) - if [[ -n "$2" && "$2" != -* ]]; then - compose_files+=("$2") - shift 2 - else - echo "${BOLD}${RED}❌Error: '-c' option requires a Docker Compose file as argument.${RESET}" - exit 1 - fi - ;; - --port | -p) - ssh_port="$2" - shift 2 - ;; - --upgrade|-U) - SPIN_FORCE_INSTALL_GALAXY_DEPS=true - shift - ;; - *) - if [[ -z "$deployment_environment" ]]; then # capture the first positional argument as environment - deployment_environment="$1" - fi - shift - ;; - esac - done - # Clean up services on exit trap cleanup_on_exit EXIT