Skip to content

Commit

Permalink
Refactored main command
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrogers committed Dec 21, 2023
1 parent 4124f7d commit 62d6be2
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 170 deletions.
306 changes: 136 additions & 170 deletions bin/spin
Original file line number Diff line number Diff line change
@@ -1,191 +1,157 @@
#!/usr/bin/env bash

# set -x
set -e

SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SPIN_HOME=$(dirname "$SCRIPT_DIR") #Assume the parent directory of this script is the home
SPIN_CACHE_DIR=${SPIN_CACHE_DIR:-$SPIN_HOME/cache}
SPIN_CONFIG_FILE_LOCATION=${SPIN_CONFIG_FILE_LOCATION:-"$SPIN_HOME/conf/spin.conf"}
AUTO_UPDATE_INTERVAL_IN_DAYS=${AUTO_UPDATE_INTERVAL_IN_DAYS:-14}
AUTO_PULL_INTERVAL_IN_DAYS=${AUTO_PULL_INTERVAL_IN_DAYS:-1}
# Default Environment
SPIN_ENV=${SPIN_ENV:-dev}

# Set up our structure for our re-used commands
export COMPOSE_CMD=${COMPOSE_CMD:-"docker compose"}

### Default Images
# Export the current user and group ID
export SPIN_USER_ID=$(id -u)
export SPIN_GROUP_ID=$(id -g)

# Default Images
SPIN_PHP_IMAGE=${SPIN_PHP_IMAGE:-"serversideup/php:beta-cli"}
SPIN_NODE_IMAGE=${SPIN_NODE_IMAGE:-"node:20"}
SPIN_ANSIBLE_IMAGE=${SPIN_ANSIBLE_IMAGE:-"willhallonline/ansible:2.15-alpine-3.18"}

### Default Users
# Default Service Names
SPIN_DEFAULT_PHP_SERVICE_NAME=${SPIN_DEFAULT_PHP_RUN_SERVICE:-"php"}

# Export the current user and group ID
export SPIN_USER_ID=$(id -u)
export SPIN_GROUP_ID=$(id -g)
# Script Configuration
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SPIN_HOME=$(dirname "$SCRIPT_DIR") #Assume the parent directory of this script is the home
SPIN_CACHE_DIR=${SPIN_CACHE_DIR:-$SPIN_HOME/cache}
SPIN_CONFIG_FILE_LOCATION=${SPIN_CONFIG_FILE_LOCATION:-"$SPIN_HOME/conf/spin.conf"}
AUTO_UPDATE_INTERVAL_IN_DAYS=${AUTO_UPDATE_INTERVAL_IN_DAYS:-14}
AUTO_PULL_INTERVAL_IN_DAYS=${AUTO_PULL_INTERVAL_IN_DAYS:-1}

# Import common functions
source "$SPIN_HOME/lib/functions.sh"
setup_color

main() {
## Set defaults for our environment

# Select environments to be used
# Allows passing as `ENV` environment variable
# or defaults to `dev`
SPIN_ENV=${SPIN_ENV:-dev}

# Convert the SPIN_ENV variable into an array of environments
IFS=',' read -ra ENV_ARRAY <<< "$SPIN_ENV"

# Initialize the COMPOSE_FILE variable
COMPOSE_FILE="docker-compose.yml"
# Check for upgrades, except for development installs and on `spin update`
if [[ $(detect_installation_type) != "development" && "$1" != "update" ]]; then
check_for_upgrade
fi

# Loop over each environment and append the corresponding compose file
for env in "${ENV_ARRAY[@]}"; do
COMPOSE_FILE="$COMPOSE_FILE:docker-compose.$env.yml"
done
# Set COMPOSE_FILE variable
export_compose_file_variable

# Export the COMPOSE_FILE variable
export COMPOSE_FILE
# Validate a few things based on the user's intent
case "$1" in
"" | base64 | debug | help | update | version | --version | -v)
: # Silent output for the user, but we're skipping the compose check too.
;;
init | kill | new | prune | provision | vault)
check_if_docker_is_running
;;
*)
check_if_docker_is_running
check_if_compose_files_exist "$COMPOSE_FILE"
;;
esac

# Check if 'set -x' is enabled
if [[ $- == *x* ]]; then
# If 'set -x' is enabled, echo the COMPOSE_FILE variable
echo "SPIN_ENV: $SPIN_ENV"
echo "COMPOSE_FILE: $COMPOSE_FILE"
fi
# Check that an argument is passed
if [ $# -gt 0 ]; then
spin_action=$1
shift 1 # Remove the action from the arguments

case "$1" in
"" | base64 | debug | help | update | version | --version | -v)
: # Silent output for the user, but we're skipping the compose check too.
;;
init | kill | new | prune | provision | vault)
check_if_docker_is_running
case $spin_action in
base64)
source "$SPIN_HOME/lib/actions/base64.sh"
action_base64 "$@"
;;
build)
source "$SPIN_HOME/lib/actions/build.sh"
action_build "$@"
;;
debug)
source "$SPIN_HOME/lib/actions/debug.sh"
action_debug "$@"
;;
down)
source "$SPIN_HOME/lib/actions/down.sh"
action_down "$@"
;;
exec)
source "$SPIN_HOME/lib/actions/exec.sh"
action_exec "$@"
;;
help)
source "$SPIN_HOME/lib/actions/help.sh"
action_help
;;
init)
source "$SPIN_HOME/lib/actions/init.sh"
action_init "$@"
;;
kill)
source "$SPIN_HOME/lib/actions/kill.sh"
action_kill
;;
latest)
source "$SPIN_HOME/lib/actions/latest.sh"
action_latest "$@"
;;
logs)
source "$SPIN_HOME/lib/actions/logs.sh"
action_logs "$@"
;;
new)
source "$SPIN_HOME/lib/actions/new.sh"
action_new "$@"
;;
php)
source "$SPIN_HOME/lib/actions/php.sh"
action_php "$@"
;;
provision)
source "$SPIN_HOME/lib/actions/provision.sh"
action_provision "$@"
;;
prune)
source "$SPIN_HOME/lib/actions/prune.sh"
action_prune "$@"
;;
pull)
source "$SPIN_HOME/lib/actions/pull.sh"
action_pull "$@"
;;
run)
source "$SPIN_HOME/lib/actions/run.sh"
action_run "$@"
;;
stop)
source "$SPIN_HOME/lib/actions/stop.sh"
action_stop
;;
up)
source "$SPIN_HOME/lib/actions/up.sh"
action_up "$@"
;;
update)
source "$SPIN_HOME/lib/actions/update.sh"
action_update
;;
vault)
source "$SPIN_HOME/lib/actions/vault.sh"
action_vault "$@"
;;
version | --version | -v)
source "$SPIN_HOME/lib/actions/version.sh"
action_version
;;
*)
check_if_docker_is_running
check_if_compose_files_exist "$COMPOSE_FILE"
;;
echo "\"$1\" is not a valid command. Below are the commands available."
source "$SPIN_HOME/lib/actions/help.sh"
action_help
;;
esac


# Set up our structure for our re-used commands
export COMPOSE_CMD=${COMPOSE_CMD:-"docker compose"}

# Check that an argument is passed
if [ $# -gt 0 ]; then
local action=$1
shift 1 # Remove the action from the arguments

case $action in
base64)
source "$SPIN_HOME/lib/actions/base64.sh"
action_base64 "$@"
;;
build)
source "$SPIN_HOME/lib/actions/build.sh"
action_build "$@"
;;
debug)
source "$SPIN_HOME/lib/actions/debug.sh"
action_debug "$@"
;;
down)
source "$SPIN_HOME/lib/actions/down.sh"
action_down "$@"
;;
exec)
source "$SPIN_HOME/lib/actions/exec.sh"
action_exec "$@"
;;
help)
source "$SPIN_HOME/lib/actions/help.sh"
action_help
;;
init)
source "$SPIN_HOME/lib/actions/init.sh"
action_init "$@"
;;
kill)
source "$SPIN_HOME/lib/actions/kill.sh"
action_kill
;;
latest)
source "$SPIN_HOME/lib/actions/latest.sh"
action_latest "$@"
;;
logs)
source "$SPIN_HOME/lib/actions/logs.sh"
action_logs "$@"
;;
new)
source "$SPIN_HOME/lib/actions/new.sh"
action_new "$@"
;;
php)
source "$SPIN_HOME/lib/actions/php.sh"
action_php "$@"
;;
provision)
source "$SPIN_HOME/lib/actions/provision.sh"
action_provision "$@"
;;
prune)
source "$SPIN_HOME/lib/actions/prune.sh"
action_prune "$@"
;;
pull)
source "$SPIN_HOME/lib/actions/pull.sh"
action_pull "$@"
;;
run)
source "$SPIN_HOME/lib/actions/run.sh"
action_run "$@"
;;
stop)
source "$SPIN_HOME/lib/actions/stop.sh"
action_stop
;;
up)
source "$SPIN_HOME/lib/actions/up.sh"
action_up "$@"
;;
update)
source "$SPIN_HOME/lib/actions/update.sh"
action_update
;;
vault)
source "$SPIN_HOME/lib/actions/vault.sh"
action_vault "$@"
;;
version | --version | -v)
source "$SPIN_HOME/lib/actions/version.sh"
action_version
;;
*)
echo "\"$1\" is not a valid command. Below are the commands available."
source "$SPIN_HOME/lib/actions/help.sh"
action_help
;;
esac
else
printf "${BOLD}${YELLOW}🤔 You didn't pass \"spin\" any arguments.${RESET}"
echo
source "$SPIN_HOME/lib/actions/help.sh"
action_help
fi
}

################################################
# 🚀 Where the script actually starts
################################################

setup_color

installation_type=$(detect_installation_type)
if [[ $installation_type != "development" && "$1" != "update" ]]; then
check_for_upgrade
fi

unset installation_type

main "$@" #Passing the original arguments to `main`
else
printf "${BOLD}${YELLOW}🤔 You didn't pass \"spin\" any arguments.${RESET}"
echo
source "$SPIN_HOME/lib/actions/help.sh"
action_help
fi
24 changes: 24 additions & 0 deletions lib/functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
#!/usr/bin/env bash

export_compose_file_variable(){
# Convert the SPIN_ENV variable into an array of environments
IFS=',' read -ra ENV_ARRAY <<< "$SPIN_ENV"

# Initialize the COMPOSE_FILE variable
COMPOSE_FILE="docker-compose.yml"

# Loop over each environment and append the corresponding compose file
for env in "${ENV_ARRAY[@]}"; do
COMPOSE_FILE="$COMPOSE_FILE:docker-compose.$env.yml"
done

# Export the COMPOSE_FILE variable
export COMPOSE_FILE

# Check if 'set -x' is enabled
if [[ $- == *x* ]]; then
# If 'set -x' is enabled, echo the COMPOSE_FILE variable
echo "SPIN_ENV: $SPIN_ENV"
echo "COMPOSE_FILE: $COMPOSE_FILE"
fi
}

install_spin_package_to_project() {
framework="$1"
project_name="$2"
Expand Down

0 comments on commit 62d6be2

Please sign in to comment.