Skip to content

Instantly share code, notes, and snippets.

@mflorida
Created July 20, 2017 12:57
Show Gist options
  • Save mflorida/108e172856fb68c9d7c6fe00a21d6447 to your computer and use it in GitHub Desktop.
Save mflorida/108e172856fb68c9d7c6fe00a21d6447 to your computer and use it in GitHub Desktop.
Parse shell script arguments but use variables instead of a 'command' string - accepted in any order but executed in a specific order.
#!/usr/bin/env bash
# This script will iterate through the arguments passed to it and collect
# a list of commands and variables to execute in a specified order.
# It differs from the 'parse-args.sh' script in that it sets variables
# instead of building up a string of commands.
# Example usage:
# ===================================================================================
# ./parse-args-vars.sh -ktdbyr --name=foo
# ...or...
# ./parse-args-vars.sh --name=foo --backup --reset --delete --build --deploy --restart
# -----------------------------------------------------------------------------------
# backs up the app, deletes the contents of the data folders, resets the database,
# rebuilds and deploys the app
OWD=`dirname $0`
SCRIPT=${BASH_SOURCE[0]}
# at least one argument is required
[[ $# == 0 ]] && echo "Command not specified. Example usage: '${SCRIPT} --rebuild'" && exit 1;
bold=$(tput bold)
normal=$(tput sgr0)
# disable verbosity by default
VERBOSE=false
# save app name for use later
APPNAME="default"
# save path to app dir for use later
APPDIR="/app/default"
helpmenu() {
echo ""
echo "================================================================================"
echo ""
echo "$ ${bold}parse-args${normal} [options]"
echo ""
echo " -h | --help - displays available flags with brief descriptions "
echo " (no other flags will be processed) "
echo " -v | --verbose - enable verbose output "
echo " -k | --backup - backup 'app' to '/backups' folder "
echo " -d | --delete - delete file data "
echo " -t | --reset - reset database "
echo " --name=x - name of the 'app' we're working with "
echo " --path=x - path for the 'app' we're working with "
echo " --dir=x - [alias to --path] "
echo " -b | --build - build the 'app' "
echo " -y | --deploy - deploy the 'app' "
echo " -r | --restart - restart the 'app' process(es) "
echo " -R | --rebuild - backup, delete, reset, build, deploy, restart "
echo ""
echo "================================================================================"
echo ""
exit 0
}
backup(){
echo "Backing up..."
}
delete(){
[[ -z $1 ]] && echo "Deleting stuff..." || echo "Deleting '${1}'..."
}
reset(){
echo "Resetting the database..."
}
path(){
echo "Path set to '${APPDIR}'..."
}
build(){
echo "Building '${APPNAME}' app..."
}
deploy(){
echo "Deploying '${APPNAME}' app..."
}
restart(){
echo "Restarting '${APPNAME}' app..."
}
# ===================================================================================
# setup command functions for execution in the proper order
# save list of flags
FLAGS=""
# iterate through the arguments
while [ $# -gt 0 ]; do
THEARG=$1
# look at single-character arguments that start with a SINGLE hyphen
if [[ ! $1 =~ ^-- ]] && [[ $1 =~ ^- ]]; then
FLAGS+="$1 "
[[ $1 =~ h ]] && helpmenu && exit 0
[[ $1 =~ v ]] && VERBOSE=true
[[ $1 =~ k ]] && X_BACKUP=true
[[ $1 =~ d ]] && X_DELETE=true
[[ $1 =~ t ]] && X_RESET=true
[[ $1 =~ b ]] && X_BUILD=true
[[ $1 =~ y ]] && X_DEPLOY=true
[[ $1 =~ r ]] && X_RESTART=true
[[ $1 =~ R ]] && X_REBUILD=true
#[[ ! $1 =~ -[hvkdtbyrR] ]] && { echo ""; echo "Error: Invalid option(s) in '-${1##*-}'"; }
#[[ $1 =~ R ]] && COMMANDS+=":REBUILD::stop::backup::delete::reset::pull::build::deploy::restart:"
# now look at other arguments and variables
else
FLAGS+="$1 "
# remove '--' from beginning, then flags can be
# parsed with or without a leading '--'
THEARG=${THEARG##*--}
# extract argument value after '='
THEVAL=${THEARG##*=}
case "${THEARG}" in
help)
helpmenu
exit 0
;;
verbose)
VERBOSE=true
;;
name=*|app=*)
X_NAME=true
APPNAME=${THEVAL}
;;
path=*|dir=*)
X_PATH=true
APPDIR=${THEVAL}
;;
backup)
X_BACKUP=true
;;
delete)
X_DELETE=true
;;
reset)
X_RESET=true
;;
build)
X_BUILD=true
;;
deploy)
X_DEPLOY=true
;;
restart)
X_RESTART=true
;;
rebuild)
X_REBUILD=true
;;
*)
echo ""
echo "Error: Invalid option '$1'"
;;
esac
fi
# next arg please
shift
done
echo ""
# print command string if $VERBOSE == true
# it doesn't matter if commands are repeated in
# the string - they'll still only be executed once
[[ ${VERBOSE} == true ]] && { echo "FLAGS = '${FLAGS}'"; echo ""; }
# setup variables that were passed in arguments
[[ ! -z ${X_NAME} ]] && echo "App name is '${APPNAME}'"
[[ ! -z ${X_PATH} ]] && path
# get confirmation for a destructive operation
if [[ ! -z ${X_REBUILD} ]]; then
read -p "Are you sure you'd like to rebuild and deploy your app? [y/N] " CHOICE
echo ""
[[ ${CHOICE} =~ [Yy] ]] \
&& { echo "Proceeding with rebuild..."; echo ""; } \
|| { echo "Exiting without rebuilding."; echo ""; exit 1; }
# assign individual commands needed for rebuild
X_BACKUP=true
X_DELETE=true
X_RESET=true
X_BUILD=true
X_DEPLOY=true
X_RESTART=true
fi
# ===================================================================================
# test commands and execute in proper order
[[ ! -z ${X_BACKUP} ]] && backup
[[ ! -z ${X_DELETE} ]] && delete ${APPNAME}
[[ ! -z ${X_RESET} ]] && reset
[[ ! -z ${X_BUILD} ]] && build
[[ ! -z ${X_DEPLOY} ]] && deploy
# restart last
[[ ! -z ${X_RESTART} ]] && restart
echo "Done."
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment