1
0
mirror of https://github.com/frebib/dotfiles.git synced 2024-06-14 12:57:23 +00:00

Improve dsh and drsh functions

This commit is contained in:
Joe Groocock 2017-07-15 13:32:22 +01:00
parent 4e03da05dc
commit eddd8e5f27
Signed by: frebib
GPG Key ID: E0B16BEACFBB6A86

31
aliases
View File

@ -157,9 +157,14 @@ alias dlg='docker logs -f'
alias dalpine='docker run -ti --rm alpine /bin/sh' alias dalpine='docker run -ti --rm alpine /bin/sh'
alias drm-stopped='docker container prune' alias drm-stopped='docker container prune'
alias drmi-untag='docker image prune' alias drmi-untag='docker image prune'
alias dsh='_dsh exec' alias dsh='_CMD=dsh _dsh exec'
alias drsh='_dsh "run --rm"' alias drsh='_CMD=drsh _dsh "run --rm"'
_dsh() { _dsh() {
if [ $# -lt 2 ]; then
echo "Usage: ${_CMD:-$0} [user@]container [program [args]]" >&2
return 64
fi
local dowhat="$1" local dowhat="$1"
local user=$(echo "$2" | cut -d'@' -f1 -s) local user=$(echo "$2" | cut -d'@' -f1 -s)
local prog="${@:3:$#}" local prog="${@:3:$#}"
@ -171,7 +176,27 @@ _dsh() {
usercmd="--user=$user " usercmd="--user=$user "
fi fi
eval "docker $dowhat -ti $usercmd$host ${prog:-"/bin/sh"}" if [ "$dowhat" = 'exec' -a -z "$(docker ps -q -f name="$host")" ]; then
echo "No such container $host" >&2
return 5
fi
if [ -z "$prog" ]; then
set -o pipefail
for shell in bash sh; do
if shell_path="$(eval "docker $dowhat -ti $host which $shell" | sed 's|[\r\n]||g')"; then
prog="$shell_path"
break
fi
done
set +o pipefail
if [ -z "$prog" ]; then
echo "Warning: No shell found in path.. trying /bin/sh" >&2
prog=/bin/sh
fi
fi
eval "docker $dowhat -ti $usercmd$host $prog"
} }
alias dm='docker-machine' alias dm='docker-machine'