Skip to content

Instantly share code, notes, and snippets.

@Neirda24
Last active November 16, 2022 12:17
Show Gist options
  • Save Neirda24/32c400034a355edfa0c860af62edd7f4 to your computer and use it in GitHub Desktop.
Save Neirda24/32c400034a355edfa0c860af62edd7f4 to your computer and use it in GitHub Desktop.
Seamless docker integration
#!/usr/bin/env bash
## ---------------
## Setup
## ---------------
CURRENT_BASH=$(ps -p $$ | tail -n 1 | awk '{ print $4 }' | perl -pe 's#.*[-/](\w)#$1#');
case "${CURRENT_BASH}" in
zsh)
CURRENT_DIR=$(cd "$(dirname "${0}")" && pwd);
;;
bash)
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd);
;;
*)
echo 1>&2;
echo -e "\033[0;31m\`${CURRENT_BASH}\` does not seems to be supported\033[0m" 1>&2;
echo 1>&2;
return 1;
;;
esac
source "${CURRENT_DIR}/.env";
unalias __getSubPath 2>/dev/null >/dev/null || true;
__getSubPath() {
local absolute_subpath="${PWD}/";
local subpath="";
if [[ "${absolute_subpath}" == "${APP_PROJECT_PATH}/"* ]]; then
subpath=${absolute_subpath#"${APP_PROJECT_PATH}/"};
fi
echo "${subpath}";
}
unalias __docker_exec 2>/dev/null >/dev/null || true;
__docker_exec() {
local subpath;
subpath=$(__getSubPath);
docker exec -it -w "/var/www/html/${subpath}" "$@";
}
export -f __docker_exec;
# 1. TODO: auto source / unsource when entering / exiting repository
# 2. TODO: autocomplete from containers
## ---------------
## Infra / Utils
## ---------------
unalias unsource 2>/dev/null >/dev/null || true;
unsource() {
exec "${CURRENT_BASH}";
}
export -f unsource;
## ---------------
## PHP & Tools
## ---------------
unalias php 2>/dev/null >/dev/null || true;
php() {
__docker_exec myapp_php bash -c "php -d memory_limit=-1 $*";
}
export -f php;
unalias composer 2>/dev/null >/dev/null || true;
composer() {
__docker_exec myapp_phptools bash -c "COMPOSER_MEMORY_LIMIT=-1 php -d memory_limit=-1 \$(which composer) $*";
}
export -f composer;
unalias phpunit 2>/dev/null >/dev/null || true;
phpunit() {
__docker_exec myapp_phptools bash -c "./vendor/bin/phpunit $*";
}
export -f phpunit;
unalias infection 2>/dev/null >/dev/null || true;
infection() {
__docker_exec myapp_phptools bash -c "php -d memory_limit=-1 \$(which infection) $*";
}
export -f infection;
unalias deptrac 2>/dev/null >/dev/null || true;
deptrac() {
__docker_exec myapp_phptools bash -c "deptrac $*";
}
export -f deptrac;
unalias phpstan 2>/dev/null >/dev/null || true;
phpstan() {
__docker_exec myapp_phptools bash -c "./vendor/bin/phpstan $*";
}
export -f phpstan;
unalias rector 2>/dev/null >/dev/null || true;
rector() {
__docker_exec myapp_phptools bash -c "./vendor/bin/rector $*";
}
export -f rector;
## ---------------
## Symfony
## ---------------
unalias sf-check 2>/dev/null >/dev/null || true;
sf-check() {
php vendor/bin/requirements-checker;
}
export -f sf-check;
unalias console 2>/dev/null >/dev/null || true;
console() {
php bin/console "$@";
}
export -f console;
unalias dev 2>/dev/null >/dev/null || true;
dev() {
console --env=dev "$@";
}
export -f dev;
unalias prod 2>/dev/null >/dev/null || true;
prod() {
console --env=prod "$@";
}
export -f prod;
unalias test 2>/dev/null >/dev/null || true;
test() {
console --env=test "$@";
}
export -f test;
## ---------------
## PostgresSQL
## ---------------
unalias pgsql 2>/dev/null >/dev/null || true
pgsql() {
source "${CURRENT_DIR}/postgres.env"
docker exec -ti myapp_pgsql sh -c "psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} $*"
}
export -f pgsql
## ---------------
## Blackfire
## ---------------
unalias blackfire 2>/dev/null >/dev/null || true;
blackfire() {
docker exec -ti myapp_blackfire sh -c "blackfire $*";
}
export -f blackfire;
## ---------------
## Node
## ---------------
unalias node 2>/dev/null >/dev/null || true;
node() {
__docker_exec myapp_node bash -c "node $*";
}
export -f node;
unalias npm 2>/dev/null >/dev/null || true;
npm() {
__docker_exec myapp_node bash -c "npm $*";
}
export -f npm;
unalias npx 2>/dev/null >/dev/null || true;
npx() {
__docker_exec myapp_node bash -c "npx $*";
}
export -f npx;
unalias yarn 2>/dev/null >/dev/null || true;
yarn() {
__docker_exec myapp_node bash -c "yarn $*";
}
export -f yarn;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment