added seek_confirmation

This commit is contained in:
Nathaniel Landau
2016-12-31 09:38:15 -05:00
parent 176dba21b6
commit 7fb1b3ae0d

View File

@@ -16,11 +16,6 @@ function mainScript() {
} }
function trapCleanup() { function trapCleanup() {
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
echo "" echo ""
# Delete temp files, if any # Delete temp files, if any
if [ -d "${tmpDir}" ] ; then if [ -d "${tmpDir}" ] ; then
@@ -30,11 +25,6 @@ function trapCleanup() {
} }
function safeExit() { function safeExit() {
# safeExit
# -----------------------------------
# Non destructive exit for when script exits naturally.
# Usage: Add this function at the end of every script.
# -----------------------------------
# Delete temp files, if any # Delete temp files, if any
if [ -d "${tmpDir}" ] ; then if [ -d "${tmpDir}" ] ; then
rm -r "${tmpDir}" rm -r "${tmpDir}"
@@ -67,10 +57,6 @@ blue=$(tput setaf 38)
underline=$(tput sgr 0 1) underline=$(tput sgr 0 1)
# Set Temp Directory # Set Temp Directory
# -----------------------------------
# Create temp directory with three random numbers and the process ID
# in the name. This directory is removed automatically at exit.
# -----------------------------------
tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$" tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$"
(umask 077 && mkdir "${tmpDir}") || { (umask 077 && mkdir "${tmpDir}") || {
die "Could not create temporary directory! Exiting." die "Could not create temporary directory! Exiting."
@@ -79,17 +65,11 @@ tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$"
# Logging # Logging
# ----------------------------------- # -----------------------------------
# Log is only used when the '-l' flag is set. # Log is only used when the '-l' flag is set.
#
# To never save a logfile change variable to '/dev/null'
# Save to Desktop use: $HOME/Desktop/${scriptBasename}.log
# Save to standard user log location use: $HOME/Library/Logs/${scriptBasename}.log
# -----------------------------------
logFile="${HOME}/Library/Logs/${scriptBasename}.log" logFile="${HOME}/Library/Logs/${scriptBasename}.log"
# Options and Usage # Options and Usage
# ----------------------------------- # -----------------------------------
# Print usage
usage() { usage() {
echo -n "${scriptName} [OPTION]... [FILE]... echo -n "${scriptName} [OPTION]... [FILE]...
@@ -148,7 +128,7 @@ unset options
# Print help if no arguments were passed. # Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script # Uncomment to force arguments when invoking the script
# ------------------------------------- # -------------------------------------
# [[ $# -eq 0 ]] && set -- "--help" [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff # Read the options and set stuff
while [[ $1 = -?* ]]; do while [[ $1 = -?* ]]; do
@@ -174,12 +154,8 @@ done
args+=("$@") args+=("$@")
# Logging and Colors # Logging & Feedback
# ----------------------------------------------------- # -----------------------------------------------------
# Here we set the colors for our script feedback.
# Example usage: success "sometext"
#------------------------------------------------------
function _alert() { function _alert() {
if [ "${1}" = "error" ]; then local color="${bold}${red}"; fi if [ "${1}" = "error" ]; then local color="${bold}${red}"; fi
if [ "${1}" = "warning" ]; then local color="${red}"; fi if [ "${1}" = "warning" ]; then local color="${red}"; fi
@@ -214,6 +190,40 @@ function input() { local _message="${*}"; echo -n "$(_alert input)"; }
function header() { local _message="== ${*} == "; echo -e "$(_alert header)"; } function header() { local _message="== ${*} == "; echo -e "$(_alert header)"; }
function verbose() { if ${verbose}; then debug "$@"; fi } function verbose() { if ${verbose}; then debug "$@"; fi }
# SEEKING CONFIRMATION
# ------------------------------------------------------
function seek_confirmation() {
# echo ""
input "$@"
if "${force}"; then
notice "Forcing confirmation with '--force' flag set"
else
read -p " (y/n) " -n 1
echo ""
fi
}
function is_confirmed() {
if "${force}"; then
return 0
else
if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
return 0
fi
return 1
fi
}
function is_not_confirmed() {
if "${force}"; then
return 1
else
if [[ "${REPLY}" =~ ^[Nn]$ ]]; then
return 0
fi
return 1
fi
}
# Trap bad exits with your cleanup function # Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM trap trapCleanup EXIT INT TERM