Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function array_exist()
{
local needle="$1"
shift
local i
for i; do
[ "$needle" = "$i" ] && return 0 || continue
done
return 1
}
# Provides new action "oneshot" = "run" + "down"
function docker-compose()
{
local compose_args=()
local action_args=()
local action
# everything except this, has as argument
# (sync with docker-compose --help)
local compose_flags=(
--verbose
--no-ansi
--tls
--tlsverify
--skip-hostname-check
--compatibility
-v --version
)
local args=("$@")
for ((i = 0; i < $#; ++i)); do
local arg="${args[i]}"
if [[ -n $action ]]; then
action_args+=("$arg")
continue
fi
if [[ ${arg:0:1} != "-" ]]; then
action="$arg"
else
compose_args+=("$arg")
if ! array_exist "$arg" "${compose_flags[@]}"; then
# skip next argument too
(( ++i ))
arg="${args[i]}"
compose_args+=("$arg")
fi
fi
done
case "$action" in
oneshot)
command docker-compose "${compose_args[@]}" run "${action_args[@]}" && \
command docker-compose "${compose_args[@]}" down --volumes --remove-orphans
;;
*)
command docker-compose "$@";;
esac
}
export -f docker-compose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment