Last active
August 29, 2024 10:31
-
-
Save thiago-rezende/4ceeca70c01a8895d6ed6ed5f249fde6 to your computer and use it in GitHub Desktop.
Development Environment Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ██░ ██ ▒█████ ██▀███ █ ██ ██████ | |
# ▓██░ ██▒▒██▒ ██▒▓██ ▒ ██▒ ██ ▓██▒▒██ ▒ | |
# ▒██▀▀██░▒██░ ██▒▓██ ░▄█ ▒▓██ ▒██░░ ▓██▄ | |
# ░▓█ ░██ ▒██ ██░▒██▀▀█▄ ▓▓█ ░██░ ▒ ██▒ | |
# ░▓█▒░██▓░ ████▓▒░░██▓ ▒██▒▒▒█████▓ ▒██████▒▒ | |
# ▒ ░░▒░▒░ ▒░▒░▒░ ░ ▒▓ ░▒▓░░▒▓▒ ▒ ▒ ▒ ▒▓▒ ▒ ░ | |
# ▒ ░▒░ ░ ░ ▒ ▒░ ░▒ ░ ▒░░░▒░ ░ ░ ░ ░▒ ░ ░ | |
# ░ ░░ ░░ ░ ░ ▒ ░░ ░ ░░░ ░ ░ ░ ░ ░ | |
# ░ ░ ░ ░ ░ ░ ░ ░ | |
# | |
# Filename: dev-helper.sh | |
# Maintainer: Thiago Rezende <thiago.manoel.rezende@gmail.com> | |
# ANSI colors | |
declare -r \ | |
ansi_black='\033[30m' \ | |
ansi_black_bold='\033[0;30;1m' \ | |
ansi_red='\033[31m' \ | |
ansi_red_bold='\033[0;31;1m' \ | |
ansi_green='\033[32m' \ | |
ansi_green_bold='\033[0;32;1m' \ | |
ansi_yellow='\033[33m' \ | |
ansi_yellow_bold='\033[0;33;1m' \ | |
ansi_blue='\033[34m' \ | |
ansi_blue_bold='\033[0;34;1m' \ | |
ansi_magenta='\033[35m' \ | |
ansi_magenta_bold='\033[0;35;1m' \ | |
ansi_cyan='\033[36m' \ | |
ansi_cyan_bold='\033[0;36;1m' \ | |
ansi_white='\033[37m' \ | |
ansi_white_bold='\033[0;37;1m' \ | |
ansi_reset='\033[0m' | |
declare -r ansi_grey="$ansi_black_bold" | |
# script file name | |
script_name=`basename "$0"` | |
# script verison | |
version="v1.4.1" | |
# script global variables | |
positional_args=() | |
# aws global variables | |
container=$(docker ps -a | grep localstack-main | grep -oE --max-count=1 '[^ ]+$') | |
endpoint="http://localhost:4566" | |
region="us-east-1" | |
output="json" | |
# usage message | |
usage() { | |
echo -e "[$ansi_green_bold $script_name $ansi_reset]$ansi_white_bold <$ansi_yellow_bold $version$ansi_white_bold > $ansi_reset" | |
echo -e " $ansi_blue_bold Development Environment Helper $ansi_reset" | |
echo -e "" | |
echo -e "[$ansi_white_bold usage $ansi_reset]" | |
echo -e " $ansi_green_bold $script_name $ansi_white_bold utility $ansi_yellow_bold command $ansi_magenta_bold < argument > $ansi_cyan_bold [ options ] $ansi_reset" | |
echo -e "" | |
echo -e "[$ansi_white_bold utilities $ansi_reset]" | |
echo -e " $ansi_white_bold help $ansi_reset | show this help message" | |
echo -e " $ansi_white_bold sqs $ansi_reset | sqs utility for$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_white_bold s3 $ansi_reset | s3 utility for$ansi_white_bold localstack $ansi_reset" | |
echo -e "" | |
echo -e "[$ansi_white_bold sqs $ansi_reset]" | |
echo -e " $ansi_yellow_bold messages $ansi_magenta_bold < queue > $ansi_reset | list the messages in a queue on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold inspect $ansi_magenta_bold < queue > $ansi_reset | inspect the attributes of a queue on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold create $ansi_magenta_bold < queue > [ ... < queue > ] $ansi_reset | create a queue or list of queues on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold delete $ansi_magenta_bold < queue > [ ... < queue > ] $ansi_reset | delete a queue or list of queues on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold flush $ansi_magenta_bold < queue > [ ... < queue > ] $ansi_reset | flush a queue or list of queues on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold list $ansi_reset | list the queues on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold send $ansi_magenta_bold < queue > < message | file > $ansi_reset | send a message to a queue on$ansi_white_bold localstack $ansi_reset" | |
echo -e "" | |
echo -e "[$ansi_white_bold s3 $ansi_reset]" | |
echo -e " $ansi_yellow_bold notification $ansi_magenta_bold < bucket > [ ... < queue[:prefix] > $ansi_reset | enable or disable sqs notifications from a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold download $ansi_magenta_bold < bucket > < key > < file > $ansi_reset | download a file from a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold create $ansi_magenta_bold < bucket > $ansi_reset | create a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold delete $ansi_magenta_bold < bucket > $ansi_reset | delte a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold remove $ansi_magenta_bold < bucket > < key > $ansi_reset | remove a file from a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold upload $ansi_magenta_bold < bucket > < key > < file > $ansi_reset | upload a file to a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold clear $ansi_magenta_bold < bucket > $ansi_reset | clear the contents of a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold files $ansi_magenta_bold < bucket > $ansi_reset | list the files from a bucket on$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_yellow_bold list $ansi_magenta_bold < bucket > $ansi_reset | list the buckets on$ansi_white_bold localstack $ansi_reset" | |
echo -e "" | |
echo -e "[$ansi_white_bold options $ansi_reset]" | |
echo -e " $ansi_white_bold --container $ansi_reset| define container name for$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_white_bold --endpoint $ansi_reset | define aws endpoint for$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_white_bold --region $ansi_reset | define aws region for$ansi_white_bold localstack $ansi_reset" | |
echo -e " $ansi_white_bold --output $ansi_reset | define aws output for$ansi_white_bold localstack $ansi_reset" | |
echo -e "" | |
echo -e "[$ansi_white_bold management $ansi_reset]" | |
echo -e " $ansi_white_bold uninstall $ansi_reset | uninstall this script and remove it from$ansi_white_bold PATH $ansi_reset" | |
echo -e " $ansi_white_bold install $ansi_reset | install this script and make it available on$ansi_white_bold PATH $ansi_reset" | |
echo -e " $ansi_white_bold update $ansi_reset | update this script with the latest version from$ansi_white_bold GitHub Gist $ansi_reset" | |
echo -e "" | |
exit 0 | |
} | |
# invalid argument message | |
invalid_argument() { | |
echo -e "[$ansi_green_bold $script_name $ansi_reset] <$ansi_red_bold error $ansi_reset> invalid argument" \ | |
"$(if [ ! -z "$1" ]; then echo -e \'$ansi_magenta_bold $1 $ansi_reset\'; fi)" | |
echo -e "|> use '$ansi_yellow_bold $script_name help $ansi_reset' to check the available arguments" | |
exit 1 | |
} | |
# sqs handler | |
sqs_handler() { | |
case $1 in | |
messages) sqs_messages "${@:2}";; | |
inspect) sqs_inspect "${@:2}";; | |
create) sqs_create "${@:2}";; | |
delete) sqs_delete "${@:2}";; | |
flush) sqs_flush "${@:2}";; | |
list) sqs_list "${@:2}";; | |
send) sqs_send "${@:2}";; | |
*) invalid_argument $1;; | |
esac | |
} | |
sqs_messages() { | |
if [ -z $1 ]; then | |
invalid_argument "missing queue name" | |
fi | |
local queue=$1 | |
local command="aws sqs receive-message --max-number-of-messages 10 --visibility-timeout 0 --attribute-names All --queue-url $endpoint/000000000000/$queue --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
sqs_inspect() { | |
if [ -z $1 ]; then | |
invalid_argument "missing queue name" | |
fi | |
local queue=$1 | |
local command="aws sqs get-queue-attributes --attribute-names All --queue-url $endpoint/000000000000/$queue --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
sqs_create() { | |
if [ -z $1 ]; then | |
invalid_argument "missing queue name" | |
fi | |
for queue in "$@"; do | |
local attributes=() | |
if [[ $queue =~ \.fifo$ ]]; then | |
attributes+=("FifoQueue=true,ContentBasedDeduplication=true") | |
fi | |
if [[ -z $attributes ]]; then | |
local command="aws sqs create-queue --queue-name $queue --endpoint-url $endpoint" | |
else | |
local command="aws sqs create-queue --queue-name $queue --endpoint-url $endpoint --attributes ${attributes[@]}" | |
fi | |
docker exec $container /bin/bash -c "$command" | |
done; | |
} | |
sqs_delete() { | |
if [ -z $1 ]; then | |
invalid_argument "missing queue name" | |
fi | |
for queue in "$@"; do | |
local command="aws sqs delete-queue --queue-url $endpoint/000000000000/$queue --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
done; | |
} | |
sqs_flush() { | |
if [ -z $1 ]; then | |
invalid_argument "missing queue name" | |
fi | |
for queue in "$@"; do | |
local command="aws sqs purge-queue --queue-url $endpoint/000000000000/$queue --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
done; | |
} | |
sqs_list() { | |
local command="aws sqs list-queues --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
sqs_send() { | |
if [ -z $1 ]; then | |
invalid_argument "missing queue name" | |
fi | |
if [ -z "$2" ]; then | |
invalid_argument "missing message" | |
fi | |
local queue=$1 | |
local message=$2 | |
if [ -f "$2" ]; then | |
local file_path=$2 | |
local file_name=$(basename $file_path) | |
docker cp $file_path $container:/tmp/$file_name | |
message="file:///tmp/$file_name" | |
fi | |
local command="aws sqs send-message --queue-url $endpoint/000000000000/$queue --message-body '$message' --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
# s3 handler | |
s3_handler() { | |
case $1 in | |
notification) s3_notification "${@:2}";; | |
download) s3_download "${@:2}";; | |
create) s3_create "${@:2}";; | |
delete) s3_delete "${@:2}";; | |
remove) s3_remove "${@:2}";; | |
upload) s3_upload "${@:2}";; | |
clear) s3_clear "${@:2}";; | |
files) s3_files "${@:2}";; | |
list) s3_list "${@:2}";; | |
*) invalid_argument $1;; | |
esac | |
} | |
s3_notification() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
local bucket=$1 | |
local config="{}" | |
__build_config() { | |
echo "{ \"QueueConfigurations\": [" | |
local first=true | |
for queue_and_prefix in $@; do | |
local split=(${queue_and_prefix//:/ }) | |
local queue=${split[0]} | |
local prefix=${split[1]} | |
local content="{ | |
\"QueueArn\": \"arn:aws:sqs:$region:000000000000:$queue\", | |
\"Events\": [\"s3:ObjectCreated:*\"]$([ ! -z $prefix ] && echo ", | |
\"Filter\": { | |
\"Key\": { | |
\"FilterRules\": [ | |
{ | |
\"Name\": \"prefix\", | |
\"Value\": \"$prefix\" | |
} | |
] | |
} | |
}") | |
}" | |
if [ $first = false ]; then | |
echo ", " | |
fi | |
echo $content | |
first=false | |
done | |
echo "] }" | |
} | |
if [ ! -z $2 ]; then | |
config=$(__build_config ${@:2}) | |
fi | |
local command="aws s3api put-bucket-notification-configuration --bucket $bucket --notification-configuration '$config' --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
s3_download() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
if [ -z $2 ]; then | |
invalid_argument "missing file key" | |
fi | |
if [ -z $3 ]; then | |
invalid_argument "missing file path" | |
fi | |
local bucket=$1 | |
local file_key=$2 | |
local file_path=$3 | |
local file_name=$(basename $file_path) | |
local command="aws s3api get-object --bucket $bucket --key $file_key /tmp/$file_name --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
if [ $? -eq 0 ]; then | |
docker cp $container:/tmp/$file_name $file_path | |
fi | |
} | |
s3_upload() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
if [ -z $2 ]; then | |
invalid_argument "missing file key" | |
fi | |
if [ -z $3 ]; then | |
invalid_argument "missing file path" | |
fi | |
local bucket=$1 | |
local file_key=$2 | |
local file_path=$3 | |
local file_name=$(basename $file_path) | |
local command="aws s3api put-object --bucket $bucket --body /tmp/$file_name --key $file_key --endpoint-url $endpoint" | |
docker cp $file_path $container:/tmp/$file_name | |
if [ $? -eq 0 ]; then | |
docker exec $container /bin/bash -c "$command" | |
fi | |
} | |
s3_clear() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
local bucket=$1 | |
local command="aws s3 rm s3://$bucket --recursive --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
s3_create() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
local bucket=$1 | |
local command="aws s3api create-bucket --bucket $bucket --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
s3_delete() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
local bucket=$1 | |
s3_clear $bucket | |
local command="aws s3api delete-bucket --bucket $bucket --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
s3_remove() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
if [ -z $2 ]; then | |
invalid_argument "missing file key" | |
fi | |
local bucket=$1 | |
local file_key=$2 | |
local command="aws s3api delete-object --bucket $bucket --key $file_key --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
s3_files() { | |
if [ -z $1 ]; then | |
invalid_argument "missing bucket name" | |
fi | |
local bucket=$1 | |
local command="aws s3api list-objects-v2 --bucket $bucket --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
s3_list() { | |
local command="aws s3api list-buckets --endpoint-url $endpoint" | |
docker exec $container /bin/bash -c "$command" | |
} | |
# aws configure | |
aws_configure() { | |
local config_path=/root/.aws/config | |
local credentials_path=/root/.aws/credentials | |
local local_config_path=/tmp/localstack_config | |
local local_credentials_path=/tmp/localstack_credentials | |
cat <<EOF > $local_config_path | |
[default] | |
region=$region | |
output=$output | |
EOF | |
cat <<EOF > $local_credentials_path | |
[default] | |
aws_access_key_id=localstack_access_key_id | |
aws_secret_access_key=localstack_secret_access_key | |
EOF | |
local command="mkdir -p /root/.aws" | |
docker exec $container /bin/bash -c "$command" | |
if [ $? -eq 0 ]; then | |
docker cp $local_config_path $container:$config_path | |
docker cp $local_credentials_path $container:$credentials_path | |
fi | |
} | |
# installation helper | |
uninstall() { | |
rm -rf ~/.local/bin/dev-helper | |
rm -rf ~/projects/dev-helper | |
exit 0 | |
} | |
install() { | |
mkdir -p ~/projects/dev-helper | |
mkdir -p ~/.local/bin | |
curl -fsSL https://gist.githubusercontent.com/thiago-rezende/4ceeca70c01a8895d6ed6ed5f249fde6/raw/dev-helper.sh?$(date +%s) > ~/projects/dev-helper/dev-helper.sh | |
chmod 755 ~/projects/dev-helper/dev-helper.sh | |
if [ ! -f ~/.local/bin/dev-helper ]; then | |
ln -s ~/projects/dev-helper/dev-helper.sh ~/.local/bin/dev-helper | |
fi | |
exit 0 | |
} | |
update() { | |
if [ ! -f ~/projects/dev-helper/dev-helper.sh ]; then | |
install | |
fi | |
curl -fsSL https://gist.githubusercontent.com/thiago-rezende/4ceeca70c01a8895d6ed6ed5f249fde6/raw/dev-helper.sh?$(date +%s) > ~/projects/dev-helper/dev-helper.sh | |
exit 0 | |
} | |
# options handler | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
# container option | |
--container) container=$2; shift; shift;; | |
--container=*) container="${1#*=}"; shift;; | |
# endpoint option | |
--endpoint) endpoint=$2; shift; shift;; | |
--endpoint=*) endpoint="${1#*=}"; shift;; | |
# output option | |
--output) output=$2; shift; shift;; | |
--output=*) output="${1#*=}"; shift;; | |
# region option | |
--region) region=$2;shift; shift;; | |
--region=*) region="${1#*=}"; shift;; | |
# usage option | |
-h|--help) usage;; | |
# invalid option | |
-*|--*) invalid_argument $1;; | |
# positional arg | |
*) positional_args+=("$1"); shift;; | |
esac | |
done | |
set -- "${positional_args[@]}" | |
# argument handler | |
case $1 in | |
uninstall) uninstall;; | |
install) install;; | |
update) update;; | |
help) usage;; | |
sqs) aws_configure; sqs_handler "${@:2}";; | |
s3) aws_configure; s3_handler "${@:2}";; | |
*) invalid_argument $1;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment