Last active
December 11, 2024 14:45
-
-
Save baztian/0b5b189f35601b1c4b00e0bf0d94a205 to your computer and use it in GitHub Desktop.
Interactive AWS SQS command line tool
This file contains hidden or 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
#!/usr/bin/env bash | |
_sqs_send_completions() | |
{ | |
local cur=${COMP_WORDS[COMP_CWORD]} | |
# Don't consider colon (from url) as word break | |
_get_comp_words_by_ref -n : cur | |
COMPREPLY=( $( compgen -W "$(aws sqs list-queues --query 'QueueUrls' --output text)" -- $cur ) ) | |
__ltrim_colon_completions "$cur" | |
} | |
complete -F _sqs_send_completions sqs-send.sh |
This file contains hidden or 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
#!/usr/bin/env bash | |
set -e | |
show_help() { | |
script=$(basename "$0") | |
echo "Writes message to AWS SQS queue." | |
echo "Usage: $script [-f] [-h] [-i|<queue name>]" | |
echo "-i a dialog will pop up to choose a queue interactively." | |
echo "-f force sending the message without confirmation." | |
echo "-h display this help message." | |
echo "Payload is read from stdin." | |
} | |
send-message() { | |
set -e | |
local queue_url="$1" | |
local payload="$2" | |
local force="$3" | |
[ -t 0 ] || echo "$payload" | |
echo "About to write above payload to queue $queue_url" | |
aws sqs get-queue-attributes --attribute-names QueueArn ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible ApproximateNumberOfMessagesDelayed --query 'Attributes' --queue-url "$queue_url" | |
if [ "$force" = false ]; then | |
read -r -p "Are you sure? [y/N] " < /dev/tty | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
echo canceled > /dev/stderr | |
return 1 | |
fi | |
fi | |
aws sqs send-message --queue-url "$queue_url" --message-body "$payload" | |
} | |
choose_queue_via_dialog() | |
{ | |
set -e | |
queues=$1 | |
i=1 | |
items="" | |
items=$(echo "$queues"|sed -e 's#.*/##g' | while read p; do echo "$i $p" && i=$((i+1));done) | |
TMPFILE=$(mktemp) | |
dialog --keep-tite --stdout --menu "Choose queue" 0 0 0 $items >$TMPFILE | |
idx=$(cat $TMPFILE) | |
rm $TMPFILE | |
echo -n "$queues"|sed -e "${idx}q;d" | |
} | |
choose_queue() | |
{ | |
set -e | |
queues=$(aws sqs list-queues --query 'QueueUrls|[][@]' --output text) | |
set +e | |
echo "$queues"|fzf --layout=reverse-list --preview-window=down:6 \ | |
--preview="aws sqs get-queue-attributes --attribute-names QueueArn ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible ApproximateNumberOfMessagesDelayed --query 'Attributes' --queue-url {}" | |
FZF_ERRORCODE=$? | |
set -e | |
if [ $FZF_ERRORCODE -eq 127 ] ; then | |
echo "Fzf not found. Falling back to dialog." > /dev/stderr | |
choose_queue_via_dialog "$queues" | |
elif [ $FZF_ERRORCODE -ne 0 ]; then | |
exit $FZF_ERRORCODE | |
fi | |
} | |
force=false | |
interactive=false | |
while getopts ":fih" opt; do | |
case ${opt} in | |
f) | |
force=true | |
;; | |
i) | |
interactive=true | |
;; | |
h) | |
show_help | |
exit 0 | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
show_help | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
if [ $# -eq 0 ] && [ "$interactive" = false ]; then | |
show_help | |
exit 1 | |
fi | |
if [ "$interactive" = true ] ; then | |
queue_url=$(choose_queue) | |
else | |
queue_url=$1 | |
shift | |
fi | |
[ -t 0 ] && echo "Your message payload for $queue_url:" | |
payload=$(cat) | |
send-message "$queue_url" "$payload" "$force" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment