Allow combining multiple option strings into one

This commit is contained in:
cryobry
2020-05-10 12:37:33 -04:00
parent af21c8559a
commit 9ad746b486

View File

@@ -35,7 +35,7 @@ podmanRun() {
if [[ -z $_debug ]]; then
cat <<-'EOF'
USAGE
podmanRun -m MODE -o OPTIONS -c COMMANDS [--help] [--debug]
podmanRun -m MODE -o OPTIONS [COMMANDS [ARGS]...] [--help] [--debug]
COMMANDS
COMMANDS to run in the container
@@ -46,11 +46,8 @@ OPTIONS
2. persistent (reuse existing container if it exists)
--options, -o OPTIONS
OPTIONS to pass to podman run/exec
Can be passed multiple times
Can be passed multiple times to concatenate
Final option should be the name of the container image
--commands, -c COMMANDS
COMMANDS to execute in the container designated by OPTIONS
Can be passed multiple times
--debug, -d
Print debugging
--help, -h
@@ -66,11 +63,11 @@ EXAMPLES
-o "-v={FILE_ACTIVE_PATH}:/var/www/html:z" -o "php:7.3-apache"
Run an ephemeral bash script:
podmanRun -o "--name=bash_script" -o "-v=$PWD:$PWD:z" -o "-w=$PWD"
-o "debian:testing" -c "./script.sh"
-o "debian:testing" "./script.sh"
Run an ephemeral bash script using IDE:
podmanRun -o "--name=bash_{FILE_ACTIVE_NAME_BASE}" \
-o "-v={FILE_ACTIVE_PATH}:{FILE_ACTIVE_PATH}:z" -o "-w={FILE_ACTIVE_PATH}" \
-o "debian:testing" -c "{FILE_ACTIVE}"
-o "debian:testing" "{FILE_ACTIVE}"
EOF
fi
@@ -87,7 +84,7 @@ EOF
local INPUT
# Use getopt to print help
if INPUT=$(getopt -o +m:o:c:dh -l mode:,options:,commands:,debug,help -- "$@"); then
if INPUT=$(getopt -o +m:o:dh -l mode:,options:,debug,help -- "$@"); then
eval set -- "$INPUT"
while true; do
case "$1" in
@@ -97,12 +94,9 @@ EOF
;;
--options|-o)
shift
#
_opts_arr+=("$1")
;;
--command|-c)
shift
_cmds_arr+=("$1")
;;
--help|-h)
_printHelpAndExit 0
;;
@@ -122,15 +116,22 @@ EOF
_printHelpAndExit 1
fi
# Mode
# Set default
# Load remaining arguments into the commands array
shift $((OPTIND - 1))
_cmds_arr=("$@")
# Set default mode
[[ -z $_mode ]] && _mode="recreate"
# Allow mode numbers
[[ "$_mode" == "1" ]] && _mode="recreate"
[[ "$_mode" == "2" ]] && _mode="persistent"
# Sanity checks
# Sanity check
[[ ! "$_mode" =~ ^(recreate|persistent)$ ]] && err "Bad --mode" && _printHelpAndExit 1
# Split options on whitespace
# https://unix.stackexchange.com/a/519917/382539
readarray -td' ' _opts_arr < <(printf '%s' "${_opts_arr[*]}")
debug "_opts_arr:" "${_opts_arr[@]}"
debug "_cmds_arr:" "${_cmds_arr[@]}"
}
@@ -157,15 +158,18 @@ EOF
fi
done
# If no container name is specified, then generate one using the opts and cmds arrays
[[ -z $_name ]] && _name="${_opts_arr[*]}${_cmds_arr[*]}"
_sanitize() {
local i="$*"; i="${i// /}" && i="${i//[^a-zA-Z0-9_]/}" && i="${i,,}"; echo "$i" ;
}
# Sanitize container name
_name="${_name//_/}" && _name="${_name// /}" && \
_name="${_name//[^a-zA-Z0-9_]/}" && _name="${_name,,}"
# If no --name is specified, then generate one using the opts and cmds arrays
if [[ -z $_name ]]; then
_cname="$(_sanitize "${_opts_arr[*]}${_cmds_arr[*]}")"
else
_cname="$(_sanitize "$_name")"
[[ "$_name" != "$_cname" ]] && err "You must provide a valid --name" && exit 1
fi
# Our finished container name
_cname="$_name"
debug "_cname: $_cname"
}
@@ -197,7 +201,7 @@ EOF
# Get input
_parseInput "$@"
# Set container name
# Set _cname (container name)
_setContainerName
# Remove container if necessary