Cleanup debugging and generalize function

This commit is contained in:
cryobry
2020-05-09 10:26:00 -04:00
parent 3e07284a4e
commit 55aea4774a

View File

@@ -49,10 +49,6 @@ OPTIONS
Read arguments from an existing or new ARRAY (bash >= 4.3) Read arguments from an existing or new ARRAY (bash >= 4.3)
This is useful to reduce parsing errors and recommended for build-wrapper plugins This is useful to reduce parsing errors and recommended for build-wrapper plugins
--silent, -s
Don't output anything from this program (container output will still be passed to stdout
if -it option is used instead of -d, see `man podman run` for more information)
--help, -h --help, -h
Print this help message and exit (overrides --silent) Print this help message and exit (overrides --silent)
@@ -63,22 +59,21 @@ EOF
} }
_runDebug () { debug () {
[[ -n $_debug ]] && echo "Running: " "$@" [[ -n $_debug ]] && echo "debug: " "$@"
} }
_parseInput () { _parseInput () {
_runDebug "${FUNCNAME[0]}" "$@" debug "${FUNCNAME[0]}" "$@"
# Unset vars # Unset vars
unset _array unset _array
declare -a _quiet=(">" "/dev/null" "2>&1")
# Parse input and set switches using getopt # Parse input and set switches using getopt
if _input=$(getopt -o +c:i:r:a:ndsh -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,silent,help -- "$@"); then if _input=$(getopt -o +c:i:r:nda:h -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,help -- "$@"); then
eval set -- "$_input" eval set -- "$_input"
while true; do while true; do
case "$1" in case "$1" in
@@ -97,21 +92,17 @@ EOF
--recreate) --recreate)
_recreate="true" _recreate="true"
;; ;;
--no-sh) --no-sh|-n)
_no_sh="true" _no_sh="true"
;; ;;
--debug) --debug|-d)
_debug="true" _debug="true"
unset _quiet
echo "Debugging on!" echo "Debugging on!"
;; ;;
--array|-a) --array|-a)
shift && _array="$1" shift && _array="$1"
break break
;; ;;
--silent)
#_silent="true"
;;
--help|-h) --help|-h)
_printHelpAndExit 0 _printHelpAndExit 0
;; ;;
@@ -139,16 +130,16 @@ EOF
# Create _pre_commands_array from remaining arguments # Create _pre_commands_array from remaining arguments
# shift getopt parameters away # shift getopt parameters away
shift $((OPTIND - 1)) shift $((OPTIND - 1))
# create array # Assume program name is first argument
declare -a _cmd_array
_program="$1" _program="$1"
# create command array
declare -ga _cmd_array=("$@") declare -ga _cmd_array=("$@")
} }
_shWrap () { _shWrap () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
if [[ -z $_no_sh ]]; then if [[ -z $_no_sh ]]; then
_cmd_array=("sh" "-c" "${_cmd_array[*]}") _cmd_array=("sh" "-c" "${_cmd_array[*]}")
@@ -158,36 +149,33 @@ EOF
_toolboxExists () { _toolboxExists () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1"
toolbox list -c | cut -d ' ' -f 3 | grep -w "$_cname" toolbox list -c | cut -d ' ' -f 3 | grep -w "$1" > /dev/null 2>&1
} }
_toolboxCreate () { _toolboxCreate () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1" "${_image[@]}" "${_release[@]}"
toolbox create -c "$_cname" "${_image[@]}" "${_release[@]}" "${_quiet[@]}"
toolbox create -c "$1" "${_image[@]}" "${_release[@]}"
} }
_toolboxRemove () { _toolboxRemove () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1"
toolbox rm -f "$_cname" "${_quiet[@]}" toolbox rm -f "$1"
} }
_toolboxRun () { _toolboxRun () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1" "${_cmd_array[@]}"
[[ -n $_debug ]] && echo "_cmd_array:" "${_cmd_array[@]}" toolbox run -c "$1" "${_cmd_array[@]}"
toolbox run -c "$_cname" "${_cmd_array[@]}"
} }
@@ -203,18 +191,18 @@ EOF
_shWrap _shWrap
# Check if container exists # Check if container exists
if _toolboxExists; then if _toolboxExists "$_cname"; then
if [[ -n $_recreate || -n $_ephemeral ]]; then if [[ -n $_recreate || -n $_ephemeral ]]; then
_toolboxRemove _toolboxRemove "$_cname"
fi fi
else else
_toolboxCreate _toolboxCreate "$_cname"
fi fi
_toolboxRun _toolboxRun "$_cname"
if [[ -n $_ephemeral ]]; then if [[ -n $_ephemeral ]]; then
_toolboxRemove _toolboxRemove "$_cname"
fi fi
} }