Update docker-env command

This commit is contained in:
Daniel_I_Am 2022-06-30 12:34:43 +02:00
parent e518752bc3
commit 7505fbb408

View File

@ -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 <port> Publish a container's port(s) to the host
-e VAR=value Add environment variable VAR with value
-v <volume> 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: <name|uid>[:<group|gid>])
-p <port> Publish a container's port(s) to the host locally accessible (127.0.0.1)
-P <port> Publish a container's port(s) to the host publically accessible
-e VAR=value Add environment variable VAR with value
-E <varfile> Load environment variables from a file
-v <volume> Bind mount a volume
-w <workdir> Set working directory (default=/work_dir)
-h <hostname> Container hostname
-m limit Memory limit
-u user Username or UID (format: <name|uid[:<group|gid>])
-U user Same as -u, but create user if it does not exist (WIP)
-r <raw> 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: (<name|uid>|(<name|uid>:<group|gid>)))]" \
"(-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