added cli options functions

This commit is contained in:
Nathaniel Landau
2015-01-04 00:50:21 -05:00
parent 8f558f6ccd
commit 5ff40a206c
6 changed files with 175 additions and 100 deletions

57
lib/parseOpts.sh Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
# Parse commandline options
#
# Works in along with the 'Command Line Options' and 'Set Switches' functions
# of many of my scripts
#
# All of this taken whole-cloth from: https://github.com/kvz/bash3boilerplate
#####################################################################
# Translate usage string -> getopts arguments, and set $arg_<flag> defaults
while read line; do
opt="$(echo "${line}" |awk '{print $1}' |sed -e 's#^-##')"
if ! echo "${line}" |egrep '\[.*\]' >/dev/null 2>&1; then
init="0" # it's a flag. init with 0
else
opt="${opt}:" # add : if opt has arg
init="" # it has an arg. init with ""
fi
opts="${opts}${opt}"
varname="arg_${opt:0:1}"
if ! echo "${line}" |egrep '\. Default=' >/dev/null 2>&1; then
eval "${varname}=\"${init}\""
else
match="$(echo "${line}" |sed 's#^.*Default=\(\)#\1#g')"
eval "${varname}=\"${match}\""
fi
done <<< "${usage}"
# Reset in case getopts has been used previously in the shell.
OPTIND=1
# Overwrite $arg_<flag> defaults with the actual CLI options
while getopts "${opts}" opt; do
line="$(echo "${usage}" |grep "\-${opt}")"
[ "${opt}" = "?" ] && help "Invalid use of script: ${@} "
varname="arg_${opt:0:1}"
default="${!varname}"
value="${OPTARG}"
if [ -z "${OPTARG}" ] && [ "${default}" = "0" ]; then
value="1"
fi
eval "${varname}=\"${value}\""
#debug "cli arg ${varname} = ($default) -> ${!varname}"
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift

View File

@@ -27,10 +27,10 @@ function scriptPath() {
# Outputs each line in a variable named $result
# ------------------------------------------------------
function readFile() {
unset $result
unset ${result}
while read result
do
echo $result
echo ${result}
done < "$1"
}
@@ -93,8 +93,8 @@ function pushover() {
# Send to Pushover
PUSHOVERURL="https://api.pushover.net/1/messages.json"
API_KEY="$PUSHOVER_API_KEY"
USER_KEY="$PUSHOVER_USER_KEY"
API_KEY="${PUSHOVER_API_KEY}"
USER_KEY="${PUSHOVER_USER_KEY}"
DEVICE=""
TITLE="${1}"
MESSAGE="${2}"
@@ -299,3 +299,21 @@ function unmountDrive() {
diskutil unmount "$1"
fi
}
# help
# ------------------------------------------------------
# Prints help for a script when invoked from the command
# line. Typically via '-h'. If additional flags or help
# text is available in the script they will be printed
# in the '$usage' variable.
# ------------------------------------------------------
function help () {
echo "" 1>&2
e_bold " ${@}" 1>&2
if [ -n "${usage}" ]; then # print usage information if available
echo " ${usage}" 1>&2
fi
echo "" 1>&2
exit 1
}

View File

@@ -9,12 +9,12 @@
# First we locate this script and populate the $SCRIPTPATH variable
# Doing so allows us to source additional files from this utils file.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
while [ -h "${SOURCE}" ]; do # resolve ${SOURCE} until the file is no longer a symlink
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
SOURCE="$(readlink "${SOURCE}")"
[[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SOURCEPATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCEPATH="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
# Write the list of utility files to be sourced
FILES="
@@ -26,10 +26,10 @@ FILES="
# Source the Utility Files
for file in $FILES
do
if [ -f "$SOURCEPATH/$file" ]; then
source "$SOURCEPATH/$file"
if [ -f "${SOURCE}PATH/${file}" ]; then
source "${SOURCE}PATH/${file}"
else
e_error "$file does not exist. Exiting"
e_error "${file} does not exist. Exiting"
Exit 1
fi
done

View File

@@ -46,7 +46,7 @@ RECIPES=(
moom
nvalt
omnifocus
onepassword
1password
plex-home-theater
qlcolorcode
qlmarkdown

View File

@@ -297,19 +297,19 @@ if is_confirmed; then
/usr/libexec/PlistBuddy -c "Set DesktopViewSettings:IconViewSettings:labelOnBottom false" ~/Library/Preferences/com.apple.finder.plist
e_success "Enable snap-to-grid for icons on the desktop and in other icon views"
/usr/libexec/PlistBuddy -c "Set :DesktopViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set :FK_StandardViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set :StandardViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set DesktopViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set FK_StandardViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set StandardViewSettings:IconViewSettings:arrangeBy grid" ~/Library/Preferences/com.apple.finder.plist
e_success "Increase grid spacing for icons on the desktop and in other icon views"
/usr/libexec/PlistBuddy -c "Set :DesktopViewSettings:IconViewSettings:gridSpacing 100" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set :FK_StandardViewSettings:IconViewSettings:gridSpacing 100" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set :StandardViewSettings:IconViewSettings:gridSpacing 100" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set DesktopViewSettings:IconViewSettings:gridSpacing 100" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set FK_StandardViewSettings:IconViewSettings:gridSpacing 100" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set StandardViewSettings:IconViewSettings:gridSpacing 100" ~/Library/Preferences/com.apple.finder.plist
e_success "Increase the size of icons on the desktop and in other icon views"
/usr/libexec/PlistBuddy -c "Set :DesktopViewSettings:IconViewSettings:iconSize 40" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set :FK_StandardViewSettings:IconViewSettings:iconSize 40" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set :StandardViewSettings:IconViewSettings:iconSize 40" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set DesktopViewSettings:IconViewSettings:iconSize 40" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set FK_StandardViewSettings:IconViewSettings:iconSize 40" ~/Library/Preferences/com.apple.finder.plist
/usr/libexec/PlistBuddy -c "Set StandardViewSettings:IconViewSettings:iconSize 40" ~/Library/Preferences/com.apple.finder.plist
e_success "Use column view in all Finder windows by default"
defaults write com.apple.finder FXPreferredViewStyle -string "clmv"
@@ -464,7 +464,7 @@ if is_confirmed; then
e_success "Make Safaris search banners default to Contains instead of Starts With"
defaults write com.apple.Safari FindOnPageMatchesWordStartsOnly -bool false
Remove useless icons from Safaris bookmarks bar
e_success "Remove useless icons from Safaris bookmarks bar"
defaults write com.apple.Safari ProxiesInBookmarksBar "()"
e_success "Enable the Develop menu and the Web Inspector in Safari"
@@ -689,7 +689,7 @@ fi
###############################################################################
e_header "Running SSD Specific OSX Tweaks"
seek_confirmation "Confirm that you have an SSD Hard Drive and want to "
seek_confirmation "Confirm that you have an SSD Hard Drive and want to disable sudden motion sensor."
if is_confirmed; then
# e_success "Remove the sleep image file to save disk space"

View File

@@ -43,13 +43,13 @@ fi
# This script calls for a configuration file.
# This is its location
CONFIG="../etc/$SCRIPTNAME.cfg"
CONFIG="../etc/${SCRIPTNAME}.cfg"
# HELP
# When -h is passed to the script, this will display inline help
function HELP () {
e_bold "\nHelp for $SCRIPTNAME"
e_bold "\nHelp for ${SCRIPTNAME}"
echo -e "This script will give you the option of using rsync"
echo -e "or Unison. Rsync is for one-way syncing, Unison is for"
echo -e "two-way syncing.\n"
@@ -96,29 +96,29 @@ done
# Create new copy of the script if template is being executed
function newCopy() {
scriptPath
if [ "$SCRIPTNAME" = "SyncTemplate.sh" ]; then
if [ "${SCRIPTNAME}" = "SyncTemplate.sh" ]; then
e_bold "name your new script:"
read newname
cp "$SCRIPTPATH"/"$SCRIPTNAME" "$SCRIPTPATH"/"$newname"
e_success "$newname created."
cp "${SCRIPTPATH}"/"${SCRIPTNAME}" "${SCRIPTPATH}"/"${newname}"
e_success "${newname} created."
exit 0
fi
}
function configFile() {
# Here we source the Config file or create a new one if none exists.
if is_file "$CONFIG"; then
source "$CONFIG"
if is_file "${CONFIG}"; then
source "${CONFIG}"
else
seek_confirmation "Config file does not exist. Would you like to create one?"
if is_not_confirmed; then
die "No config file. Exiting"
else
touch "$CONFIG"
cat >"$CONFIG" <<EOL
touch "${CONFIG}"
cat >"${CONFIG}" <<EOL
# ##################################################
# CONFIG FILE FOR $SCRIPTNAME
# CREATED ON $NOW
# CONFIG FILE FOR ${SCRIPTNAME}
# CREATED ON ${NOW}
#
# Created by version "$VERSION" of "SyncTemplate.sh"
# ##################################################
@@ -207,7 +207,7 @@ PUSHOVERNOTIFY="false"
CANONICALHOST=""
EOL
e_success "Config file created. Edit the values before running this script again."
e_arrow "The file is located at: $CONFIG"
e_arrow "The file is located at: ${CONFIG}"
echo "Exiting."
exit 0
fi
@@ -219,9 +219,9 @@ fi
# Confirm we can run this script. If a canonical host is set in
# the config file we check it here.
function hostCheck() {
if [ "$THISHOST" = "$CANONICALHOST" ]; then
echo "$NOW - Script was not run since we were on the wrong host" >> "$LOGFILE"
die "We are currently on $THISHOST and can not proceed. Be sure to run this script on the non-canonical host."
if [ "${THISHOST}" = "${CANONICALHOST}" ]; then
echo "${NOW} - Script was not run since we were on the wrong host" >> "${LOGFILE}"
die "We are currently on ${THISHOST} and can not proceed. Be sure to run this script on the non-canonical host."
fi
}
@@ -229,8 +229,8 @@ function hostCheck() {
# Confirm we have either Unison or Rsync specified
# in the config file. Exit if not
function MethodCheck() {
if [ "$METHOD" != "rsync" ] && [ "$METHOD" != "unison" ]; then
echo "$NOW - Script aborted without a method specified in the config file." >> "$LOGFILE"
if [ "${METHOD}" != "rsync" ] && [ "${METHOD}" != "unison" ]; then
echo "${NOW} - Script aborted without a method specified in the config file." >> "${LOGFILE}"
die "We can not continue. Please specify a sync method in the config file."
fi
}
@@ -239,27 +239,27 @@ function mainScript() {
# Time the script by logging the start time
STARTTIME=$(date +"%s")
# Log Script Start to $LOGFILE
echo -e "-----------------------------------------------------" >> "$LOGFILE"
echo -e "$NOW - $SCRIPTNAME Begun" >> "$LOGFILE"
echo -e "-----------------------------------------------------\n" >> "$LOGFILE"
# Log Script Start to ${LOGFILE}
echo -e "-----------------------------------------------------" >> "${LOGFILE}"
echo -e "${NOW} - ${SCRIPTNAME} Begun" >> "${LOGFILE}"
echo -e "-----------------------------------------------------\n" >> "${LOGFILE}"
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
if [ "${NEEDMOUNT}" = "true" ] || [ "${NEEDMOUNT}" = "TRUE" ]; then
# Mount AFP volume
if is_not_dir "$REMOTEVOLUME"; then
if is_not_dir "${REMOTEVOLUME}"; then
e_arrow "Mounting drive"
mkdir "$REMOTEVOLUME"
if [ "$MOUTPW" = "true" ]; then # if password prompt needed
mount_afp -i "$MOUNTPOINT" "$REMOTEVOLUME"
mkdir "${REMOTEVOLUME}"
if [ "${MOUTPW}" = "true" ]; then # if password prompt needed
mount_afp -i "${MOUNTPOINT}" "${REMOTEVOLUME}"
else
mount_afp "$MOUNTPOINT" "$REMOTEVOLUME"
mount_afp "${MOUNTPOINT}" "${REMOTEVOLUME}"
fi
sleep 10
echo "$NOW - $REMOTEVOLUME Mounted" >> "$LOGFILE"
e_success "$REMOTEVOLUME Mounted"
echo "${NOW} - ${REMOTEVOLUME} Mounted" >> "${LOGFILE}"
e_success "${REMOTEVOLUME} Mounted"
else
e_success "$REMOTEVOLUME already mounted"
e_success "${REMOTEVOLUME} already mounted"
fi
fi
@@ -267,38 +267,38 @@ function mainScript() {
# If they don't exist we can't continue
# test for target
if is_dir "$TARGETDIRECTORY"; then
e_success "$TARGETDIRECTORY exists"
if is_dir "${TARGETDIRECTORY}"; then
e_success "${TARGETDIRECTORY} exists"
else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
if [ "${NEEDMOUNT}" = "true" ] || [ "${NEEDMOUNT}" = "TRUE" ]; then
unmountDrive "${REMOTEVOLUME}"
if is_dir "${REMOTEVOLUME}"; then
rm -r "${REMOTEVOLUME}"
fi
fi
echo -e "$NOW - Script aborted since target dir: $TARGETDIRECTORY was not found. Exited.\n" >> "$LOGFILE"
die "target directory: $TARGETDIRECTORY does not exist. Exiting."
echo -e "${NOW} - Script aborted since target dir: ${TARGETDIRECTORY} was not found. Exited.\n" >> "${LOGFILE}"
die "target directory: ${TARGETDIRECTORY} does not exist. Exiting."
fi
# Test for source directory
if is_dir "$SOURCEDIRECTORY"; then
e_success "$SOURCEDIRECTORY exists"
if is_dir "${SOURCEDIRECTORY}"; then
e_success "${SOURCEDIRECTORY} exists"
else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
if [ "${NEEDMOUNT}" = "true" ] || [ "${NEEDMOUNT}" = "TRUE" ]; then
unmountDrive "${REMOTEVOLUME}"
if is_dir "${REMOTEVOLUME}"; then
rm -r "${REMOTEVOLUME}"
fi
fi
echo -e "$NOW - Script aborted since source dir: $SOURCEDIRECTORY was not found. Exited.\n" >> "$LOGFILE"
die "source directory: $SOURCEDIRECTORY does not exist. Exiting."
echo -e "${NOW} - Script aborted since source dir: ${SOURCEDIRECTORY} was not found. Exited.\n" >> "${LOGFILE}"
die "source directory: ${SOURCEDIRECTORY} does not exist. Exiting."
fi
# Time to sync
if [ "$METHOD" = "rsync" ]; then
/usr/bin/rsync -vahh"$DRYRUN""$COMPRESS" --progress --force --delete --exclude-from="$EXCLUDE" "$SOURCEDIRECTORY" "$TARGETDIRECTORY" --log-file="$LOGFILE"
if [ "${METHOD}" = "rsync" ]; then
/usr/bin/rsync -vahh"$DRYRUN""$COMPRESS" --progress --force --delete --exclude-from="$EXCLUDE" "${SOURCEDIRECTORY}" "${TARGETDIRECTORY}" --log-file="${LOGFILE}"
fi
if [ "$METHOD" = "unison" ]; then
if [ "${METHOD}" = "unison" ]; then
# Check if Unison is installed. It is not a standard package
if type_not_exists 'unison'; then
@@ -308,10 +308,10 @@ function mainScript() {
brewMaintenance
brew install unison
else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
if [ "${NEEDMOUNT}" = "true" ] || [ "${NEEDMOUNT}" = "TRUE" ]; then
unmountDrive "${REMOTEVOLUME}"
if is_dir "${REMOTEVOLUME}"; then
rm -r "${REMOTEVOLUME}"
fi
fi
die "Can not continue without Unison."
@@ -319,54 +319,54 @@ function mainScript() {
fi
# Run Unison
if [ "$PROFILEROOTS" = "true" ]; then
if [ "${PROFILEROOTS}" = "true" ]; then
# Throw error if we don't have enough information
if [ "$USEPROFILE" = "false" ] || [ "$UNISONPROFILE" = "" ]; then
echo "$NOW - We were missing the Unison Profile. Could not sync." >> "$LOGFILE"
if [ "${USEPROFILE}" = "false" ] || [ "${UNISONPROFILE}" = "" ]; then
echo "${NOW} - We were missing the Unison Profile. Could not sync." >> "${LOGFILE}"
die "We were missing the Unison Profile. Could not sync."
fi
# Run unison with the profile
echo "$NOW - Beginning Unison with command: unison $UNISONPROFILE" >> "$LOGFILE"
unison "$UNISONPROFILE"
echo "${NOW} - Beginning Unison with command: unison ${UNISONPROFILE}" >> "${LOGFILE}"
unison "${UNISONPROFILE}"
else
if [ "$USEPROFILE" = "true" ]; then
if [ "${USEPROFILE}" = "true" ]; then
# Throw error if we can't find the profile
if [ "$UNISONPROFILE" = "" ]; then
echo "$NOW - We were missing the Unison Profile. Could not sync." >> "$LOGFILE"
if [ "${UNISONPROFILE}" = "" ]; then
echo "${NOW} - We were missing the Unison Profile. Could not sync." >> "${LOGFILE}"
die "We were missing the Unison Profile. Could not sync."
fi
# Run unison with a profile
echo "$NOW - Beginning Unison with command: unison $UNISONPROFILE $SOURCEDIRECTORY $TARGETDIRECTORY" >> "$LOGFILE"
unison "$UNISONPROFILE" "$SOURCEDIRECTORY" "$TARGETDIRECTORY"
echo "${NOW} - Beginning Unison with command: unison ${UNISONPROFILE} ${SOURCEDIRECTORY} ${TARGETDIRECTORY}" >> "${LOGFILE}"
unison "${UNISONPROFILE}" "${SOURCEDIRECTORY}" "${TARGETDIRECTORY}"
else
# Run Unison without a profile
echo "$NOW - Beginning Unison with command: unison $SOURCEDIRECTORY $TARGETDIRECTORY" >> "$LOGFILE"
unison "$SOURCEDIRECTORY" "$TARGETDIRECTORY"
echo "${NOW} - Beginning Unison with command: unison ${SOURCEDIRECTORY} ${TARGETDIRECTORY}" >> "${LOGFILE}"
unison "${SOURCEDIRECTORY}" "${TARGETDIRECTORY}"
fi
fi
fi
# Unmount the drive (if mounted)
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
echo "$NOW - $REMOTEVOLUME Unmounted" >> "$LOGFILE"
e_success "$REMOTEVOLUME UnMounted"
if [ "${NEEDMOUNT}" = "true" ] || [ "${NEEDMOUNT}" = "TRUE" ]; then
unmountDrive "${REMOTEVOLUME}"
echo "${NOW} - ${REMOTEVOLUME} Unmounted" >> "${LOGFILE}"
e_success "${REMOTEVOLUME} UnMounted"
fi
# Time the script by logging the end time
ENDTIME=$(date +"%s")
TOTALTIME=$(($ENDTIME-$STARTTIME-20))
TOTALTIME=$((${ENDTIME}-${STARTTIME}-20))
# notify with pushover if requested
if [ "$PUSHOVERNOTIFY" = "true" ]; then
pushover "$SCRIPTNAME Completed" "$SCRIPTNAME was run in $(convertsecs $TOTALTIME)"
if [ "${PUSHOVERNOTIFY}" = "true" ]; then
pushover "${SCRIPTNAME} Completed" "${SCRIPTNAME} was run in $(convertsecs $TOTALTIME)"
fi
echo -e "\n-----------------------------------------------------" >> "$LOGFILE"
echo "$NOW - $SCRIPTNAME completed in $(convertsecs $TOTALTIME)" >> "$LOGFILE"
echo -e "-----------------------------------------------------\n" >> "$LOGFILE"
echo -e "\n-----------------------------------------------------" >> "${LOGFILE}"
echo "${NOW} - ${SCRIPTNAME} completed in $(convertsecs $TOTALTIME)" >> "${LOGFILE}"
echo -e "-----------------------------------------------------\n" >> "${LOGFILE}"
e_success "$NOW - $SCRIPTNAME completed in $(convertsecs $TOTALTIME)"
e_success "${NOW} - ${SCRIPTNAME} completed in $(convertsecs $TOTALTIME)"
}
newCopy