Skip to content

Instantly share code, notes, and snippets.

@Billy-
Created July 3, 2017 21:53
Show Gist options
  • Save Billy-/edbf2c73c6d3e538835d3f8cacfa31cd to your computer and use it in GitHub Desktop.
Save Billy-/edbf2c73c6d3e538835d3f8cacfa31cd to your computer and use it in GitHub Desktop.
project-compose, to compose your docker-compose files across projects. I heard you liked composing files.
#!/bin/bash
dev=''
build=''
restart=''
help=''
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
echo "I’m sorry, `getopt --test` failed in this environment."
exit 1
fi
SHORT=dbrh
LONG=dev,build,restart,help
# -temporarily store output to be able to check for errors
# -activate advanced mode getopt quoting e.g. via “--options”
# -pass arguments only via -- "$@" to separate them correctly
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-d|--dev)
dev='true'
shift
;;
-b|--build)
build='true'
shift
;;
-r|--restart)
restart='true'
shift
;;
-h|--help)
help='true'
shift
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
if [[ $# -lt 1 ]]; then
echo "no args"
fi
if [ $help ]
then
echo "Usage..."
exit 0
fi
CMD="docker-compose -p ${PWD##*/} -f ./config/docker/docker-compose.yml"
if [ $dev ]
then
CMD="$CMD -f ./config/docker/docker-compose.dev.yml"
fi
for service in "$@"
do
d=$([ "$dev" == 'true' ] && echo "dev." || echo "")
CMD="$CMD -f ./config/docker/docker-compose.$service.${d}yml"
done
CMD="$CMD up"
if [ $restart ]
then
CMD="$CMD -d"
fi
if [ $build ]
then
CMD="$CMD --build"
fi
if [ $restart ]
then
CMD="$CMD $@"
fi
echo $CMD
$CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment