Created
January 7, 2019 20:25
-
-
Save Deifinger/5c491d2e91973253e30d19403cd8f14f to your computer and use it in GitHub Desktop.
Project shell helper
This file contains hidden or 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 | |
# Written with best practices: | |
# - https://www.davidpashley.com/articles/writing-robust-shell-scripts/ | |
# - https://github.com/progrium/bashstyle | |
[[ "$TRACE" ]] && set -o xtrace # For debugging. Print command traces before executing command. | |
set -o errexit # Tells bash that it should exit the script if any statement returns a non-true return value | |
set -o nounset # Exit script if you try to use an uninitialised variable | |
set -o pipefail # false | true will be considered to have fail | |
set -o noclobber # IO redirection | |
alias errcho=">&2 echo" | |
readonly SCRIPT_NAME="Bash Manager" | |
readonly INIT_DIR=$(pwd) | |
readonly LARADOCK_REPO="[email protected]:laradock/laradock.git" | |
readonly LARADOCK_DIR=${INIT_DIR}/laradock | |
readonly LARADOCK_ENV=${LARADOCK_DIR}/.env | |
readonly LARADOCK_ENV_EXAMPLE_FILENAME=".env-laradock" | |
readonly SRC_DIR=${INIT_DIR}/src | |
readonly LARAVEL_ENV_EXAMPLE_FILENAME=".env.example" | |
readonly LARAVEL_ENV_EXAMPLE=${SRC_DIR}/${LARAVEL_ENV_EXAMPLE_FILENAME} | |
readonly LARAVEL_ENV=${SRC_DIR}/.env | |
readonly DOCKER_SERVICES="nginx php-fpm postgres redis workspace" | |
readonly WORKSPACE_SERVICE_NAME="workspace" | |
readonly REDIS_SERVICE_NAME="redis" | |
readonly POSTGRES_SERVICE_NAME="postgres" | |
readonly POSTGRES_USER=$(cat ${LARADOCK_ENV} | grep -m1 POSTGRES_USER | cut -d'=' -f2) | |
readonly POSTGRES_DB=$(cat ${LARADOCK_ENV} | grep -m1 POSTGRES_DB | cut -d'=' -f2) | |
# Init in parse_args function | |
declare -A PARSED_ARGS | |
declare HELP_TEXT=" | |
Usage: ./$(basename "$0") command [OPTION]... | |
init [OPTION]... If you will run it without options, all of options will be launched | |
OPTIONS: | |
-ld Downloads laradock | |
-dc Setup docker-compose services | |
-ll Makes Laravel initial commands (copy .env from .env.example, key:generate, migrate) | |
-p Runs passport:install | |
artisan | art [OPTION]... Launches artisan inside workspace docker container | |
OPTIONS: Use any option or command for artisan | |
postgres | psql Connect to Postgres docker container | |
redis | rds Connect to Redis docker container | |
dc | docker [OPTION]... Runs docker-compose in laradock directory | |
OPTIONS: Use any option or command for docker-compose | |
OR use next predefined options for most use-cases | |
up up -d $DOCKER_SERVICES | |
clear rm -s | |
-h | --help Usage tips | |
" | |
usage() { | |
printf "You are using $SCRIPT_NAME\n $HELP_TEXT" | |
} | |
parse_args() { | |
for i in "$@" | |
do | |
PARSED_ARGS["${i//-}"]=true; | |
shift; | |
done | |
} | |
print_parsed_args() { | |
for i in "${!PARSED_ARGS[@]}" | |
do | |
echo "key : $i" | |
echo "value: ${PARSED_ARGS[$i]}" | |
done | |
} | |
workspace() { | |
local args="$1" | |
cd ${LARADOCK_DIR} | |
docker_compose exec ${WORKSPACE_SERVICE_NAME} ${args} | |
} | |
artisan() { | |
local args="$1" | |
workspace "php artisan ${args}" | |
} | |
docker_compose() { | |
local f_arg="$1" | |
local args="$@" | |
case "$f_arg" in | |
up ) args="up -d $DOCKER_SERVICES" ;; | |
clear ) args="rm -s" ;; | |
esac | |
cd ${LARADOCK_DIR} | |
docker-compose ${args} | |
} | |
postgresql() { | |
docker_compose exec ${POSTGRES_SERVICE_NAME} psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} | |
} | |
redis() { | |
docker_compose exec ${REDIS_SERVICE_NAME} redis-cli | |
} | |
init() { | |
local all=false | |
# if no args | |
[[ -v PARSED_ARGS[@] ]] || all=true | |
# all or -ld enabled | |
(${all} || [[ -v "PARSED_ARGS[ld]" ]]) && init_laradock | |
(${all} || [[ -v "PARSED_ARGS[dc]" ]]) && docker_compose up | |
(${all} || [[ -v "PARSED_ARGS[ll]" ]]) && init_laravel | |
(${all} || [[ -v "PARSED_ARGS[p]" ]]) && init_artisan_passport | |
} | |
init_laradock() { | |
git clone ${LARADOCK_REPO} ${LARADOCK_DIR} | |
cp ${LARADOCK_ENV_EXAMPLE_FILENAME} ${LARADOCK_ENV} | |
} | |
init_laravel() { | |
cp ${LARAVEL_ENV_EXAMPLE} ${LARAVEL_ENV} | |
docker_compose exec --user=laradock ${WORKSPACE_SERVICE_NAME} "composer install" | |
artisan key:generate | |
artisan migrate | |
} | |
init_artisan_passport() { | |
artisan passport:install | |
} | |
main() { | |
local rule | |
if [[ $# -eq 0 ]]; then | |
rule="" | |
else | |
rule="$1" | |
shift | |
fi | |
local args="$@" | |
parse_args ${args} | |
case "$rule" in | |
init ) init; exit ;; | |
workspace | ws ) workspace "$args"; exit ;; | |
artisan | art ) artisan "$args"; exit ;; | |
postgres | psql ) postgresql; exit ;; | |
redis | rds ) redis; exit ;; | |
dc | docker ) docker_compose "$args"; exit ;; | |
-h | --help ) usage; exit ;; | |
* ) usage; exit 1 ;; | |
esac | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment