Skip to content

Instantly share code, notes, and snippets.

@thehowl
Last active May 7, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thehowl/252c913495f217139c3f264761169e80 to your computer and use it in GitHub Desktop.
Save thehowl/252c913495f217139c3f264761169e80 to your computer and use it in GitHub Desktop.
probably the only good build tool
#!/bin/bash
# provides documentation for the sub-commands.
DOCUMENTATION=(
'help [COMMAND]'
"returns help for the specified command, or for all commands if none is specified.
$ ./tool help
$ ./tool help up"
'dev [COMMAND]'
"shortcut to call docker-compose, using the development docker compose files.
if passed without arguments, it will start in development mode
building only the backend services first.
$ ./tool dev
$ ./tool dev stop
$ ./tool dev hot-reload payment # custom addition: rebuilds the service and starts it"
'prod <COMMAND>'
"shortcut to call docker-compose, using the production docker compose files.
$ ./tool prod up --build
$ ./tool prod stop"
'db [PATH_TO_SQLFILE]'
'if called without arguments, will start a connection to the database.
if an argument is passed, it will be loaded as a sql file into the db.
to load from stdin, use /dev/stdin. psql must be installed.
$ ./tool db
$ ./tool db migrate/fixtures/00_product.sql'
)
# absolute path to script
tool_root="$(dirname "$(realpath "$0")")"
tool() {
cmd="$1"
shift
case "$cmd" in
dev)
if [ $# -gt 1 -a $1 = 'hot-reload' ]; then
tool dev stop $2 && tool dev up --build -d $2
return $?
fi
docker-compose -f "$tool_root/docker-compose.base.yml" -f "$tool_root/docker-compose.dev.yml" "$@"
return $?
;;
# ----------------------------------------------------------------------
prod)
docker-compose -f "$tool_root/docker-compose.base.yml" -f "$tool_root/docker-compose.prod.yml" "$@"
;;
# ----------------------------------------------------------------------
db)
if [ $# -eq 0 ]; then
PGPASSWORD=1234 psql --host 127.0.0.1 testdb testuser
else
PGPASSWORD=1234 psql --host 127.0.0.1 testdb testuser < $1
fi
;;
# ----------------------------------------------------------------------
*)
BOLD='\033[1m'
NC='\033[0m'
# command called with no args
if [ "$cmd" == 'help' ]; then
printf "${BOLD}tool${NC} "
echo 'answers the question "what if you need to use make?" but'
echo 'you obsess over simplicity and prefer to make a bash script.'
echo 'It collects a bunch of utilities that you may (or may not)'
echo 'find useful in the development workflow of this project.'
echo
else
printf "Usage: ${BOLD}./tool <SUBCOMMAND> [<ARGUMENT>...]${NC}\n"
fi
if [ -z "$1" ]; then
echo 'Subcommands:'
fi
idx=0
found=false
while [ "${DOCUMENTATION[idx]}" ]; do
stringarr=(${DOCUMENTATION[idx]})
# if $1 is set and this is the wrong command, then skip it
if [ -n "$1" -a "${stringarr[0]}" != "$1" ]; then
((idx+=2))
continue
fi
found=true
echo
printf "\t${BOLD}${DOCUMENTATION[idx]}${NC}\n"
printf "\t\t${DOCUMENTATION[idx+1]}\n"
((idx+=2))
done
if [ "$found" = false ]; then
echo
echo "The specified subcommand $1 could not be found."
fi
;;
esac
}
tool "$@"
#!/bin/bash
# tool anywhere!
# install and +x in your $PATH
pwd="$PWD"
while [ ! -f './tool' -a "$PWD" != '/' ]; do
cd ..
done
if [ "$PWD" = '/' ]; then
echo 'tool not found'
exit 127
fi
env -C "$pwd" "$(realpath ./tool)" "$@"
exit $?
This is a dead-easy "build tool" which I use for some of my projects
when `go build` is not enough. It is a simple bash script which can
easily be extended to do whatever you like.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment