-
Notifications
You must be signed in to change notification settings - Fork 15
/
kafka.plugin.zsh.generator
executable file
·136 lines (120 loc) · 3.76 KB
/
kafka.plugin.zsh.generator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/zsh
OUT=kafka.plugin.zsh
#COMMANDS=("kafka-topics" "kafka-console-consumer" "kafka-console-producer")
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" "kafka-reassign-partitions"
)
function type_from_option() {
option=$1
if [[ "$option" =~ ".*String.*" ]]; then
if [[ "$option" =~ ".*topic.*" ]]; then
echo ":topic:_kafka-list-topic"
elif [[ "$option" =~ ".*file.*" ]]; then
echo ":file:_files"
else
echo ":file:_files"
fi
elif [[ "$option" =~ ".*Integer.*" ]]; then
echo ":file:_files"
elif [[ "$option" =~ ".*Long.*" ]]; then
echo ":file:_files"
else
echo ""
fi
}
function escape_bracket() {
def=$1
echo $def | sed 's/\[/\\[/g' | sed 's/\]/\\]/g'
}
function kafka_retrieve_help_command() {
cmd=$1
option=""
desc=""
help_output=`$cmd --help 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 [[ "$start_desc_column" == "-1" ]]; then
first_part_line=`echo $line | tr '\t' ' '`
second_part_line=`echo $line | tr '\t' ' '`
else
first_part_line=`echo $line | cut -c -$start_desc_column | tr '\t' ' '`
second_part_line=`echo $line | cut -c $start_desc_column- | tr '\t' ' '`
fi
if [[ $first_part_line =~ "^[ \s\t]*--[a-z][a-z\-\.]+" ]]; then
if [ ! -z $option ]; then
ctype=`type_from_option $option_full`
desc=`escape_bracket $desc`
echo "$arg_name+=('${option}[${desc//\'/''}]${ctype}')" >> $OUT
fi
option=`echo $first_part_line | sed -E 's/^\s*(--[a-z\.\\\-]+).*$/\1/'`
option_full=$first_part_line
desc=""
fi
IFS=" "
for word in `echo $second_part_line`; do
desc="$desc $word"
done
IFS=$'\n'
done
unset IFS
if [ ! -z $option ]; then
ctype=`type_from_option $option_full`
desc=`escape_bracket $desc`
echo "$arg_name+=('${option}[${desc//\'/''}]${ctype}')" >> $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 _arguments \\\$\$arg_name
}
function _kafka-list-topic() {
compadd \`kcat -L -J | jq '.topics[].topic' -r\`
}
EOF
for cmd in ${COMMANDS[@]}; do
kafka_retrieve_help_command $cmd
kafka-command $cmd
done