Update docker-env command
This commit is contained in:
parent
e518752bc3
commit
7505fbb408
@ -161,10 +161,39 @@ function see() {
|
|||||||
[ -f "$*" ] && cat "$*" || ls "$*"
|
[ -f "$*" ] && cat "$*" || ls "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
function docker-env() {
|
function docker-env() {(
|
||||||
|
set -e
|
||||||
|
|
||||||
run_command_switches=()
|
run_command_switches=()
|
||||||
workdir="/work_dir"
|
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
|
while
|
||||||
do
|
do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@ -172,15 +201,40 @@ function docker-env() {
|
|||||||
shift
|
shift
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
-[pevhmu])
|
-[evhmu])
|
||||||
run_command_switches+=("$1")
|
run_command_switches+=("$1")
|
||||||
run_command_switches+=("$2")
|
run_command_switches+=("$2")
|
||||||
shift
|
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)
|
-w)
|
||||||
workdir="$2"
|
workdir="$2"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
-r)
|
||||||
|
shift
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
@ -199,13 +253,17 @@ function docker-env() {
|
|||||||
Usage: docker-env [OPTIONS] [--] IMAGE [COMMAND] [ARG...]
|
Usage: docker-env [OPTIONS] [--] IMAGE [COMMAND] [ARG...]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-p <port> Publish a container's port(s) to the host
|
-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 VAR=value Add environment variable VAR with value
|
||||||
|
-E <varfile> Load environment variables from a file
|
||||||
-v <volume> Bind mount a volume
|
-v <volume> Bind mount a volume
|
||||||
-w workdir Set working directory (default=/work_dir)
|
-w <workdir> Set working directory (default=/work_dir)
|
||||||
-h hostname Container host name
|
-h <hostname> Container hostname
|
||||||
-m limit Memory limit
|
-m limit Memory limit
|
||||||
-u user Username or UID (format: <name|uid>[:<group|gid>])
|
-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
|
EOS
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@ -228,7 +286,7 @@ EOS
|
|||||||
run_command+="-w \"$workdir\" \"$image\" $cmd"
|
run_command+="-w \"$workdir\" \"$image\" $cmd"
|
||||||
|
|
||||||
eval $run_command
|
eval $run_command
|
||||||
}
|
)}
|
||||||
|
|
||||||
# Completion for docker-env
|
# Completion for docker-env
|
||||||
compdef _docker-env docker-env
|
compdef _docker-env docker-env
|
||||||
@ -236,15 +294,17 @@ function _docker-env() {
|
|||||||
local curcontext="$curcontext" state
|
local curcontext="$curcontext" state
|
||||||
|
|
||||||
_arguments \
|
_arguments \
|
||||||
"*-p[Publish a containers port(s) to the host]:port:" \
|
"*-p[Publish a container's port(s) to the host locally accessible (127.0.0.1)]:port:" \
|
||||||
"*-e[Set environment variables]:" \
|
"*-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]:" \
|
"*-v[Bind mount a volume]:" \
|
||||||
"-w[Working directory inside the container]" \
|
"-w[Working directory inside the container]" \
|
||||||
"-h[Container host name]" \
|
"-h[Container host name]" \
|
||||||
"-m[Memory limit]" \
|
"-m[Memory limit]" \
|
||||||
"-u[Username or UID (format: (<name|uid>|(<name|uid>:<group|gid>)))]" \
|
"-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 -P -e -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 --)1:Container image:->image" \
|
||||||
"*:Container CMD:->cmd"
|
"*:Container CMD:->cmd"
|
||||||
|
|
||||||
case $state in
|
case $state in
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user