diff --git a/templates/zshrc b/templates/zshrc index 149502f..48901fc 100644 --- a/templates/zshrc +++ b/templates/zshrc @@ -162,14 +162,75 @@ function see() { } function docker-env() { - if [ $# -lt 1 ]; then - echo "Usage: docker-env [command]" - return 1 - else - name=$(echo -n $1| grep -Eo '^[^:]+' | tr '/' '-') - index=$(docker ps -a --filter "name=docker-env-$name-*" --format '{% raw %}{{.Names}}{% endraw %}' | perl -e 'my $max = 0; while (<>) {my ($n) = $_ =~ /(\d+)$/; if ($n > $max) { $max = $n };}; print $max+1;') - docker run --rm --name "docker-env-$name-$index" -it -v "$(pwd):/work_dir" -w "/work_dir" "$1" "${@:2}" + function usage() { + cat >&2 < Publish a container's port(s) to the host + -v Bind mount a volume +EOS + exit 1 + } + + port= + workdir=/work_dir + volumes=("$(pwd):/work_dir") + + while + do + case "$1" in + --) + shift + break + ;; + -p) + port=$2 + shift + ;; + -v) + volumes+=("$2") + shift + ;; + *) + break + ;; + esac + + if [ $# -eq 0 ] + then + usage + fi + shift + done + + if [ $# -eq 0 ] + then + usage fi + + image="$1" + shift + cmd="$@" + + image_repository=$(echo -n "$image"| grep -Eo '^[^:]+' | tr '/' '-') + index=$(docker ps -a --filter "name=docker-env-$image_repository-*" --format '{% raw %}{{.Names}}{% endraw %}' | perl -e 'my $max = 0; while (<>) {my ($n) = $_ =~ /(\d+)$/; if ($n > $max) { $max = $n };}; print $max+1;') + name="docker-env-$image_repository-$index" + + run_command="docker run --rm -it --name \"$name\"" + + if [ ! -z "$port" ] + then + run_command+=" -p $port" + fi + for volume in $volumes + do + run_command+=" -v $volume" + done + + run_command+=" $image $cmd" + + eval $run_command } function limit_video_size() {