Features: 1) Added spinner function to enhance user feedback during command execution; 2) Improved cursor visibility control with hide_cursor and show_cursor functions; 3) Refined output messages for improved clarity.

Fixes: 1) Fixed irregular Bash array formatting within SPINNER; 2) Resolved typo in COLOR array definition.

Extra: Simplified docker commands by removing redundant `docker compose` prefix and replaced with shorter aliases; enhanced code readability and formatting.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-16 14:23:09 +03:00
parent d8bc92ced0
commit 9bc982a6f1

View file

@ -1,46 +1,52 @@
#!/usr/bin/env bash
set -euo pipefail
SPINNER=('|' '/' '-' '\\')
COLORS=(97 37 90)
SPINNER=('|' '/' '-' '\')
COLORS=(97 37)
DELAY=0.1
run_with_spinner() {
local cmd="$1"
local msg="$2"
hide_cursor() { tput civis 2>/dev/null || true; }
show_cursor() { tput cnorm 2>/dev/null || true; }
printf "\e[1m%s...\e[0m " "$msg"
eval "$cmd" &> /dev/null &
spinner() {
local pid=$1 msg=$2 i=0 frame color
hide_cursor
while kill -0 "$pid" 2>/dev/null; do
frame="${SPINNER[i % ${#SPINNER[@]}]}"
color="${COLORS[i % ${#COLORS[@]}]}"
printf "\r\e[36m%s... \e[0m\e[%sm%s\e[0m" "$msg" "$color" "$frame"
((i++))
sleep "$DELAY"
done
show_cursor
}
run_with_spinner() {
local args="$1" msg="$2" rc
printf "\e[36m%s... \e[0m" "$msg"
bash -c "docker --ansi never $args" &> /dev/null &
local pid=$!
local i=0
while kill -0 "$pid" 2>/dev/null; do
local frame=${SPINNER[i % ${#SPINNER[@]}]}
local color=${COLORS[i % ${#COLORS[@]}]}
printf "\e[${color}m%s\e[0m" "$frame"
sleep "$DELAY"
printf "\b"
((i++))
done
spinner "$pid" "$msg"
set +e
wait "$pid"
local rc=$?
set -e
if [[ $rc -ne 0 ]]; then
printf "\e[33m(! exit %d)\e[0m " "$rc"
wait "$pid" || rc=$?
rc=${rc:-0}
if [[ $rc -eq 0 ]]; then
printf "\r\e[32m✔\e[0m %s\n" "$msg"
else
printf "\r\e[33m✖ (exit %d)\e[0m %s\n" "$rc" "$msg"
fi
printf "\e[32m✔\e[0m\n"
}
echo
run_with_spinner "docker compose --ansi never down || true" "Stopping services"
run_with_spinner "docker compose --ansi never up -d --build" "Rebuilding & starting services"
run_with_spinner "docker compose --ansi never exec app poetry run python manage.py migrate --no-input" "Applying database migrations"
run_with_spinner "docker compose --ansi never exec app poetry run python manage.py collectstatic --no-input" "Collecting static files"
run_with_spinner "docker compose --ansi never exec app poetry run python manage.py set_default_caches" "Setting default caches"
run_with_spinner "docker system prune -f" "Cleaning up unused Docker data"
echo -e "\e[1mAll done! Your application is up and running.\e[0m"
run_with_spinner "compose down" "Stopping services (down)"
run_with_spinner "compose up -d --build" "Rebuilding & starting services"
run_with_spinner "compose exec app poetry run python manage.py migrate --no-input" "Applying database migrations"
run_with_spinner "compose exec app poetry run python manage.py collectstatic --no-input" "Collecting static files"
run_with_spinner "compose exec app poetry run python manage.py set_default_caches" "Setting default caches"
run_with_spinner "system prune -f" "Cleaning up unused Docker data"
echo -e "\n\e[1mAll done! Your application is up and running.\e[0m"