Commit a86c0956 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add Docker-CLI wrapper

parent e6d41756
Loading
Loading
Loading
Loading

.bin/docker

0 → 100755
+61 −0
Original line number Diff line number Diff line
#!/bin/bash
set -eu -o pipefail
shopt -s lastpipe

declare CMD
declare -a OPTIONS=()
declare -a ARG=()

until [[ $# -eq 0 ]]; do
	case $1 in
		# Bool flag options; no option argument without '='
		--help|-D|--debug|-v|--version) OPTIONS+=( "$1" ) ;;
		--tls|--tlsverify) OPTIONS+=( "$1" ) ;;

		# Handle options with arguments & compacted flags
		--*=*) OPTIONS+=( "$1" ) ;;
		--*|-?) OPTIONS+=( "$1" ${2+"$2"} ); shift ;;
		-*) set -- ${1%${1#-?}} -${1#-?} "${@:2}"; continue ;;

		# End of run options; --rm not found so add it
		*) CMD=$1; shift; break ;;
	esac
	shift || break
done

cmd_run() {
	# Ensure --rm is in args if --detach is not
	until [[ $# -eq 0 ]]; do
		case $1 in
			# Shortcut --help, --rm and --detach; no need to add anything
			--help|--rm|-d|--detach*) ARG+=( "$@" ); return ;;

			# Bool flag options; no option argument without '='
			--init|-i|--interactive|--privileged) ARG+=( "$1" ) ;;
			--read-only|--sig-proxy|-t|--tty) ARG+=( "$1" ) ;;

			# Handle options with arguments & compacted flags
			--) ARG+=( "$@" ); return ;;
			--*=*) ARG+=( "$1" ) ;;
			--*|-?) ARG+=( "$1" ${2+"$2"} ); shift ;;
			-*) set -- ${1%${1#-?}} -${1#-?} "${@:2}"; continue ;;

			# End of run options; --rm not found so add it
			*) ARG+=( --rm "$@" ); return ;;
		esac
		shift || break
	done
}

case $CMD in
	run) cmd_run "$@" ;;
	*) ARG=( "$@" ) ;;
esac

for path in $(type -ap docker); do
	[[ $path -ef $0 ]] && continue
	exec $path "${OPTIONS[@]}" "$CMD" "${ARG[@]}"
done

echo >&2 "No 'docker' found on PATH"
exit 127