Skip to content

Commit

Permalink
add bash completion script (#74)
Browse files Browse the repository at this point in the history
add bash completion for memtier-benchmark
  • Loading branch information
YaacovHazan authored and yaacovhazan-Redislabs committed Apr 10, 2019
1 parent 891df95 commit 57c8fdc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ m4
Makefile.in
missing
*.o
memtier_benchmark
/memtier_benchmark
.deps
Makefile
config.log
Expand Down
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ AUTOMAKE_OPTIONS = foreign 1.9
EXTRA_DIST = README.md COPYING

bin_PROGRAMS = memtier_benchmark
completionsdir=$(BASH_COMPLETION_DIR)
completions_DATA=bash-completion/memtier_benchmark

memtier_benchmark_CPPFLAGS = $(LIBEVENT_CFLAGS)
memtier_benchmark_SOURCES = \
Expand Down
98 changes: 98 additions & 0 deletions bash-completion/memtier_benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#/usr/bin/env bash

_memtier_look_for_element () {
local op prev="$1" cur="$2"
shift
shift

for op; do
if [[ "${op}" == "${prev}" ]];then
echo "${op}"
break
elif [[ "${cur}" == ${op}=* ]]; then
echo "${op}="
break
fi
done
}

_memtier_completions()
{
options_no_comp=("--server" "--port" "--unix-socket" "--out-file" "--client-stats" "--run-count" "--clients"\
"--requests" "--threads" "--test-time" "--ratio" "--pipeline" "--data-size" "--data-offset"\
"--data-size-range" "--data-size-list" "--expiry-range" "--data-import" "--key-prefix"\
"--key-minimum" "--key-maximum" "--reconnect-interval" "--multi-key-get" "--authenticate"\
"--select-db" "--wait-ratio" "--num-slaves" "--wait-timeout" "--json-out-file" "--command"\
"-s" "-p" "-S" "-o" "-x" "-c" "-n" "-t" "-d" "-a")

options_no_args=("--debug" "--show-config" "--hide-histogram" "--distinct-client-seed" "--randomize"\
"--random-data" "--data-verify" "--verify-only" "--generate-keys" "--key-stddev"\
"--key-median" "--no-expiry" "--cluster-mode" "--help" "--version"\
"-D" "-R" "-h" "-v")

options_comp=("--protocol" "-P" "--key-pattern" "--data-size-pattern")

all_options="${options_no_comp[@]} ${options_no_args[@]} ${options_comp[@]}"

local cur
local prev
_get_comp_words_by_ref -n ${COMP_WORDBREAKS} cur prev

# check if it's option without completion
local option=$(_memtier_look_for_element "${prev}" "" "${options_no_comp[@]}")
if [ -n "${option}" ]; then
option="no completion"
else
# check if it's option with completion
option=$(_memtier_look_for_element "${prev}" "${cur}" "${options_comp[@]}")
fi

case "${option}" in
"no completion")
return
;;
"--protocol=")
cur=${cur#"--protocol="}
;&
"-P")
;&
"--protocol")
all_options="redis memcache_text memcache_binary"
;;
"--data-size-pattern=")
cur=${cur#"--data-size-pattern="}
;&
"--data-size-pattern")
all_options="R S"
;;
"--key-pattern=")
cur=${cur#"--key-pattern="}
;&
"--key-pattern")
if [[ -z "${cur}" ]]; then
COMPREPLY=( $( compgen -W "G R S P" ) )
else
if [[ "${cur}" =~ (G|R|S|P):(G|R|S)$ ]]; then
COMPREPLY="${COMP_WORDS[COMP_CWORD]} "
elif [[ "${cur}" =~ (G|R|S|P):$ ]]; then
COMPREPLY=( $( compgen -W "G R S" ) )
elif [[ "${cur}" =~ (G|R|S|P)$ ]]; then
COMPREPLY="${cur}:"
fi
fi

return
;;
*)
;;
esac

COMPREPLY=( $( compgen -W "${all_options}" -- "${cur}" ) )
if [ -n "${COMPREPLY}" ];then
COMPREPLY="${COMPREPLY} "
fi

return
}

complete -o nospace -F _memtier_completions memtier_benchmark
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ PKG_CHECK_MODULES(LIBEVENT,
AC_SUBST(LIBEVENT_CFLAGS) AC_SUBST(LIBEVENT_LIBS)
)

# bash completion
PKG_CHECK_MODULES([BASH_COMPLETION], [bash-completion >= 2.0],
[BASH_COMPLETION_DIR="`pkg-config --variable=completionsdir bash-completion`"],
[BASH_COMPLETION_DIR="$datadir/bash-completion/completionsss"])
AC_SUBST([BASH_COMPLETION_DIR])

AC_CONFIG_FILES([
Makefile
])
Expand Down

0 comments on commit 57c8fdc

Please sign in to comment.