Skip to content

Instantly share code, notes, and snippets.

@JoaoSetas
Last active June 28, 2019 08:58
Show Gist options
  • Save JoaoSetas/ee123819765e2f248e76aaf9d997c246 to your computer and use it in GitHub Desktop.
Save JoaoSetas/ee123819765e2f248e76aaf9d997c246 to your computer and use it in GitHub Desktop.
Alias for bash
DOCKER_PATH='~/Documents/docker'
PROJECTS_PATH='/c/Users/Setas/Documents/Projects'
#Relative path to dump folder inside the project path. Not the full path
DUMP_PATH="db_dump"
CONTAINER_WEB='docker_php7_1'
CONTAINER_MYSQL='docker_db_1'
MYSQL_USER='root'
MYSQL_PASS='123123'
EDITOR_ALIAS='code'
RED='\e[1;31m'
GRN='\e[1;32m'
YWL='\e[1;33m'
BLUE='\e[1;36m'
MAG='\e[1;35m'
END='\e[0m'
alias up="cd $DOCKER_PATH && docker-compose up -d; cd - > /dev/null && docker image prune -a"
function down(){
if [ "$1" = "-h" ]; then
echo && echo " down [?container]"
return
fi
local ALL=$(docker ps -a -q)
docker stop ${1:-$ALL} && docker rm ${1:-$ALL}
}
function _chmod(){
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " _chmod [project]"
return
fi
cd $PROJECTS_PATH/$1
chmod -R 777 .
cd - > /dev/null
}
function dexec() {
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " dexec [container =web, =mysql] [command]"
return
fi
case $1 in
web)
CONTAINER=$CONTAINER_WEB;;
mysql)
CONTAINER=$CONTAINER_MYSQL;;
*)
CONTAINER=$1;;
esac
winpty docker exec -it $CONTAINER "${@:2}"
}
function web(){
if [ "$1" = "-h" ]; then
echo && echo " web [project] [?command =tail, =migrate, =repair, =preload [?options], =update [?options], =migration [app] [JIRA ISSUE] [DESCRIPTION] [AUTHOR]]"
return
fi
case $2 in
"")
dexec web bash -c "cd ${1:-/var/www/html/} && bash";;
update)
dexec web bash -c "cd $1 && beevo update-project ${*:3}";;
migrate)
dexec web bash -c "cd $1 && beevo run-migrations migrate";;
repair)
dexec web bash -c "cd $1 && beevo run-migrations repair";;
versions)
dexec web bash -c "cd $1 && beevo update-versions";;
preload)
dexec web bash -c "cd $1 && rm -rf beevo/cache beevo/preload && beevo preload ${*:3}";;
tail)
dexec web bash -c "cd $1 && tail -f logs/beevo.log";;
migration)
local STEP1=$(curl -s http://api.manager.beevo.com/v1/installables/$3/versions | grep -Po '"version":.*?[^\\]",' | tail -1)
local STEP2=${STEP1#*\"version\":\"v}
local STEP3=${STEP2%\",}
local VERSION=${STEP3//.}
STEP1=$(curl -s http://api.manager.beevo.com/v1/installables/$3 | grep -Po '"installableId":.*?[^\\]",')
STEP2=${STEP1#*\"installableId\":\"}
local APP_NUMBER=${STEP2%\",}
printf "\nAutomatically Added\n${GRN}App Number:$END $APP_NUMBER"
printf "\n${GRN}Version:$END $VERSION\n\n"
dexec web bash -c "cd $1/apps/$3/Install && beevo generate-migration $APP_NUMBER $VERSION ${*:4}"
local MIGRATION=$(ls -t $PROJECTS_PATH/$1/apps/$3/Install | head -1)
if ! [ -z "$4" ]; then
open $1 $3/Install/$MIGRATION
fi
;;
*)
dexec web bash -c "cd $1 && ${*:2}";;
esac
}
function pv(){
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " pv [dump] [DB] [?username] [?password]"
return
fi
while read p || [ -n "$p" ]; do
local p=$(echo "$p" | tr -d '\r')
local TABLES+="| sed '/$p/d'"
done <$PROJECTS_PATH/$DUMP_PATH/.tables_to_skip
printf "\n${GRN}Reducing dump...\n$END"
dexec mysql bash -c "pv $DUMP_PATH/$1 $TABLES > $DUMP_PATH/reduced_$1"
printf "\n${GRN}Importing reduced dump...\n$END"
dexec mysql bash -c "mysql -u${3:-$MYSQL_USER} -p${4:-$MYSQL_PASS} -e 'CREATE DATABASE IF NOT EXISTS $2;'"
dexec mysql bash -c "pv $DUMP_PATH/reduced_$1 | mysql -f -u${3:-$MYSQL_USER} -p${4:-$MYSQL_PASS} $2"
while [ "$CONFIRMATION" != "y" ]; do
printf "\n${YWL}Do you want to remove the old dump? [y/n]: $END"
read CONFIRMATION
if [ "$CONFIRMATION" = "n" ]; then
return
fi
done
rm $PROJECTS_PATH/$DUMP_PATH/$1
}
function docs(){
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " docs [project] [app] [?version]"
return
fi
dexec web bash -c "cd $1/apps/$2 && beevo docs-compare --namespace $2 --version ${3:-\$(git describe --abbrev=0 --match 'v[0-9]*')} --print"
}
function clone(){
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " clone [repo name] [project]"
return
fi
if [ "$1" = "Core" ] || [ "$1" = "core" ]; then
cd "$PROJECTS_PATH/$2" && \
rm -rf beevo && \
git clone git@bitbucket.org:bsolus_daredevil/$1.git beevo
cd - > /dev/null
return
fi
cd "$PROJECTS_PATH/$2/apps" && \
rm -rf $1 && \
git clone git@bitbucket.org:bsolus_daredevil/$1.git $1
cd - > /dev/null
}
function open(){
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " open [project] [?app]"
return
fi
if [ -z "$2" ]; then
$EDITOR_ALIAS $PROJECTS_PATH/$1
return
fi
if [ "$2" = "Core" ] || [ "$2" = "core" ]; then
$EDITOR_ALIAS $PROJECTS_PATH/$1/beevo
return
fi
$EDITOR_ALIAS $PROJECTS_PATH/$1/apps/$2
}
function tag(){
if [ "$1" = "-h" ] || [ -z "$1" ]; then
echo && echo " tag [app] [project] [version]"
return
fi
#Validate docs
docs $2 $1
printf "\n${END}${YWL}Check for errors inside the doc array ↑${END}\n"
local CONFIRMATION="0"
while [ "$CONFIRMATION" != "y" ]; do
printf "\n${GRN}Do you want to tag this app? [y/n]: $END"
read CONFIRMATION
if [ "$CONFIRMATION" = "n" ]; then
return
fi
done
echo && echo "---------------------------------------------------------------------------"
printf "\n${GRN}Pushing Tag to git... $END\n\n"
#Tag git
cd $PROJECTS_PATH/$2/apps/$1
COMIT=$(git ls-remote --heads https://bitbucket.org/bsolus_daredevil/$1 | grep -m1 'refs/heads/production' | grep -o '^\S*')
git fetch --all
git tag $3 $COMIT
RESULT=$(git push --porcelain origin $3)
cd - > /dev/null
echo $RESULT
echo $RESULT | grep -v "\[new tag\]\|\[up to date\]" && printf "${RED}\nERROR taging git. Please try again${END}\n" && return
echo && echo "---------------------------------------------------------------------------"
printf "\n${GRN}Pushing Tag to beevo... $END\n\n"
#Tag beevo
echo && curl -s -X POST -F "version=$3" http://api.manager.beevo.com/v1/installables/$1/versions/ && echo && \
curl -s -X POST http://api.manager.beevo.com/v1/installables/$1/versions/$3/checkdependencies
printf "\n\nLoading "
while : ; do
sleep 1
local STEP1=$(curl -s http://api.manager.beevo.com/v1/installables/$1/versions/$3 | grep -Po '"lastMessage":.*?[^\\]",')
local STEP2=${STEP1#*\"lastMessage\":\"}
local STEP3=${STEP2%\",}
printf "."
[ "${STEP3:0:1}" = "\"" ] || break
done
printf "\n\nResponse: $BLUE$STEP3$END\n"
}
alias info='printf "\n up\n" && down -h && _chmod && dexec && web -h && pv && docs && clone && open && tag'
_downComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "$(docker ps -a --format "{{.Names}}")" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_chmodComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH | sed 's#/##')" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_dexecComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "web mysql" -- $cur) )
COMPREPLY+=( $(compgen -W "$(docker ps -a --format "{{.Names}}")" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "[command]" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_webComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH | sed 's#/##')" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "tail update migrate repair versions preload migration" -- $cur) )
;;
3)
case ${prev} in
preload|update)
COMPREPLY=($(compgen -W "[?options]" -- ${cur}))
;;
migration)
COMPREPLY=($(compgen -W "$(ls $PROJECTS_PATH/${COMP_WORDS[1]}/apps | sed 's#/##')" -- ${cur}))
;;
esac
;;
4)
case ${COMP_WORDS[COMP_CWORD-2]} in
migration)
COMPREPLY=($(compgen -W "[JIRA_ISSUE][DESCRIPTION][AUTHOR]" -- ${cur}))
;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
_pvComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH/$DUMP_PATH | sed 's#/##')" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "[DB][?username][?password]" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_docsComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH | sed 's#/##')" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH/$prev/apps | sed 's#/##')" -- $cur) )
;;
3)
COMPREPLY=( $(compgen -W "[?version]" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_cloneComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
case ${COMP_CWORD} in
1)
local NAMESPACE=$(curl -s http://api.manager.beevo.com/v1/installables | grep -Po '"namespace":.*?[^\\]",' | sed 's/\"namespace\":\"//g' | sed 's/\",//g' )
COMPREPLY=( $(compgen -W "$NAMESPACE" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH)" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_openComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH | sed 's#/##')" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH/$prev/apps | sed 's#/##')" -- $cur) )
COMPREPLY+=( $(compgen -W "Core core" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
_tagComplete()
{
local cur=${COMP_WORDS[COMP_CWORD]}
case ${COMP_CWORD} in
1)
local NAMESPACE=$(curl -s http://api.manager.beevo.com/v1/installables | grep -Po '"namespace":.*?[^\\]",' | sed 's/\"namespace\":\"//g' | sed 's/\",//g' )
COMPREPLY=( $(compgen -W "$NAMESPACE" -- $cur) )
;;
2)
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_PATH | sed 's#/##')" -- $cur))
;;
3)
local STEP1=$(curl -s "http://api.manager.beevo.com/v1/installables/${COMP_WORDS[1]}/versions?o=(version)&f=(version,n_errors)&q=(status=1)" | grep -Po '"version":.*?[^\\]",' | tail -1)
local STEP2=${STEP1#*\"version\":\"}
local VERSION=${STEP2%\",}
COMPREPLY=( $(compgen -W "$VERSION" -- $cur) )
;;
*)
COMPREPLY=()
;;
esac
}
complete -F _downComplete down
complete -F _chmodComplete _chmod
complete -F _dexecComplete dexec
complete -F _webComplete web
complete -F _pvComplete pv
complete -F _docsComplete docs
complete -F _cloneComplete clone
complete -F _openComplete open
complete -F _tagComplete tag
@MEGuim
Copy link

MEGuim commented Jun 7, 2019

Para quem tem zsh e quer completion, adicionar estas linhas ao ficheiro .zshrc:
autoload bashcompinit
bashcompinit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment