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 if [[ -z $_debug ]]; then
cat <<-'EOF' cat <<-'EOF'
USAGE USAGE
podmanRun -m MODE -o OPTIONS -c COMMANDS [--help] [--debug] podmanRun -m MODE -o OPTIONS [COMMANDS [ARGS]...] [--help] [--debug]
COMMANDS COMMANDS
COMMANDS to run in the container COMMANDS to run in the container
@@ -46,11 +46,8 @@ OPTIONS
2. persistent (reuse existing container if it exists) 2. persistent (reuse existing container if it exists)
--options, -o OPTIONS --options, -o OPTIONS
OPTIONS to pass to podman run/exec 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 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 --debug, -d
Print debugging Print debugging
--help, -h --help, -h
@@ -66,11 +63,11 @@ EXAMPLES
-o "-v={FILE_ACTIVE_PATH}:/var/www/html:z" -o "php:7.3-apache" -o "-v={FILE_ACTIVE_PATH}:/var/www/html:z" -o "php:7.3-apache"
Run an ephemeral bash script: Run an ephemeral bash script:
podmanRun -o "--name=bash_script" -o "-v=$PWD:$PWD:z" -o "-w=$PWD" 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: Run an ephemeral bash script using IDE:
podmanRun -o "--name=bash_{FILE_ACTIVE_NAME_BASE}" \ podmanRun -o "--name=bash_{FILE_ACTIVE_NAME_BASE}" \
-o "-v={FILE_ACTIVE_PATH}:{FILE_ACTIVE_PATH}:z" -o "-w={FILE_ACTIVE_PATH}" \ -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 EOF
fi fi
@@ -87,7 +84,7 @@ EOF
local INPUT local INPUT
# Use getopt to print help # 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" eval set -- "$INPUT"
while true; do while true; do
case "$1" in case "$1" in
@@ -97,12 +94,9 @@ EOF
;; ;;
--options|-o) --options|-o)
shift shift
#
_opts_arr+=("$1") _opts_arr+=("$1")
;; ;;
--command|-c)
shift
_cmds_arr+=("$1")
;;
--help|-h) --help|-h)
_printHelpAndExit 0 _printHelpAndExit 0
;; ;;
@@ -122,15 +116,22 @@ EOF
_printHelpAndExit 1 _printHelpAndExit 1
fi fi
# Mode # Load remaining arguments into the commands array
# Set default shift $((OPTIND - 1))
_cmds_arr=("$@")
# Set default mode
[[ -z $_mode ]] && _mode="recreate" [[ -z $_mode ]] && _mode="recreate"
# Allow mode numbers # Allow mode numbers
[[ "$_mode" == "1" ]] && _mode="recreate" [[ "$_mode" == "1" ]] && _mode="recreate"
[[ "$_mode" == "2" ]] && _mode="persistent" [[ "$_mode" == "2" ]] && _mode="persistent"
# Sanity checks # Sanity check
[[ ! "$_mode" =~ ^(recreate|persistent)$ ]] && err "Bad --mode" && _printHelpAndExit 1 [[ ! "$_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 "_opts_arr:" "${_opts_arr[@]}"
debug "_cmds_arr:" "${_cmds_arr[@]}" debug "_cmds_arr:" "${_cmds_arr[@]}"
} }
@@ -157,15 +158,18 @@ EOF
fi fi
done done
# If no container name is specified, then generate one using the opts and cmds arrays _sanitize() {
[[ -z $_name ]] && _name="${_opts_arr[*]}${_cmds_arr[*]}" local i="$*"; i="${i// /}" && i="${i//[^a-zA-Z0-9_]/}" && i="${i,,}"; echo "$i" ;
}
# Sanitize container name # If no --name is specified, then generate one using the opts and cmds arrays
_name="${_name//_/}" && _name="${_name// /}" && \ if [[ -z $_name ]]; then
_name="${_name//[^a-zA-Z0-9_]/}" && _name="${_name,,}" _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" debug "_cname: $_cname"
} }
@@ -197,7 +201,7 @@ EOF
# Get input # Get input
_parseInput "$@" _parseInput "$@"
# Set container name # Set _cname (container name)
_setContainerName _setContainerName
# Remove container if necessary # Remove container if necessary