Skip to content

Instantly share code, notes, and snippets.

@HashanCP
Last active April 2, 2021 13:33
Show Gist options
  • Save HashanCP/b993b0a2cc97b15dabe864a6cfa077b0 to your computer and use it in GitHub Desktop.
Save HashanCP/b993b0a2cc97b15dabe864a6cfa077b0 to your computer and use it in GitHub Desktop.
a PoC cli interface for restapify
#!/bin/bash
usage() { echo "Usage: $0 [-m] [-r] [-s 2xx,5xx] [-e 4xx]" 1>&2; exit 1; }
generateMinimalRoutes() {
touch "$1/_.POST.json"
touch "$1/_.DELETE.json"
}
generateResourcefulRoutes() {
touch "$1/_.POST.json"
touch "$1/_.PUT.json"
touch "$1/_.PATCH.json"
touch "$1/_.DELETE.json"
}
generateMoreRoutes() {
case $1 in
"minimal")
generateMinimalRoutes $2
;;
"resource")
generateResourcefulRoutes $2
;;
*)
;;
esac
}
generateRoute() {
baseRoute=$(echo $1 | cut -d "/" -f 1)
routeParam=$(echo $1 | cut -d "/" -s -f 2 | cut -d ":" -s -f 2)
if [ -z $routeParam ]; then
mkdir -p "$baseRoute"
touch "$baseRoute/_.GET.json"
if [ ! -z $2 ]; then
touch "$baseRoute/_.POST.json"
fi
else
mkdir -p "$baseRoute/[$routeParam]"
touch "$baseRoute/[$routeParam]/_.GET.json"
if [ ! -z $2 ]; then
generateMoreRoutes $2 "$baseRoute/[$routeParam]"
fi
fi
}
declare -A minimalStateMap
minimalStateMap[GET]="400:BAD_REQUEST"
minimalStateMap[POST]="201:CREATED 400:BAD_REQUEST"
minimalStateMap[PUT]="400:BAD_REQUEST 409:CONFLICT"
minimalStateMap[PATCH]="400:BAD_REQUEST 404:NOT_FOUND"
minimalStateMap[DELETE]="404:NOT_FOUND"
declare -A extendedStateMap
extendedStateMap[GET]="400:BAD_REQUEST 401:UNAUTHORIZED 500:INTERNAL_SERVER_ERROR"
extendedStateMap[POST]="201:CREATED 400:BAD_REQUEST 401:UNAUTHORIZED 403:FORBIDDEN 409:CONFLICT 500:INTERNAL_SERVER_ERROR"
extendedStateMap[PUT]="204:NO_CONTENT 400:BAD_REQUEST 401:UNAUTHORIZED 403:FORBIDDEN 409:CONFLICT 500:INTERNAL_SERVER_ERROR"
extendedStateMap[PATCH]="204:NO_CONTENT 400:BAD_REQUEST 401:UNAUTHORIZED 403:FORBIDDEN 404:NOT_FOUND 500:INTERNAL_SERVER_ERROR"
extendedStateMap[DELETE]="204:NO_CONTENT 401:UNAUTHORIZED 403:FORBIDDEN 404:NOT_FOUND 409:CONFLICT 500:INTERNAL_SERVER_ERROR"
HTTP_METHODS=(GET POST PUT PATCH DELETE)
generateStates() {
baseRoute=$(echo $1 | cut -d "/" -f 1)
routeParam=$(echo $1 | cut -d "/" -s -f 2 | cut -d ":" -s -f 2)
if [ -z $routeParam ]; then
for httpMethod in "${HTTP_METHODS[@]}"; do
if [ -f "$baseRoute/_.$httpMethod.json" ]; then
states=(${minimalStateMap[$httpMethod]// / })
for state in "${states[@]}"; do
CODE=$(echo $state | cut -d ":" -f 1)
STATE=$(echo $state | cut -d ":" -f 2)
touch "$baseRoute/_.$httpMethod.$CODE.{$STATE}.json"
done
fi
done
else
for httpMethod in "${HTTP_METHODS[@]}"; do
if [ -f "$baseRoute/[$routeParam]/_.$httpMethod.json" ]; then
states=(${minimalStateMap[$httpMethod]// / })
for state in "${states[@]}"; do
CODE=$(echo $state | cut -d ":" -f 1)
STATE=$(echo $state | cut -d ":" -f 2)
touch "$baseRoute/[$routeParam]/_.$httpMethod.$CODE.{$STATE}.json"
done
fi
done
fi
}
generateExtendedStates() {
baseRoute=$(echo $1 | cut -d "/" -f 1)
routeParam=$(echo $1 | cut -d "/" -s -f 2 | cut -d ":" -s -f 2)
if [ -z $routeParam ]; then
for httpMethod in "${HTTP_METHODS[@]}"; do
echo $httpMethod
if [ -f "$baseRoute/_.$httpMethod.json" ]; then
states=(${extendedStateMap[$httpMethod]// / })
echo "${states[@]}"
for state in "${states[@]}"; do
CODE=$(echo $state | cut -d ":" -f 1)
STATE=$(echo $state | cut -d ":" -f 2)
touch "$baseRoute/_.$httpMethod.$CODE.{$STATE}.json"
done
fi
done
else
for httpMethod in "${HTTP_METHODS[@]}"; do
echo $httpMethod
if [ -f "$baseRoute/[$routeParam]/_.$httpMethod.json" ]; then
states=(${extendedStateMap[$httpMethod]// / })
for state in "${states[@]}"; do
CODE=$(echo $state | cut -d ":" -f 1)
STATE=$(echo $state | cut -d ":" -f 2)
touch "$baseRoute/[$routeParam]/_.$httpMethod.$CODE.{$STATE}.json"
done
fi
done
fi
}
case "$1" in
"generate")
generateRoute $2 ""
;;
"resource")
generateRoute "$2/:id" "resource"
generateRoute $2 "resource"
;;
"help")
usage
;;
esac
RESOURCE=$2
shift $((OPTION+2))
while getopts "mrs:e:" o; do
case "${o}" in
m)
generateRoute $RESOURCE "minimal"
;;
r)
generateRoute $RESOURCE "resource"
;;
s)
generateStates $RESOURCE ${OPTARG}
;;
e)
generateExtendedStates $RESOURCE ${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
tree $pwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment