commit c56d452dc1e7d98f5f0938d9c0e9967c8671f2f8 Author: Nicholas Chambers Date: Sat May 2 14:34:34 2020 -0500 Add code for forming a Redis command diff --git a/lib/cli.sh b/lib/cli.sh new file mode 100644 index 0000000..b7216dd --- /dev/null +++ b/lib/cli.sh @@ -0,0 +1,60 @@ +get_cmd() { + if (( $# )); then + IFS= read -ep "$(build_prompt)" "$1" + fi +} + +parse_cmd() { + local cmd=( ) arg + declare -n serialized=$1 + + route_cmd "$2" + + serialized=*${#cmd[@]}$'\r\n' + serialized+=${cmd[0]^^} + + for arg in "${cmd[@]:1}"; do + serialized+=$arg + done +} + +route_cmd() { + case $1 in + [[:space:]]*) + parse_ws "$1" + ;; + "") + return 0 + ;; + *) + parse_word "$1" + esac +} + +parse_ws() { + local line=$1 + + while (( ${#line} )) && [[ $line = [[:space:]]* ]]; do + line=${line:1} + done + + route_cmd "$line" +} + +parse_word() { + local arg line=$1 + + while (( ${#line} )); do + local next=${line:0:1} + + if [[ $next != [[:space:]] ]]; then + arg+=$next + line=${line:1} + else + break + fi + done + + cmd+=( "\$${#arg}"$'\r\n'"$arg"$'\r\n' ) + route_cmd "$line" +} diff --git a/lib/net.sh b/lib/net.sh new file mode 100644 index 0000000..9baef9a --- /dev/null +++ b/lib/net.sh @@ -0,0 +1,13 @@ +### +# redis sock +### + +exec {sock}<>/dev/tcp/"$host"/"$port" + +### +# net wrappers +### + +redis_send() { + printf %s "$@" >&"$sock" +} diff --git a/lib/util.sh b/lib/util.sh new file mode 100644 index 0000000..86d3810 --- /dev/null +++ b/lib/util.sh @@ -0,0 +1,7 @@ +build_prompt() { + if (( table )); then + printf '%s:%d[%d]> ' "$host" "$port" "$table" + else + printf '%s:%d> ' "$host" "$port" + fi +} diff --git a/redi.sh b/redi.sh new file mode 100644 index 0000000..523c211 --- /dev/null +++ b/redi.sh @@ -0,0 +1,5 @@ +shopt -s nullglob + +for file in "${REDISH_LIB_PATH:-.}"/lib/*.sh; do + . "$file" +done diff --git a/redish-cli b/redish-cli new file mode 100755 index 0000000..e47eafb --- /dev/null +++ b/redish-cli @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +### +# defaults +### + +host=127.0.0.1 +port=6379 +table=0 + +### +# config +### + +if (( $# )); then + . "$1" + shift +fi + +### +# library +### + +. "${REDISH_LIB_PATH:-.}"/redi.sh + +### +# driver +### + +while get_cmd line; do + parse_cmd redis_cmd "$line" + redis_send "$redis_cmd" +done