Created
February 4, 2021 04:52
-
-
Save dinhnv/97064b0684f5b3e29c830e688b0490c6 to your computer and use it in GitHub Desktop.
Helpful shell functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
DC="${DC:-exec}" | |
APP_NAME="${APP_NAME:-hello}" | |
# If we're running in CI we need to disable TTY allocation for docker-compose | |
# commands that enable it by default, such as exec and run. | |
TTY="" | |
if [[ ! -t 1 ]]; then | |
TTY="-T" | |
fi | |
function flask { | |
## Run any Flask commands | |
docker-compose "${DC}" ${TTY} web flask "${@}" | |
} | |
function flake8 { | |
## Lint Python code with flake8 | |
docker-compose "${DC}" ${TTY} web flake8 "${@}" | |
} | |
function pytest { | |
## Run test suite with pytest | |
docker-compose "${DC}" ${TTY} web pytest test/ "${@}" | |
} | |
function pytest-cov { | |
## Get test coverage with pytest-cov | |
docker-compose "${DC}" ${TTY} web pytest \ | |
--cov test/ --cov-report term-missing "${@}" | |
} | |
function bash { | |
## Start a Bash session in the web container | |
docker-compose "${DC}" ${TTY} web bash "${@}" | |
} | |
function psql { | |
## Connect to PostgreSQL with psql | |
docker-compose "${DC}" ${TTY} postgres psql -U "${APP_NAME}" "${@}" | |
} | |
function redis-cli { | |
## Connect to Redis with redis-cli | |
docker-compose "${DC}" ${TTY} redis redis-cli "${@}" | |
} | |
function pip3:install { | |
## Install pip3 dependencies and write lock file | |
docker-compose build | |
docker-compose run ${TTY} web bin/pip3-install | |
docker-compose down | |
} | |
function yarn:install { | |
## Install yarn dependencies and write lock file | |
docker-compose build | |
docker-compose run ${TTY} webpack yarn install | |
docker-compose down | |
} | |
function clean { | |
## Remove cache and other machine generates files | |
rm -rf .pytest_cache/ .webpack_cache/ public/* .coverage celerybeat-schedule | |
touch public/.keep | |
} | |
function ci:install-deps { | |
# Install Continuous Integration (CI) dependencies | |
sudo apt-get install -y curl shellcheck | |
sudo curl \ | |
-L https://raw.githubusercontent.com/nickjj/wait-until/v0.1.2/wait-until \ | |
-o /usr/local/bin/wait-until && sudo chmod +x /usr/local/bin/wait-until | |
} | |
function ci:test { | |
# Execute Continuous Integration (CI) pipeline | |
# | |
# It's expected that your CI environment has these tools available: | |
# - https://github.com/koalaman/shellcheck | |
# - https://github.com/nickjj/wait-until | |
shellcheck run bin/* | |
cp --no-clobber .env.example .env | |
cp --no-clobber docker-compose.override.yml.example docker-compose.override.yml | |
docker-compose build | |
docker-compose up -d | |
# shellcheck disable=SC1091 | |
. .env | |
wait-until "docker-compose exec -T \ | |
-e PGPASSWORD=${POSTGRES_PASSWORD} postgres \ | |
psql -U ${POSTGRES_USER} ${POSTGRES_USER} -c 'SELECT 1'" | |
flake8 "${@}" | |
flask db reset --with-testdb | |
pytest "${@}" | |
} | |
function help { | |
printf "%s <task> [args]\n\nTasks:\n" "${0}" | |
compgen -A function | cat -n | |
printf "\nExtended help:\n Each task has comments for general usage\n" | |
} | |
# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile | |
TIMEFORMAT=$'\nTask completed in %3lR' | |
time "${@:-help}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment