initial commit
This commit is contained in:
192
bin/helpers/func.sh
Executable file
192
bin/helpers/func.sh
Executable file
@@ -0,0 +1,192 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
#Color print function, usage: println "message" "color"
|
||||
println() {
|
||||
color=$2
|
||||
printfe "%s\n" $color "$1"
|
||||
}
|
||||
|
||||
# print colored with printf (args: format, color, message ...)
|
||||
printfe() {
|
||||
format=$1
|
||||
color=$2
|
||||
message=$3
|
||||
show_time=true
|
||||
|
||||
# Check if $4 is explicitly set to false, otherwise default to true
|
||||
if [ ! -z "$4" ] && [ "$4" == "false" ]; then
|
||||
show_time=false
|
||||
fi
|
||||
|
||||
red=$(tput setaf 1)
|
||||
green=$(tput setaf 2)
|
||||
yellow=$(tput setaf 3)
|
||||
blue=$(tput setaf 4)
|
||||
magenta=$(tput setaf 5)
|
||||
cyan=$(tput setaf 6)
|
||||
normal=$(tput sgr0)
|
||||
grey=$(tput setaf 8)
|
||||
|
||||
case $color in
|
||||
"red")
|
||||
color=$red
|
||||
;;
|
||||
"green")
|
||||
color=$green
|
||||
;;
|
||||
"yellow")
|
||||
color=$yellow
|
||||
;;
|
||||
"blue")
|
||||
color=$blue
|
||||
;;
|
||||
"magenta")
|
||||
color=$magenta
|
||||
;;
|
||||
"cyan")
|
||||
color=$cyan
|
||||
;;
|
||||
"grey")
|
||||
color=$grey
|
||||
;;
|
||||
*)
|
||||
color=$normal
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ "$show_time" == "false" ]; then
|
||||
printf "$color$format$normal" "$message"
|
||||
return
|
||||
fi
|
||||
|
||||
printf $grey"%s" "$(date +'%H:%M:%S')"$normal
|
||||
|
||||
case $color in
|
||||
$green | $cyan | $blue | $magenta | $normal)
|
||||
printf "$green INF $normal"
|
||||
;;
|
||||
$yellow)
|
||||
printf "$yellow WRN $normal"
|
||||
;;
|
||||
$red)
|
||||
printf "$red ERR $normal"
|
||||
;;
|
||||
*)
|
||||
printf "$normal"
|
||||
;;
|
||||
esac
|
||||
printf "$color$format$normal" "$message"
|
||||
}
|
||||
|
||||
# Print and run a command in yellow
|
||||
log_and_run() {
|
||||
printfe "%s\n" "yellow" "$*"
|
||||
eval "$@"
|
||||
}
|
||||
|
||||
run_docker_command() {
|
||||
cmd=$1
|
||||
log_level="$2"
|
||||
shift
|
||||
shift
|
||||
params=$@
|
||||
composer_image="composer/composer:2.7.8"
|
||||
php_image="php:8.3-cli-alpine3.20"
|
||||
phpstan_image="atishoo/phpstan:latest"
|
||||
AUTH_SOCK_DIRNAME=$(dirname $SSH_AUTH_SOCK)
|
||||
|
||||
# It's possible $SSH_AUTH_SOCK is not set, in that case we should set it to /tmp/ssh_auth_sock
|
||||
if [ -z "$SSH_AUTH_SOCK" ]; then
|
||||
AUTH_SOCK_DIRNAME="/tmp/ssh_auth_sock:/tmp/ssh_auth_sock"
|
||||
fi
|
||||
|
||||
# Take the name of the current directory
|
||||
container=$(basename $(pwd) | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
# Check if the $container is an actual container from the $TRADAWARE_PATH/docker-compose.yml
|
||||
result=$(docker compose -f $TRADAWARE_PATH/docker-compose.yml ps -q $container 2>/dev/null)
|
||||
if [ -z "$result" ]; then
|
||||
# Ensure /home/$USER/.config/composer/auth.json exists, if not prefill it with an empty JSON object
|
||||
if [ ! -f /home/$USER/.config/composer/auth.json ]; then
|
||||
mkdir -p /home/$USER/.config/composer
|
||||
touch /home/$USER/.config/composer/auth.json
|
||||
echo "{
|
||||
\"github-oauth\": {
|
||||
\"github.com\": \"KEY_HERE\"
|
||||
}
|
||||
}" > /home/$USER/.config/composer/auth.json
|
||||
printfe "%s" "yellow" "Created an empty auth.json file at '"
|
||||
printfe "%s" "cyan" "/home/$USER/.config/composer/auth.json"
|
||||
printfe "%s\n" "yellow" "', you should edit this file and add your GitHub OAuth key."
|
||||
return
|
||||
fi
|
||||
|
||||
# In case cmd is composer run it with composer image
|
||||
if [ "$cmd" == "composer" ]; then
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$composer_image'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
|
||||
docker run --rm --interactive --tty \
|
||||
--volume $PWD:/app \
|
||||
--volume $AUTH_SOCK_DIRNAME \
|
||||
--volume /etc/passwd:/etc/passwd:ro \
|
||||
--volume /etc/group:/etc/group:ro \
|
||||
--volume /home/$USER/.ssh:/root/.ssh \
|
||||
--volume /home/$USER/.config/composer/auth.json:/tmp/auth.json \
|
||||
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
|
||||
--user $(id -u):$(id -g) \
|
||||
$composer_image $cmd $params
|
||||
elif [ "$cmd" == "php" ]; then
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$php_image'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
|
||||
docker run --rm --interactive --tty \
|
||||
--volume $PWD:/app \
|
||||
--volume $AUTH_SOCK_DIRNAME \
|
||||
--volume /etc/passwd:/etc/passwd:ro \
|
||||
--volume /etc/group:/etc/group:ro \
|
||||
--volume /home/$USER/.ssh:/root/.ssh \
|
||||
--volume /home/$USER/.config/composer/auth.json:/tmp/auth.json \
|
||||
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
|
||||
--user $(id -u):$(id -g) \
|
||||
$php_image $cmd $params
|
||||
elif [ "$cmd" == "phpstan" ]; then
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$phpstan_image'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
|
||||
docker run --rm --interactive --tty \
|
||||
--volume $PWD:/app \
|
||||
--user $(id -u):$(id -g) \
|
||||
$phpstan_image $params
|
||||
else
|
||||
println "No container found named $container and given command is not composer or php." "red"
|
||||
fi
|
||||
return
|
||||
fi
|
||||
|
||||
docker_user=docker
|
||||
|
||||
if [ "$log_level" == "0" ] || [ "$log_level" == "-1" ]; then
|
||||
printfe "%s" "cyan" "Running '"
|
||||
printfe "%s" "yellow" "$cmd $params"
|
||||
printfe "%s" "cyan" "' in "
|
||||
printfe "%s" "yellow" "'$container'"
|
||||
printfe "%s\n" "cyan" " container..."
|
||||
fi
|
||||
docker compose -f $TRADAWARE_PATH/docker-compose.yml exec -u $docker_user --interactive --tty $container $cmd $params
|
||||
}
|
Reference in New Issue
Block a user