code cleanup

This commit is contained in:
Heavybullets8 2022-06-11 00:13:09 -06:00
parent 9e429e2a5b
commit 45127be73a

View File

@ -4,13 +4,8 @@
[[ -z "$*" || "-" == "$*" || "--" == "$*" ]] && echo "This script requires an argument, use --help for help" && exit [[ -z "$*" || "-" == "$*" || "--" == "$*" ]] && echo "This script requires an argument, use --help for help" && exit
while getopts ":si:rb:t:uUpSRv-:" opt help(){
do [[ $help == "true" ]] && clear -x
case $opt in
-)
case "${OPTARG}" in
help)
clear -x
echo "Basic Utilities" echo "Basic Utilities"
echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data" echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data"
echo "--restore | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset" echo "--restore | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset"
@ -38,74 +33,8 @@ do
echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup" echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup"
echo echo
exit exit
;; }
dns) export -f help
dns="true"
;;
restore)
restore="true"
;;
mount)
mount="true"
;;
delete-backup)
deleteBackup="true"
;;
*)
echo "Invalid Option --$OPTARG, type --help for help"
exit
;;
esac
;;
\?)
echo "Invalid Option -$OPTARG, type --help for help"
exit
;;
:)
echo "Option: -$OPTARG requires an argument" >&2
exit
;;
b)
re='^[0-9]+$'
number_of_backups=$OPTARG
! [[ $OPTARG =~ $re ]] && echo -e "Error: -b needs to be assigned an interger\n\"""$number_of_backups""\" is not an interger" >&2 && exit
[[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit
;;
r)
rollback="true"
;;
i)
ignore+=("$OPTARG")
;;
t)
re='^[0-9]+$'
timeout=$OPTARG
! [[ $timeout =~ $re ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit
;;
s)
sync="true"
;;
U)
update_all_apps="true"
;;
u)
update_apps="true"
;;
S)
stop_before_update="true"
;;
p)
prune="true"
;;
R)
rollback="true"
echo "WARNING: -R is being transisitioned to -r, this is due to a refactor in the script. Please Make the change ASAP!"
;;
v)
verbose="true"
;;
esac
done
deleteBackup(){ deleteBackup(){
@ -126,6 +55,7 @@ else
echo "Invalid Selection" echo "Invalid Selection"
fi fi
} }
export -f deleteBackup
backup(){ backup(){
@ -245,7 +175,7 @@ elif [[ $selection == "2" ]]; then
done done
rmdir /mnt/heavyscript rmdir /mnt/heavyscript
else else
echo "Invalid selection, type -h for help" echo "Invalid selection, \"$selection\" was not an option"
fi fi
} }
export -f mount export -f mount
@ -319,6 +249,7 @@ update_apps(){
} }
export -f update_apps export -f update_apps
after_update_actions(){ after_update_actions(){
SECONDS=0 SECONDS=0
count=0 count=0
@ -384,11 +315,13 @@ fi
} }
export -f after_update_actions export -f after_update_actions
prune(){ prune(){
echo -e "\nPruning Docker Images" && docker image prune -af | grep "^Total" || echo "Failed to Prune Docker Images" echo -e "\nPruning Docker Images" && docker image prune -af | grep "^Total" || echo "Failed to Prune Docker Images"
} }
export -f prune export -f prune
title(){ title(){
echo ' _ _ _____ _ _ ' echo ' _ _ _____ _ _ '
echo '| | | | / ___| (_) | | ' echo '| | | | / ___| (_) | | '
@ -402,10 +335,94 @@ echo
} }
export -f title export -f title
# Parse script options
while getopts ":si:rb:t:uUpSRv-:" opt
do
case $opt in
-)
case "${OPTARG}" in
help)
help="true"
;;
dns)
dns="true"
;;
restore)
restore="true"
;;
mount)
mount="true"
;;
delete-backup)
deleteBackup="true"
;;
*)
echo -e "Invalid Option \"--$OPTARG\"\n" && help
exit
;;
esac
;;
\?)
echo -e "Invalid Option \"-$OPTARG\"\n" && help
exit
;;
:)
echo -e "Option: \"-$OPTARG\" requires an argument\n" && help
exit
;;
b)
re='^[0-9]+$'
number_of_backups=$OPTARG
! [[ $OPTARG =~ $re ]] && echo -e "Error: -b needs to be assigned an interger\n\"""$number_of_backups""\" is not an interger" >&2 && exit
[[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit
;;
r)
rollback="true"
;;
i)
ignore+=("$OPTARG")
;;
t)
re='^[0-9]+$'
timeout=$OPTARG
! [[ $timeout =~ $re ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit
;;
s)
sync="true"
;;
U)
update_all_apps="true"
;;
u)
update_apps="true"
;;
S)
stop_before_update="true"
;;
p)
prune="true"
;;
R)
rollback="true"
echo "WARNING: -R is being transisitioned to -r, this is due to a refactor in the script. Please Make the change ASAP!"
;;
v)
verbose="true"
;;
*)
echo -e "Invalid Option \"--$OPTARG\"\n" && help
exit
;;
esac
done
#exit if incompatable functions are called #exit if incompatable functions are called
[[ "$update_all_apps" == "true" && "$update_apps" == "true" ]] && echo -e "-U and -u cannot BOTH be called" && exit [[ "$update_all_apps" == "true" && "$update_apps" == "true" ]] && echo -e "-U and -u cannot BOTH be called" && exit
#Continue to call functions in specific order #Continue to call functions in specific order
[[ "$help" == "true" ]] && help
[[ "$deleteBackup" == "true" ]] && deleteBackup && exit [[ "$deleteBackup" == "true" ]] && deleteBackup && exit
[[ "$dns" == "true" ]] && dns && exit [[ "$dns" == "true" ]] && dns && exit
[[ "$restore" == "true" ]] && restore && exit [[ "$restore" == "true" ]] && restore && exit