Add code for forming a Redis command
This commit is contained in:
commit
c56d452dc1
5 changed files with 118 additions and 0 deletions
60
lib/cli.sh
Normal file
60
lib/cli.sh
Normal file
|
@ -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"
|
||||
}
|
13
lib/net.sh
Normal file
13
lib/net.sh
Normal file
|
@ -0,0 +1,13 @@
|
|||
###
|
||||
# redis sock
|
||||
###
|
||||
|
||||
exec {sock}<>/dev/tcp/"$host"/"$port"
|
||||
|
||||
###
|
||||
# net wrappers
|
||||
###
|
||||
|
||||
redis_send() {
|
||||
printf %s "$@" >&"$sock"
|
||||
}
|
7
lib/util.sh
Normal file
7
lib/util.sh
Normal file
|
@ -0,0 +1,7 @@
|
|||
build_prompt() {
|
||||
if (( table )); then
|
||||
printf '%s:%d[%d]> ' "$host" "$port" "$table"
|
||||
else
|
||||
printf '%s:%d> ' "$host" "$port"
|
||||
fi
|
||||
}
|
5
redi.sh
Normal file
5
redi.sh
Normal file
|
@ -0,0 +1,5 @@
|
|||
shopt -s nullglob
|
||||
|
||||
for file in "${REDISH_LIB_PATH:-.}"/lib/*.sh; do
|
||||
. "$file"
|
||||
done
|
33
redish-cli
Executable file
33
redish-cli
Executable file
|
@ -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
|
Loading…
Add table
Reference in a new issue