Created
January 17, 2020 05:32
-
-
Save mathew-fleisch/654f52688de4a74ae8786754a9578469 to your computer and use it in GitHub Desktop.
Implement Queue
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
# sqs (unit test) | |
# input: {"id": "data1"} | |
# push, pop, mark_complete, mark_pending | |
QUEUE="./queue.txt" | |
# echo "{\"id\":\"data1\"}" > queue.txt | |
# cat queue.txt | |
mypush() { | |
if [ -z "$1" ]; then | |
echo "Must pass value" | |
exit 1 | |
fi | |
echo "$1" >> "pending,$QUEUE" | |
} | |
mark_inprogress() { | |
COUNT=$(cat "$QUEUE" | wc -l) | |
COUNT=$((COUNT-1)) | |
POP_VALUE=$(cat "$QUEUE" | grep -n 'pending,' | head -1) | |
INDEX=$(echo "$POP_VALUE" | awk -F: '{print $1}') | |
# echo "Line Number: $POP_VALUE" | |
echo "$(cat $QUEUE | sed ${INDEX}'s/pending/in_progress/g')" > "$QUEUE" | |
# > "$QUEUE" | |
echo "$POP_VALUE" | |
} | |
mark_complete() { | |
COUNT=$(cat "$QUEUE" | wc -l) | |
COUNT=$((COUNT-1)) | |
POP_VALUE=$(cat "$QUEUE" | grep -n 'in_progress,' | head -1) | |
INDEX=$(echo "$POP_VALUE" | awk -F: '{print $1}') | |
# echo "Line Number: $POP_VALUE" | |
echo "$(cat $QUEUE | sed ${INDEX}'s/in_progress/complete/g')" > "$QUEUE" | |
# > "$QUEUE" | |
echo "$POP_VALUE" | |
} | |
while [[ $# -gt 0 ]] && [[ "$1" == "--"* ]]; do | |
opt="$1"; | |
shift; | |
case "$opt" in | |
"--" ) break 2;; | |
"--action" ) | |
ACTION="$1"; shift;; | |
"--value" ) | |
VALUE="$1"; shift;; | |
*) echo >&2 "Invalid option: $@"; exit 1;; | |
esac | |
done | |
ACTION_FOUND=0 | |
# Action | |
if [ -z "$ACTION" ]; then | |
echo "missing arugment (action)" | |
exit 1 | |
fi | |
if [ "$ACTION" == "push" ]; then | |
# Value | |
if [ -z "$VALUE" ]; then | |
echo "missing arugment (value)" | |
exit 1 | |
fi | |
ACTION_FOUND=1 | |
mypush "$VALUE" | |
exit 0 | |
fi | |
if [ "$ACTION" == "pop" ]; then | |
ACTION_FOUND=1 | |
mark_inprogress "$VALUE" | |
exit 0 | |
fi | |
if [[ $ACTION_FOUND -eq 0 ]]; then | |
echo "Must pass a valid action" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment