Skip to content

Commit 62d6be2

Browse files
committed
Refactored main command
1 parent 4124f7d commit 62d6be2

File tree

2 files changed

+160
-170
lines changed

2 files changed

+160
-170
lines changed

bin/spin

+136-170
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,157 @@
11
#!/usr/bin/env bash
2-
3-
# set -x
42
set -e
53

6-
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
7-
SPIN_HOME=$(dirname "$SCRIPT_DIR") #Assume the parent directory of this script is the home
8-
SPIN_CACHE_DIR=${SPIN_CACHE_DIR:-$SPIN_HOME/cache}
9-
SPIN_CONFIG_FILE_LOCATION=${SPIN_CONFIG_FILE_LOCATION:-"$SPIN_HOME/conf/spin.conf"}
10-
AUTO_UPDATE_INTERVAL_IN_DAYS=${AUTO_UPDATE_INTERVAL_IN_DAYS:-14}
11-
AUTO_PULL_INTERVAL_IN_DAYS=${AUTO_PULL_INTERVAL_IN_DAYS:-1}
4+
# Default Environment
5+
SPIN_ENV=${SPIN_ENV:-dev}
6+
7+
# Set up our structure for our re-used commands
8+
export COMPOSE_CMD=${COMPOSE_CMD:-"docker compose"}
129

13-
### Default Images
10+
# Export the current user and group ID
11+
export SPIN_USER_ID=$(id -u)
12+
export SPIN_GROUP_ID=$(id -g)
13+
14+
# Default Images
1415
SPIN_PHP_IMAGE=${SPIN_PHP_IMAGE:-"serversideup/php:beta-cli"}
1516
SPIN_NODE_IMAGE=${SPIN_NODE_IMAGE:-"node:20"}
1617
SPIN_ANSIBLE_IMAGE=${SPIN_ANSIBLE_IMAGE:-"willhallonline/ansible:2.15-alpine-3.18"}
1718

18-
### Default Users
19+
# Default Service Names
1920
SPIN_DEFAULT_PHP_SERVICE_NAME=${SPIN_DEFAULT_PHP_RUN_SERVICE:-"php"}
2021

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

2530
# Import common functions
2631
source "$SPIN_HOME/lib/functions.sh"
32+
setup_color
2733

28-
main() {
29-
## Set defaults for our environment
30-
31-
# Select environments to be used
32-
# Allows passing as `ENV` environment variable
33-
# or defaults to `dev`
34-
SPIN_ENV=${SPIN_ENV:-dev}
35-
36-
# Convert the SPIN_ENV variable into an array of environments
37-
IFS=',' read -ra ENV_ARRAY <<< "$SPIN_ENV"
38-
39-
# Initialize the COMPOSE_FILE variable
40-
COMPOSE_FILE="docker-compose.yml"
34+
# Check for upgrades, except for development installs and on `spin update`
35+
if [[ $(detect_installation_type) != "development" && "$1" != "update" ]]; then
36+
check_for_upgrade
37+
fi
4138

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

47-
# Export the COMPOSE_FILE variable
48-
export COMPOSE_FILE
42+
# Validate a few things based on the user's intent
43+
case "$1" in
44+
"" | base64 | debug | help | update | version | --version | -v)
45+
: # Silent output for the user, but we're skipping the compose check too.
46+
;;
47+
init | kill | new | prune | provision | vault)
48+
check_if_docker_is_running
49+
;;
50+
*)
51+
check_if_docker_is_running
52+
check_if_compose_files_exist "$COMPOSE_FILE"
53+
;;
54+
esac
4955

50-
# Check if 'set -x' is enabled
51-
if [[ $- == *x* ]]; then
52-
# If 'set -x' is enabled, echo the COMPOSE_FILE variable
53-
echo "SPIN_ENV: $SPIN_ENV"
54-
echo "COMPOSE_FILE: $COMPOSE_FILE"
55-
fi
56+
# Check that an argument is passed
57+
if [ $# -gt 0 ]; then
58+
spin_action=$1
59+
shift 1 # Remove the action from the arguments
5660

57-
case "$1" in
58-
"" | base64 | debug | help | update | version | --version | -v)
59-
: # Silent output for the user, but we're skipping the compose check too.
60-
;;
61-
init | kill | new | prune | provision | vault)
62-
check_if_docker_is_running
61+
case $spin_action in
62+
base64)
63+
source "$SPIN_HOME/lib/actions/base64.sh"
64+
action_base64 "$@"
65+
;;
66+
build)
67+
source "$SPIN_HOME/lib/actions/build.sh"
68+
action_build "$@"
69+
;;
70+
debug)
71+
source "$SPIN_HOME/lib/actions/debug.sh"
72+
action_debug "$@"
73+
;;
74+
down)
75+
source "$SPIN_HOME/lib/actions/down.sh"
76+
action_down "$@"
77+
;;
78+
exec)
79+
source "$SPIN_HOME/lib/actions/exec.sh"
80+
action_exec "$@"
81+
;;
82+
help)
83+
source "$SPIN_HOME/lib/actions/help.sh"
84+
action_help
85+
;;
86+
init)
87+
source "$SPIN_HOME/lib/actions/init.sh"
88+
action_init "$@"
89+
;;
90+
kill)
91+
source "$SPIN_HOME/lib/actions/kill.sh"
92+
action_kill
93+
;;
94+
latest)
95+
source "$SPIN_HOME/lib/actions/latest.sh"
96+
action_latest "$@"
97+
;;
98+
logs)
99+
source "$SPIN_HOME/lib/actions/logs.sh"
100+
action_logs "$@"
101+
;;
102+
new)
103+
source "$SPIN_HOME/lib/actions/new.sh"
104+
action_new "$@"
105+
;;
106+
php)
107+
source "$SPIN_HOME/lib/actions/php.sh"
108+
action_php "$@"
109+
;;
110+
provision)
111+
source "$SPIN_HOME/lib/actions/provision.sh"
112+
action_provision "$@"
113+
;;
114+
prune)
115+
source "$SPIN_HOME/lib/actions/prune.sh"
116+
action_prune "$@"
117+
;;
118+
pull)
119+
source "$SPIN_HOME/lib/actions/pull.sh"
120+
action_pull "$@"
121+
;;
122+
run)
123+
source "$SPIN_HOME/lib/actions/run.sh"
124+
action_run "$@"
125+
;;
126+
stop)
127+
source "$SPIN_HOME/lib/actions/stop.sh"
128+
action_stop
129+
;;
130+
up)
131+
source "$SPIN_HOME/lib/actions/up.sh"
132+
action_up "$@"
133+
;;
134+
update)
135+
source "$SPIN_HOME/lib/actions/update.sh"
136+
action_update
137+
;;
138+
vault)
139+
source "$SPIN_HOME/lib/actions/vault.sh"
140+
action_vault "$@"
141+
;;
142+
version | --version | -v)
143+
source "$SPIN_HOME/lib/actions/version.sh"
144+
action_version
63145
;;
64146
*)
65-
check_if_docker_is_running
66-
check_if_compose_files_exist "$COMPOSE_FILE"
67-
;;
147+
echo "\"$1\" is not a valid command. Below are the commands available."
148+
source "$SPIN_HOME/lib/actions/help.sh"
149+
action_help
150+
;;
68151
esac
69-
70-
71-
# Set up our structure for our re-used commands
72-
export COMPOSE_CMD=${COMPOSE_CMD:-"docker compose"}
73-
74-
# Check that an argument is passed
75-
if [ $# -gt 0 ]; then
76-
local action=$1
77-
shift 1 # Remove the action from the arguments
78-
79-
case $action in
80-
base64)
81-
source "$SPIN_HOME/lib/actions/base64.sh"
82-
action_base64 "$@"
83-
;;
84-
build)
85-
source "$SPIN_HOME/lib/actions/build.sh"
86-
action_build "$@"
87-
;;
88-
debug)
89-
source "$SPIN_HOME/lib/actions/debug.sh"
90-
action_debug "$@"
91-
;;
92-
down)
93-
source "$SPIN_HOME/lib/actions/down.sh"
94-
action_down "$@"
95-
;;
96-
exec)
97-
source "$SPIN_HOME/lib/actions/exec.sh"
98-
action_exec "$@"
99-
;;
100-
help)
101-
source "$SPIN_HOME/lib/actions/help.sh"
102-
action_help
103-
;;
104-
init)
105-
source "$SPIN_HOME/lib/actions/init.sh"
106-
action_init "$@"
107-
;;
108-
kill)
109-
source "$SPIN_HOME/lib/actions/kill.sh"
110-
action_kill
111-
;;
112-
latest)
113-
source "$SPIN_HOME/lib/actions/latest.sh"
114-
action_latest "$@"
115-
;;
116-
logs)
117-
source "$SPIN_HOME/lib/actions/logs.sh"
118-
action_logs "$@"
119-
;;
120-
new)
121-
source "$SPIN_HOME/lib/actions/new.sh"
122-
action_new "$@"
123-
;;
124-
php)
125-
source "$SPIN_HOME/lib/actions/php.sh"
126-
action_php "$@"
127-
;;
128-
provision)
129-
source "$SPIN_HOME/lib/actions/provision.sh"
130-
action_provision "$@"
131-
;;
132-
prune)
133-
source "$SPIN_HOME/lib/actions/prune.sh"
134-
action_prune "$@"
135-
;;
136-
pull)
137-
source "$SPIN_HOME/lib/actions/pull.sh"
138-
action_pull "$@"
139-
;;
140-
run)
141-
source "$SPIN_HOME/lib/actions/run.sh"
142-
action_run "$@"
143-
;;
144-
stop)
145-
source "$SPIN_HOME/lib/actions/stop.sh"
146-
action_stop
147-
;;
148-
up)
149-
source "$SPIN_HOME/lib/actions/up.sh"
150-
action_up "$@"
151-
;;
152-
update)
153-
source "$SPIN_HOME/lib/actions/update.sh"
154-
action_update
155-
;;
156-
vault)
157-
source "$SPIN_HOME/lib/actions/vault.sh"
158-
action_vault "$@"
159-
;;
160-
version | --version | -v)
161-
source "$SPIN_HOME/lib/actions/version.sh"
162-
action_version
163-
;;
164-
*)
165-
echo "\"$1\" is not a valid command. Below are the commands available."
166-
source "$SPIN_HOME/lib/actions/help.sh"
167-
action_help
168-
;;
169-
esac
170-
else
171-
printf "${BOLD}${YELLOW}🤔 You didn't pass \"spin\" any arguments.${RESET}"
172-
echo
173-
source "$SPIN_HOME/lib/actions/help.sh"
174-
action_help
175-
fi
176-
}
177-
178-
################################################
179-
# 🚀 Where the script actually starts
180-
################################################
181-
182-
setup_color
183-
184-
installation_type=$(detect_installation_type)
185-
if [[ $installation_type != "development" && "$1" != "update" ]]; then
186-
check_for_upgrade
187-
fi
188-
189-
unset installation_type
190-
191-
main "$@" #Passing the original arguments to `main`
152+
else
153+
printf "${BOLD}${YELLOW}🤔 You didn't pass \"spin\" any arguments.${RESET}"
154+
echo
155+
source "$SPIN_HOME/lib/actions/help.sh"
156+
action_help
157+
fi

lib/functions.sh

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
#!/usr/bin/env bash
2+
3+
export_compose_file_variable(){
4+
# Convert the SPIN_ENV variable into an array of environments
5+
IFS=',' read -ra ENV_ARRAY <<< "$SPIN_ENV"
6+
7+
# Initialize the COMPOSE_FILE variable
8+
COMPOSE_FILE="docker-compose.yml"
9+
10+
# Loop over each environment and append the corresponding compose file
11+
for env in "${ENV_ARRAY[@]}"; do
12+
COMPOSE_FILE="$COMPOSE_FILE:docker-compose.$env.yml"
13+
done
14+
15+
# Export the COMPOSE_FILE variable
16+
export COMPOSE_FILE
17+
18+
# Check if 'set -x' is enabled
19+
if [[ $- == *x* ]]; then
20+
# If 'set -x' is enabled, echo the COMPOSE_FILE variable
21+
echo "SPIN_ENV: $SPIN_ENV"
22+
echo "COMPOSE_FILE: $COMPOSE_FILE"
23+
fi
24+
}
25+
226
install_spin_package_to_project() {
327
framework="$1"
428
project_name="$2"

0 commit comments

Comments
 (0)