Simplify __main() logic
This commit is contained in:
@@ -1,29 +1,29 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# shellcheck disable=SC1090,SC2004
|
# shellcheck disable=SC1090,SC2004
|
||||||
|
|
||||||
podmanRunWrapper () {
|
podmanRunWrapper() {
|
||||||
|
|
||||||
########################
|
########################
|
||||||
###### FUNCTIONS #######
|
###### FUNCTIONS #######
|
||||||
########################
|
########################
|
||||||
|
|
||||||
_printHelpAndExit () {
|
_printHelpAndExit() {
|
||||||
|
|
||||||
if [[ -z $_debug ]]; then
|
if [[ -z $_debug ]]; then
|
||||||
cat <<-'EOF'
|
cat <<-'EOF'
|
||||||
USAGE
|
USAGE
|
||||||
Argument mode:
|
Argument mode:
|
||||||
podman-run-wrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME]
|
podmanRunWrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME] [--help]
|
||||||
[--help] [--silent] [--debug] [COMMANDS [ARGS...]]
|
[--debug] [COMMANDS [ARGS...]]
|
||||||
|
|
||||||
Array mode (bash >= 4.3):
|
Array mode (bash >= 4.3):
|
||||||
podman-run-wrapper -a ARRAY
|
podmanRunWrapper -a ARRAY
|
||||||
|
|
||||||
EXAMPLE
|
EXAMPLE
|
||||||
podman-run-wrapper -m ephemeral -o "-it -v $PWD:$PWD -w $PWD" -i "php:latest" -c "php ./script.php"
|
podmanRunWrapper -m ephemeral -o "-it -v $PWD:$PWD -w $PWD" -i "php:latest" -c "php ./script.php"
|
||||||
|
|
||||||
ARRAY=( "-m" "ephemeral" "-o" "--rm -it -v $PWD:$PWD -w $PWD" "-i" "php:latest" "-c" "php ./script.php")
|
ARRAY=( "-m" "ephemeral" "-o" "--rm -it -v $PWD:$PWD -w $PWD" "-i" "php:latest" "-c" "php ./script.php")
|
||||||
podman-run-wrapper -a ARRAY
|
podmanRunWrapper -a ARRAY
|
||||||
|
|
||||||
COMMANDS
|
COMMANDS
|
||||||
COMMANDS to run in the container (e.g. the current active file, an external build script, a
|
COMMANDS to run in the container (e.g. the current active file, an external build script, a
|
||||||
@@ -37,7 +37,6 @@ OPTIONS
|
|||||||
1. ephemeral
|
1. ephemeral
|
||||||
2. persistent
|
2. persistent
|
||||||
3. recreate-persistent
|
3. recreate-persistent
|
||||||
4. remove-persistent
|
|
||||||
|
|
||||||
--options, -o OPTIONS
|
--options, -o OPTIONS
|
||||||
OPTIONS to pass directly to `podman run` or `podman exec` depending on the mode or
|
OPTIONS to pass directly to `podman run` or `podman exec` depending on the mode or
|
||||||
@@ -65,9 +64,6 @@ OPTIONS
|
|||||||
--selinuxfix
|
--selinuxfix
|
||||||
A temporary hack to grant SELinux write access on $PWD until a better fix is found
|
A temporary hack to grant SELinux write access on $PWD until a better fix is found
|
||||||
|
|
||||||
--silent, -s
|
|
||||||
Only print errors
|
|
||||||
|
|
||||||
--debug, -d
|
--debug, -d
|
||||||
Print debugging
|
Print debugging
|
||||||
|
|
||||||
@@ -83,12 +79,12 @@ EOF
|
|||||||
|
|
||||||
|
|
||||||
# Parse input
|
# Parse input
|
||||||
_parseInput () {
|
_parseInput() {
|
||||||
|
|
||||||
unset _mode _cmds_arr _opts_arr _options _prw_opts_arr _image _name _array _selinux_fix
|
unset _mode _cmds_arr _opts_arr _options _prw_opts_arr _image _name _array _selinux_fix
|
||||||
|
|
||||||
# Use getopt to print help
|
# Use getopt to print help
|
||||||
if INPUT=$(getopt -o +m:o:i:x:n:a:sdh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,silent,debug,help -- "$@"); then
|
if INPUT=$(getopt -o +m:o:i:x:n:a:dh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,debug,help -- "$@"); then
|
||||||
eval set -- "$INPUT"
|
eval set -- "$INPUT"
|
||||||
while true; do
|
while true; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@@ -126,11 +122,8 @@ EOF
|
|||||||
--help|-h)
|
--help|-h)
|
||||||
_printHelpAndExit 0
|
_printHelpAndExit 0
|
||||||
;;
|
;;
|
||||||
--silent|-s)
|
|
||||||
_silent="1"
|
|
||||||
;;
|
|
||||||
--debug|-d)
|
--debug|-d)
|
||||||
_debug="1"
|
export _debug="1"
|
||||||
echo "Debugging on!"
|
echo "Debugging on!"
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
@@ -178,16 +171,16 @@ EOF
|
|||||||
declare -ga _prw_cmds_arr
|
declare -ga _prw_cmds_arr
|
||||||
_prw_cmds_arr=("$@")
|
_prw_cmds_arr=("$@")
|
||||||
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
|
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
|
||||||
[[ -z $_silent ]] && echo "Warning: running container without any commands"
|
debug "Running container without any commands"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ -n $_debug ]] && echo "_prw_opts_arr:" "${_prw_opts_arr[@]}"
|
debug "_prw_opts_arr:" "${_prw_opts_arr[@]}"
|
||||||
[[ -n $_debug ]] && echo "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
|
debug "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_addCName () {
|
_addCName() {
|
||||||
|
|
||||||
# autogenerate _name if missing
|
# autogenerate _name if missing
|
||||||
[[ -z $_name ]] && _name="${_image}${_prw_cmds_arr[*]}"
|
[[ -z $_name ]] && _name="${_image}${_prw_cmds_arr[*]}"
|
||||||
@@ -206,27 +199,24 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_removeContainer () {
|
_removeContainer() {
|
||||||
|
|
||||||
if podman container exists "$_cname"; then
|
if podman container exists "$1"; then
|
||||||
[[ -z $_silent ]] && echo "Removing container: $_cname"
|
debug "podman rm -v -f $1"
|
||||||
[[ -n $_debug ]] && echo "podman rm -v -f $_cname"
|
podman rm -v -f "$1"
|
||||||
podman rm -v -f "$_cname"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_runContainer () {
|
_runContainer() {
|
||||||
|
|
||||||
# Run _remove_container first to not run in existing container
|
# Run _remove_container first to not run in existing container
|
||||||
if podman container exists "${_cname}"; then
|
if podman container exists "$1"; then
|
||||||
[[ -z $_silent ]] && echo "Reusing container: $_cname"
|
debug podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
|
||||||
[[ -n $_debug ]] && echo podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
|
podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
|
||||||
podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
|
|
||||||
exit $?
|
exit $?
|
||||||
else
|
else
|
||||||
[[ -z $_silent ]] && echo "Running in container: $_cname"
|
debug "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
|
||||||
[[ -n $_debug ]] && echo "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
|
|
||||||
podman run "${_prw_opts_arr[@]}" "$_image" "${_prw_cmds_arr[@]}"
|
podman run "${_prw_opts_arr[@]}" "$_image" "${_prw_cmds_arr[@]}"
|
||||||
exit $?
|
exit $?
|
||||||
fi
|
fi
|
||||||
@@ -237,7 +227,7 @@ EOF
|
|||||||
####### EXECUTE #########
|
####### EXECUTE #########
|
||||||
#########################
|
#########################
|
||||||
|
|
||||||
_execute () {
|
__main() {
|
||||||
|
|
||||||
# Get input
|
# Get input
|
||||||
_parseInput "$@"
|
_parseInput "$@"
|
||||||
@@ -249,26 +239,40 @@ EOF
|
|||||||
[[ -n $_selinux_fix ]] && fixPermissions "$PWD"
|
[[ -n $_selinux_fix ]] && fixPermissions "$PWD"
|
||||||
|
|
||||||
# Execute podman
|
# Execute podman
|
||||||
if [[ $_mode == "ephemeral" || $_mode == "recreate-persistent" ]]; then
|
if [[ "$_mode" =~ ^(ephemeral|recreate-persistent)$ ]]; then
|
||||||
_removeContainer
|
_removeContainer "$_cname"
|
||||||
_runContainer
|
|
||||||
elif [[ $_mode == "remove-persistent" ]]; then
|
|
||||||
_removeContainer
|
|
||||||
elif [[ $_mode == "persistent" ]]; then
|
|
||||||
_runContainer
|
|
||||||
else
|
|
||||||
echo "Unknown mode!"
|
|
||||||
_printHelpAndExit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
_runContainer "$_cname"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Allow this function to be executed directly
|
# Allow this function to be executed directly
|
||||||
_execute "$@"
|
__main "$@"
|
||||||
|
exit $?
|
||||||
}
|
}
|
||||||
|
|
||||||
# Allow script to be called directly
|
# Allow script to be called directly
|
||||||
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
||||||
# No imported functions
|
|
||||||
#source-functions
|
_getBaseDir () {
|
||||||
|
# Get base directory name of where this script resides
|
||||||
|
# https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself#comment54598418_246128
|
||||||
|
_basedir=$(dirname "$(readlink -f "$0")")
|
||||||
|
}
|
||||||
|
|
||||||
|
_sourceFunctions () {
|
||||||
|
# Get the location of this file
|
||||||
|
_getBaseDir
|
||||||
|
# Go up two directories
|
||||||
|
ff="${_basedir%/*/*}/functions"
|
||||||
|
# Source functions file
|
||||||
|
if [[ -f "$ff" ]]; then
|
||||||
|
source "$ff"
|
||||||
|
else
|
||||||
|
echo "Cannot find functions file: ${ff}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_sourceFunctions
|
||||||
podmanRunWrapper "$@"
|
podmanRunWrapper "$@"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user