Skip to content

Instantly share code, notes, and snippets.

@vikas17a
Last active September 22, 2015 14:09
Show Gist options
  • Save vikas17a/7b04ca2b607fb293f6d3 to your computer and use it in GitHub Desktop.
Save vikas17a/7b04ca2b607fb293f6d3 to your computer and use it in GitHub Desktop.
Enhanced Jsonlite
#!/bin/bash
set -e
VERSION="0.4.0"
COMMAND=$1
CWD=$(pwd);
case "$COMMAND" in
"set")
if [ -z "$2" ]; then
printf "Missing required argument json document"
exit 1;
fi
mkdir -p "$CWD/jsonlite.data"
UUID=`python -c 'import uuid;print(uuid.uuid4())'`
# Piping to python -m json.tool to pretty print json is super expensive.
# What would be a good alternative?
echo "$2" | python -m json.tool > "$CWD/jsonlite.data/$UUID"
echo "$UUID";
;;
"get")
if [ -z "$2" ]; then
printf "Missing required argument document id"
exit 2;
fi
if [ -f "$CWD/jsonlite.data/$2" ]; then
cat "$CWD/jsonlite.data/$2"
fi
;;
"delete")
if [ -z "$2" ]; then
printf "Missing required argument document id"
exit 2;
fi
if [ -f "$CWD/jsonlite.data/$2" ]; then
rm -f "$CWD/jsonlite.data/$2"
fi
;;
"drop")
if [ -d "$CWD/jsonlite.data" ]; then
read -p "Are you sure you want to drop '$CWD/jsonlite.data' (y/n)? " confirm
case "$confirm" in
# Do we need to guard against potentially naughty things here?
y|Y|yes|YES ) rm -rf "$CWD/jsonlite.data";;
* ) exit 4;;
esac
fi
;;
"version")
printf "%s" "$VERSION"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment