ohmyzsh/plugins/kafka/generate.zsh
2018-08-29 17:50:11 +02:00

95 lines
2.4 KiB
Bash
Executable file

#!/bin/zsh
OUT=kafka.zsh
COMMANDS=("kafka-acls" "kafka-avro-console-consumer" "kafka-avro-console-producer"
"kafka-broker-api-versions" "kafka-configs" "kafka-console-consumer"
"kafka-console-producer" "kafka-consumer-groups" "kafka-consumer-perf-test"
"kafka-delegation-tokens" "kafka-topics" "kafka-producer-perf-test"
"kafka-dump-log" "kafka-log-dirs" "kafka-verifiable-consumer"
"kafka-verifiable-producer" "kafka-streams-application-reset"
"kafka-mirror-maker" "kafka-delete-records" "replicator"
)
function kafka_retrieve_help_command() {
cmd=$1
option=""
desc=""
help_output=`$cmd 2>&1`
arg_name="$(echo $cmd | tr - _)_args"
start_desc_column=`echo $help_output | grep Description | head -n 1`
# If a "Description" column is present
# look for the offset to truncate the
# description of the options.
#
# This as some tools usage use a table with 2
# column with the format
# Option Description
# --blbla this is
# useful!
if [[ ! -z $start_desc_column ]]; then
searchstring="Description"
rest=${start_desc_column##*$searchstring}
start_desc_column=$(( ${#start_desc_column} - ${#rest} - ${#searchstring} ))
else
start_desc_column=1
fi
echo "declare -a $arg_name" >> $OUT
echo "$arg_name=()" >> $OUT
# Iterate over each line to check for options
# after check the iteration, truncate over the
# offset and iterate word by word to build the
# description
IFS=$'\n'
for line in `echo $help_output`; do
if [[ $line =~ "^[ \t]*--[a-z][a-z\-\.]+" ]]; then
if [ ! -z $option ]; then
echo "$arg_name+=('$option:${desc//\'/''}')" >> $OUT
fi
option=`echo $line | sed -E 's/^\s*(--[a-z\.\\\-]+).*$/\1/'`
desc=""
fi
IFS=" "
for word in `echo $line | cut -c $start_desc_column-`; do
desc="$desc $word"
done
IFS=$'\n'
done
unset IFS
if [ ! -z $option ]; then
echo "$arg_name+=('$option:${desc//\'/''}')" >> $OUT
fi
}
function kafka-command() {
cmd=$1
echo "compdef \"_kafka-command $cmd\" $cmd" >> $OUT
}
cat << EOF > $OUT
#!/bin/sh
#
# DISCLAIMER: THIS FILE HAS BEEN AUTOMATICALLY GENERATED
# PLEASE DO NOT TOUCH!!!
# IF YOU NEED TO DO ANY MODIFICATION, EDIT GENERATE.ZSH
# FOR MORE INFORMATION https://github.com/Dabz/kafka-zsh-completions
#
function _kafka-command() {
cmd=\$1
arg_name="\$(echo \$cmd | tr - _)_args"
typeset -a options
eval _describe 'values' options -- \$arg_name
}
EOF
for cmd in ${COMMANDS[@]}; do
kafka_retrieve_help_command $cmd
kafka-command $cmd
done