Add server response parser
This commit is contained in:
parent
8b6fd587d7
commit
68168b91fe
|
@ -11,3 +11,9 @@ exec {sock}<>/dev/tcp/"$host"/"$port"
|
|||
redis_send() {
|
||||
printf %s "$@" >&"$sock"
|
||||
}
|
||||
|
||||
redis_recv() {
|
||||
if (( $# )); then
|
||||
IFS= read -rt 5 "$1" <&"$sock"
|
||||
fi
|
||||
}
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
parse_res() {
|
||||
local line=${1%$'\r'}
|
||||
|
||||
case $line in
|
||||
+*)
|
||||
parse_simple "$line"
|
||||
;;
|
||||
-*)
|
||||
parse_err "$line"
|
||||
;;
|
||||
:*)
|
||||
parse_int "$line"
|
||||
;;
|
||||
\$*)
|
||||
parse_bulk "$line"
|
||||
;;
|
||||
\**)
|
||||
parse_arr "$line"
|
||||
;;
|
||||
*)
|
||||
parse_other "$line"
|
||||
esac
|
||||
}
|
||||
|
||||
parse_simple() {
|
||||
(( lines_left -= 1 ))
|
||||
printf '%s\n' "${1:1}"
|
||||
}
|
||||
|
||||
parse_int() {
|
||||
(( lines_left -= 1 ))
|
||||
printf '(integer) %d\n' "${1:1}"
|
||||
}
|
||||
|
||||
parse_err() {
|
||||
(( lines_left -= 1 ))
|
||||
printf '(error) %s\n' "${1:1}"
|
||||
}
|
||||
|
||||
parse_bulk() {
|
||||
bulk=1
|
||||
}
|
||||
|
||||
parse_arr() {
|
||||
lines_left=${1:1}
|
||||
arr=1
|
||||
}
|
||||
|
||||
parse_other() {
|
||||
(( lines_left -= 1 ))
|
||||
|
||||
if (( arr )); then
|
||||
printf '%d) ' "$arr"
|
||||
(( arr++ ))
|
||||
fi
|
||||
|
||||
if (( bulk )); then
|
||||
printf '"'
|
||||
fi
|
||||
|
||||
printf %s "$1"
|
||||
|
||||
if (( bulk )); then
|
||||
printf '"'
|
||||
fi
|
||||
|
||||
printf '\n'
|
||||
}
|
|
@ -30,4 +30,11 @@ fi
|
|||
while get_cmd line; do
|
||||
parse_cmd redis_cmd redis_line "$line"
|
||||
redis_send "$redis_line"
|
||||
|
||||
lines_left=1
|
||||
bulk=0 arr=0
|
||||
|
||||
while (( lines_left )) && redis_recv line; do
|
||||
parse_res "$line"
|
||||
done
|
||||
done
|
||||
|
|
Loading…
Reference in New Issue