Skip to content

Instantly share code, notes, and snippets.

@aaronlevin
Last active December 10, 2018 05:14
Show Gist options
  • Save aaronlevin/bd8bbe659d46f22c825e to your computer and use it in GitHub Desktop.
Save aaronlevin/bd8bbe659d46f22c825e to your computer and use it in GitHub Desktop.
Example scaffolding and helpers for bash scripts
#!/bin/bash
###############################################################################
# Helpers #
###############################################################################
# `is_set` accepts a function and checks if a variable is set.
# The function it accepts should generate a string for an error
# message
is_set() {
local msg_func="$1"
for i in "${@:2}"; do
eval echo \${$i:?"$($msg_func $i)"} > /dev/null
done
}
env_not_set_msg() {
local var="$1"
echo "Environment Variable $var is not set (required)."
}
param_not_set_msg() {
local var="$1"
echo "Command line param $var is not set (required)."
}
# Method to check if a program is installed and exit if not.
programs_exist() {
local msg_func="$1"
for i in "${@:2}"; do
eval command -v $i > /dev/null 2>&1 || { $msg_func $i; exit 1; }
done
}
program_not_accessible() {
local var="$1"
echo "Program $var is not installed or accessible (required)." >&2
}
is_empty() {
local var="$1"
[[ -z "$var" ]]
}
###############################################################################
# AWS :: DyanmoDB #
###############################################################################
default_aws_region() {
echo "us-east-1"
}
delete_dynamo_db_table_io() {
local aws_key="$1"
local aws_secret="$2"
local aws_region="$3"
local table_name="$4"
AWS_ACCESS_KEY_ID="$aws_key" AWS_SECRET_ACCESS_KEY="$aws_secret" aws \
dynamodb \
delete-table \
--table-name "$table_name" \
--region "$aws_region"
}
#!/bin/bash
readonly PROGNAME=$(basename $0)
readonly PROGDIR=$(readlink -m $(dirname $0))
readonly ARGS="$@"
# bring in common utilities and variables
source "$PROGDIR/common_util.sh"
usage() {
cat <<- EOF
usage: $PROGNAME
options
Delete a DynamoDB table
OPTIONS:
-k --aws-key AWS key
-r --aws-region AWS region
-s --aws-secret AWS secret
-n --table-name Name of the DynamoDB table to delete
-h --help help!
EOF
}
cmdLine() {
local aws_region_tmp=""
local arg=
for arg
do
local delim=""
case "$arg" in
--aws-key) args="${args}-k ";;
--aws-region) args="${args}-r ";;
--aws-secret) args="${args}-s ";;
--table-name) args="${args}-n ";;
--help) args="${args}-h ";;
*) [[ "${arg:0:1}" == "-" ]] || delim="\""
args="${args}${delim}${arg}${delim} ";;
esac
done
eval set -- $args
while getopts "hk:n:r:s:" OPTION
do
case $OPTION in
h)
usage
exit 0
;;
k)
readonly AWS_KEY=$OPTARG
;;
n)
readonly TABLE_NAME=$OPTARG
;;
r)
aws_region_tmp=$OPTARG
;;
s)
readonly AWS_SECRET=$OPTARG
;;
esac
done
# Set AWS_REGION to a default if it doesn't exist
readonly AWS_REGION=${aws_region_tmp:=$(default_aws_region)}
return 0
}
# Required command line params
required_cmd_line_params_io() {
is_set param_not_set_msg \
"AWS_KEY" \
"AWS_REGION" \
"AWS_SECRET" \
"TABLE_NAME"
}
# Required programs
required_programs_io() {
programs_exist program_not_accessible "aws"
}
main() {
cmdLine $ARGS
required_cmd_line_params_io
required_programs_io
delete_dynamo_db_table_io "$AWS_KEY" \
"$AWS_SECRET" \
"$AWS_REGION" \
"$TABLE_NAME"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment