81 lines
2.7 KiB
Bash
Executable file
81 lines
2.7 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Configure the following for your installation directory.
|
|
# The rest of the configuration is handled in $INSTALL_DIR/config
|
|
# INSTALL_DIR needs to be a full path. Absolute. Not relative.
|
|
export INSTALL_DIR=$HOME/Scripts
|
|
export CONFIG_FILE=$INSTALL_DIR/config
|
|
|
|
# The following function is used to cleanly exit out in a script.
|
|
# It's not globally available so just copy/pasta it into your other scripts if
|
|
# you want to use it
|
|
failout() {
|
|
[ -z "$MSG" ] || echo $MSG
|
|
cat << EOF
|
|
Usage: $(basename $0) command_group command extra_inputs
|
|
EOF
|
|
exit 11
|
|
}
|
|
|
|
export -f failout
|
|
|
|
# Start out by sourcing the config directory. Sourcing is MUCH faster and more
|
|
# flexible than how the old ASD was handling this.
|
|
# The config file should store all of your "default" environment variables.
|
|
source $CONFIG_FILE
|
|
|
|
|
|
# The following getopts allows some configuration. Namely, it allows you to
|
|
# target specific AWS regions and profiles as needed, as well as set debugging.
|
|
# Instead of getting fancy, we just override the variables
|
|
r=0 # AWS_REGION
|
|
p=0 # AWS_PROFILE
|
|
d=0 # DEBUG
|
|
while getopts ":r:p:d" o; do
|
|
case "${o}" in
|
|
r)
|
|
export AWS_REGION=${OPTARG}
|
|
;;
|
|
p)
|
|
export AWS_PROFILE=${OPTARG}
|
|
;;
|
|
d)
|
|
export ASD_DEBUG=1
|
|
;;
|
|
*)
|
|
MSG="Invalid option detected" failout
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND-1))
|
|
|
|
# Some preflight checks to see if the user is running this in a silly way
|
|
[ -z "$1" ] && MSG="Please provide something for us to run" failout
|
|
|
|
# Now we try to find the script that we want to run. This is somewhat inefficient.
|
|
# It would make more sense to REQUIRE a step one, but I wanted to be able to have something
|
|
# like `asd somescript.sh` because it's just nicer.
|
|
# We used to be able to search a single dir however I have added work scripts
|
|
# and general scripts so that I can keep publicly suitable scripts public and use
|
|
# at home. While work scripts can stay protected away from a public repo.
|
|
# You could totally use submodules in git for this but whatever.
|
|
FIRST_STAGE=$(find $INSTALL_DIR/general-scripts $INSTALL_DIR/work-scripts -mindepth 1 -maxdepth 1 -iname "${1}*" | sort | head -n1)
|
|
[ -z $FIRST_STAGE ] && MSG="Did not find your scripts directory" failout # Die if we find nothing
|
|
[ -f $FIRST_STAGE ] && SS_SKIP=1 || SS_SKIP=0 # Skip second step if we find file
|
|
|
|
# Shift out the first stage
|
|
shift 1
|
|
|
|
# If the first step doesn't skip the second step
|
|
if [ $SS_SKIP == 0 ] ; then
|
|
[ -z "$1" ] && MSG="Please provide a subcommand to run" failout
|
|
SECOND_STAGE=$(find $FIRST_STAGE -mindepth 1 -maxdepth 1 -iname "${1}*" | sort | head -n1)
|
|
[ -z $SECOND_STAGE ] && MSG="Could not find a match for $1" failout # Die if we find nothing
|
|
CMD=$SECOND_STAGE
|
|
shift 1
|
|
else
|
|
CMD=$FIRST_STAGE
|
|
fi
|
|
|
|
# Finally, we need to execute the script that we have found.
|
|
exec $CMD $@
|