From 7505fbb408979510ec5bcfd91e2f8833913c6428 Mon Sep 17 00:00:00 2001 From: Daniel de Cloet Date: Thu, 30 Jun 2022 12:34:43 +0200 Subject: [PATCH] Update docker-env command --- templates/zshrc | 88 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 14 deletions(-) diff --git a/templates/zshrc b/templates/zshrc index 904e3d2..9d6748e 100644 --- a/templates/zshrc +++ b/templates/zshrc @@ -161,10 +161,39 @@ function see() { [ -f "$*" ] && cat "$*" || ls "$*" } -function docker-env() { +function docker-env() {( + set -e + run_command_switches=() workdir="/work_dir" + function parse_port() { + echo -n "$@" | perl -e ' + my $line = <>; + my ($defaultIp, $ip, $hostPort, $contPort) = $line =~ /^(?:([\d.]+) )?(?:(?:([\d.]+):)?(\d+):)?(\d+)$/; + if (!$contPort) { + print(STDERR "Invalid port format: \"$line\"\n"); + exit(1); + } + + if ($ip) { + print "$ip:"; + } else { + if ($defaultIp) { + print "$defaultIp:"; + } + } + + if ($hostPort) { + print "$hostPort:"; + } else { + print "$contPort:"; + } + + print "$contPort"; + ' + } + while do case "$1" in @@ -172,15 +201,40 @@ function docker-env() { shift break ;; - -[pevhmu]) + -[evhmu]) run_command_switches+=("$1") run_command_switches+=("$2") shift ;; + -p) + run_command_switches+=("-p") + run_command_switches+=("$(parse_port "127.0.0.1 $2")") + shift + ;; + -P) + run_command_switches+=("-p") + run_command_switches+=("$(parse_port "$2")") + shift + ;; + -E) + [ -e "$2" ] || (echo "No such file or directory: $2">&2; exit 1) + for arg in $(grep -v '^#' "$2" | xargs) + do + run_command_switches+=("-e") + run_command_switches+=("$arg") + done + shift + ;; + -U) + shift + ;; -w) workdir="$2" shift ;; + -r) + shift + ;; *) break ;; @@ -199,13 +253,17 @@ function docker-env() { Usage: docker-env [OPTIONS] [--] IMAGE [COMMAND] [ARG...] Options: - -p Publish a container's port(s) to the host - -e VAR=value Add environment variable VAR with value - -v Bind mount a volume - -w workdir Set working directory (default=/work_dir) - -h hostname Container host name - -m limit Memory limit - -u user Username or UID (format: [:]) + -p Publish a container's port(s) to the host locally accessible (127.0.0.1) + -P Publish a container's port(s) to the host publically accessible + -e VAR=value Add environment variable VAR with value + -E Load environment variables from a file + -v Bind mount a volume + -w Set working directory (default=/work_dir) + -h Container hostname + -m limit Memory limit + -u user Username or UID (format: ]) + -U user Same as -u, but create user if it does not exist (WIP) + -r Raw argument to pass to \`docker run\` (WIP) EOS return 1 fi @@ -228,7 +286,7 @@ EOS run_command+="-w \"$workdir\" \"$image\" $cmd" eval $run_command -} +)} # Completion for docker-env compdef _docker-env docker-env @@ -236,15 +294,17 @@ function _docker-env() { local curcontext="$curcontext" state _arguments \ - "*-p[Publish a containers port(s) to the host]:port:" \ - "*-e[Set environment variables]:" \ + "*-p[Publish a container's port(s) to the host locally accessible (127.0.0.1)]:port:" \ + "*-P[Publish a container's port(s) to the host publically accessible]:port:" \ + "*-e[Add environment variable VAR with value]:" \ + "*-E[Load environment variables from a file]:file:_files" \ "*-v[Bind mount a volume]:" \ "-w[Working directory inside the container]" \ "-h[Container host name]" \ "-m[Memory limit]" \ "-u[Username or UID (format: (|(:)))]" \ - "(-p -e -v -w -h -m -u)--[Explicitely stop switches and start positional arguments]" \ - "(-p -e -v -w -h -m -u --)1:Container image:->image" \ + "(-p -P -e -E -v -w -h -m -u)--[Explicitely stop switches and start positional arguments]" \ + "(-p -P -e -E -v -w -h -m -u --)1:Container image:->image" \ "*:Container CMD:->cmd" case $state in