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)
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
Print this help message and exit (overrides --silent)
@@ -63,22 +59,21 @@ EOF
}
_runDebug () {
debug () {
[[ -n $_debug ]] && echo "Running: " "$@"
[[ -n $_debug ]] && echo "debug: " "$@"
}
_parseInput () {
_runDebug "${FUNCNAME[0]}" "$@"
debug "${FUNCNAME[0]}" "$@"
# Unset vars
unset _array
declare -a _quiet=(">" "/dev/null" "2>&1")
# 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"
while true; do
case "$1" in
@@ -97,21 +92,17 @@ EOF
--recreate)
_recreate="true"
;;
--no-sh)
--no-sh|-n)
_no_sh="true"
;;
--debug)
--debug|-d)
_debug="true"
unset _quiet
echo "Debugging on!"
;;
--array|-a)
shift && _array="$1"
break
;;
--silent)
#_silent="true"
;;
--help|-h)
_printHelpAndExit 0
;;
@@ -139,16 +130,16 @@ EOF
# Create _pre_commands_array from remaining arguments
# shift getopt parameters away
shift $((OPTIND - 1))
# create array
declare -a _cmd_array
# Assume program name is first argument
_program="$1"
# create command array
declare -ga _cmd_array=("$@")
}
_shWrap () {
_runDebug "${FUNCNAME[0]}"
debug "${FUNCNAME[0]}"
if [[ -z $_no_sh ]]; then
_cmd_array=("sh" "-c" "${_cmd_array[*]}")
@@ -158,36 +149,33 @@ EOF
_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 () {
_runDebug "${FUNCNAME[0]}"
toolbox create -c "$_cname" "${_image[@]}" "${_release[@]}" "${_quiet[@]}"
debug "${FUNCNAME[0]}" "$1" "${_image[@]}" "${_release[@]}"
toolbox create -c "$1" "${_image[@]}" "${_release[@]}"
}
_toolboxRemove () {
_runDebug "${FUNCNAME[0]}"
debug "${FUNCNAME[0]}" "$1"
toolbox rm -f "$_cname" "${_quiet[@]}"
toolbox rm -f "$1"
}
_toolboxRun () {
_runDebug "${FUNCNAME[0]}"
debug "${FUNCNAME[0]}" "$1" "${_cmd_array[@]}"
[[ -n $_debug ]] && echo "_cmd_array:" "${_cmd_array[@]}"
toolbox run -c "$_cname" "${_cmd_array[@]}"
toolbox run -c "$1" "${_cmd_array[@]}"
}
@@ -203,18 +191,18 @@ EOF
_shWrap
# Check if container exists
if _toolboxExists; then
if _toolboxExists "$_cname"; then
if [[ -n $_recreate || -n $_ephemeral ]]; then
_toolboxRemove
_toolboxRemove "$_cname"
fi
else
_toolboxCreate
_toolboxCreate "$_cname"
fi
_toolboxRun
_toolboxRun "$_cname"
if [[ -n $_ephemeral ]]; then
_toolboxRemove
_toolboxRemove "$_cname"
fi
}