Last active
February 27, 2023 15:56
-
-
Save baztian/0b5b189f35601b1c4b00e0bf0d94a205 to your computer and use it in GitHub Desktop.
Interactive AWS SQS command line tool
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
#!/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 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 | |
send-message() { | |
set -e | |
local queue_url="$1" | |
local payload="$2" | |
[ -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" | |
read -r -p "Are you sure? [y/N] " < /dev/tty | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
aws sqs send-message --queue-url "$queue_url" --message-body "$payload" | |
else | |
echo canceled > /dev/stderr | |
fi | |
} | |
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 | |
} | |
if [ $# -eq 0 ] ; then | |
script=$(basename "$0") | |
echo "Writes message to AWS SQS queue." | |
echo "Usage: $script [<queue name>|-i]" | |
echo "-i a dialog will pop up to choose a queue interactively." | |
echo "Payload is read from stdin." | |
exit 1 | |
fi | |
if [ "$1" == "-i" ] ; then | |
queue_url=$(choose_queue) | |
else | |
queue_url=$1 | |
fi | |
[ -t 0 ] && echo "Your message payload for $queue_url:" | |
payload=$(cat) | |
send-message "$queue_url" "$payload" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment