Skip to content
Merged
92 changes: 80 additions & 12 deletions dockerc
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,22 @@ else
CONTEXT=''
fi

# Other parameters are arguments passed to docker compose
ARGS="$@"

# Check for docker preset
if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then

# Other arguments to pass to docker
# Use single quotes to safely pass arguments to shell
# Escape single quotes in arguments
ARGS=''
while [ $# -gt 0 ]; do
NEW_ARG=$(echo "$1" | sed "s/'/'\"'\"'/g")
ARGS="$ARGS '$NEW_ARG'"
shift
done

PRINT_DOCKER_PRESETS_HELP=false
DOCKER_ARGS=''
RUN_SHELL=false

# Remove first char
CONTEXT="${CONTEXT#?}"
Expand Down Expand Up @@ -200,6 +208,38 @@ if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then
DOCKER_ARGS='system prune -f -a --volumes'
;;

'sa')
# Stop all containers
DOCKER_ARGS='stop $(docker ps -q)'
RUN_SHELL=true
;;

'rmf')
# Remove running containers, force
DOCKER_ARGS='rm -f $(docker ps -a -q)'
RUN_SHELL=true
;;
'rms')
# Remove stopped containers
DOCKER_ARGS='rm $(docker ps -a -q -f status=exited -f status=dead)'
RUN_SHELL=true
;;
'rmsf'|'rmfs')
# Remove stopped containers, force
DOCKER_ARGS='rm -f $(docker ps -a -q -f status=exited -f status=dead)'
RUN_SHELL=true
;;
'rma')
# Remove all containers
DOCKER_ARGS='rm $(docker ps -a -q)'
RUN_SHELL=true
;;
'rmaf'|'rmfa')
# Remove all containers, force
DOCKER_ARGS='rm -f $(docker ps -q)'
RUN_SHELL=true
;;

*)
echo "Error: Unknown docker preset '@$CONTEXT'"
exit 1
Expand All @@ -218,29 +258,57 @@ if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then
echo ' @rf Remove unused containers, networks and images'
echo ' @rfa Remove all unused containers, networks and images'
echo ' @rfav Remove all unused containers, networks, images and volumes'
echo ' @sa Stop all running containers'
echo ' @rmf Remove running containers, force'
echo ' @rms Remove stopped containers'
echo ' @rmsf Remove stopped containers, force'
echo ' (aliases: @rmfs)'
echo ' @rma Remove all containers'
echo ' @rmaf Remove all containers, force'
echo ' (aliases: @rmfa)'
echo ' options:'
echo ' -f Force, for example ignores abstract contexts'
echo ' -n Dry run, print docker command without running it'
echo ' -q Quiet, do not print docker command'
exit 0
fi

if [ "$QUIET" = false ]; then
# Print docker command
echo ''
echo "> docker $DOCKER_ARGS $ARGS"
echo ''
fi
if [ "$RUN_SHELL" = false ]; then

if [ "$QUIET" = false ]; then
# Print docker command
echo ''
echo "> docker ${DOCKER_ARGS}${ARGS}"
echo ''
fi

if [ "$DRY_RUN" = false ]; then
# Run docker command
exec docker ${DOCKER_ARGS}${ARGS}
fi

else # "$RUN_SHELL" = true

if [ "$QUIET" = false ]; then
# Print docker command
echo ''
echo "> sh -c 'docker ${DOCKER_ARGS}${ARGS}'"
echo ''
fi

if [ "$DRY_RUN" = false ]; then
# Run docker command
exec sh -c "docker ${DOCKER_ARGS}${ARGS}"
fi

if [ "$DRY_RUN" = false ]; then
# Run docker
exec docker $DOCKER_ARGS $ARGS
fi

exit 0

fi

# Other parameters are arguments passed to docker compose
ARGS="$@"

COMPOSE_FILE_ARGS=''
NO_COMPOSE_FILE_ARGS=false
Expand Down
8 changes: 8 additions & 0 deletions test/test/help_docker/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def get_help_stdout(dockerc_path: str):
b' @rf Remove unused containers, networks and images\n'
b' @rfa Remove all unused containers, networks and images\n'
b' @rfav Remove all unused containers, networks, images and volumes\n'
b' @sa Stop all running containers\n'
b' @rmf Remove running containers, force\n'
b' @rms Remove stopped containers\n'
b' @rmsf Remove stopped containers, force\n'
b' (aliases: @rmfs)\n'
b' @rma Remove all containers\n'
b' @rmaf Remove all containers, force\n'
b' (aliases: @rmfa)\n'
b' options:\n'
b' -f Force, for example ignores abstract contexts\n'
b' -n Dry run, print docker command without running it\n'
Expand Down