Skip to content

Commit

Permalink
[5.x] Improve debug-echo
Browse files Browse the repository at this point in the history
  • Loading branch information
shinsenter committed Aug 28, 2024
1 parent 61d8463 commit dc99d21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/php/common/docker-php-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Author: SHIN Company <[email protected]>
# License: https://code.shin.company/php/blob/main/LICENSE
################################################################################
if [ "$(id -u)" != "0" ]; then echo "Please run this container as the root user." >&2; exit 1; fi
if [ "$(id -u)" != "0" ]; then debug-echo -e "!!! Please run this container as the root user !!!" >&2; exit 1; fi

APP_PATH="$(app-path)"

Expand All @@ -33,7 +33,10 @@ $ENV_CHECK
" >> ~/.bashrc; fi

# export all env variables for PHP
if is-true $ALLOW_RUNTIME_PHP_ENVVARS && has-cmd php-envvars; then php-envvars export_envvars 2>&1; fi
if is-true $ALLOW_RUNTIME_PHP_ENVVARS && has-cmd php && has-cmd php-envvars; then
debug-echo "Export all env variables for PHP"
php-envvars export_envvars 2>&1
fi

# change working directory
if [ -d "$APP_PATH" ]; then cd "$APP_PATH"; fi
Expand All @@ -43,6 +46,7 @@ if [ ! -z "$SUPERVISOR_PHP_COMMAND" ]; then
hook bootstrap

# run custom command defined in $SUPERVISOR_PHP_COMMAND
debug-echo "Start container with the command: $SUPERVISOR_PHP_COMMAND $@"
exec with-env $SUPERVISOR_PHP_COMMAND "$@"
else
# run bootstrap hook to initialize the container
Expand All @@ -55,6 +59,6 @@ else
if [ $# -eq 0 ]; then set -- /bin/sh; fi

# start container with command
debug-echo "Start container with command $@"
debug-echo "Start container with the command: $@"
exec with-env "$@"
fi
2 changes: 1 addition & 1 deletion src/php/common/rootfs/etc/hooks/bootstrap/50-crontabs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fi)
CRONTAB

chmod 0600 $CRONTAB
debug-echo -i "Cron is running as $APP_USER.\n\n### Config:\n$(crontab -u $APP_USER -l 2>/dev/null)"
debug-echo -i "\nCron is running as $APP_USER.\n\n### Config:\n$(crontab -u $APP_USER -l 2>/dev/null)\n"

if ! has-s6 && has-cmd crond; then
export APP_PATH="$(app-path)"
Expand Down
34 changes: 19 additions & 15 deletions src/php/common/rootfs/usr/local/bin/debug-echo
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#!/usr/bin/env sh
# colors
NC='\033[0m' # no color

NC='\033[0m'
case "$1" in
-e|--error) CL='\033[0;31m'; shift ;;
-s|--success) CL='\033[0;32m'; shift ;;
-w|--warn) CL='\033[0;33m'; shift ;;
-i|--info) CL='\033[0;34m'; shift ;;
-l|--log) CL='\033[0;90m'; shift ;;
*) if ! is-debug; then exit 0; else CL='\033[0;90m'; fi ;;
-c|--critical) CL='\033[0;31m'; shift ;;
-e|--error) CL='\033[0;31m'; shift ;;
-s|--success) CL='\033[0;32m'; shift ;;
-w|--warn) CL='\033[0;33m'; shift ;;
-i|--info) CL='\033[0;34m'; shift ;;
-l|--log) CL='\033[0;90m'; shift ;;
*) CL='\033[0;90m'; if ! is-debug; then exit 0; fi ;;
esac

if [ ! -t 1 ]; then
NC=
CL=
fi

colorize() {
if [ -t 1 ] && [ ! -z "$CL" ]; then
if [ -n "$@" ]; then
echo -e "${CL}$@${NC}"
else
echo -e "$@"
fi
}

if [ -t 0 ]; then
colorize "$@" >&2
else
while read line; do
colorize "$@"

if [ ! -t 0 ]; then
while IFS= read -r line; do
colorize "$line"
done
fi

0 comments on commit dc99d21

Please sign in to comment.