Skip to content

Commit 3d6a5b3

Browse files
authored
0.4.0: supported autocomplete (#15)
1 parent c0f7062 commit 3d6a5b3

File tree

3 files changed

+427
-1
lines changed

3 files changed

+427
-1
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2020 Wenhao Ji <[email protected]>
3+
Copyright (c) 2020-2022 Wenhao Ji <[email protected]>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

bin/kubectl_complete-tmux_exec

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (c) 2022 Wenhao Ji <[email protected]>
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
# this software and associated documentation files (the "Software"), to deal in
7+
# the Software without restriction, including without limitation the rights to
8+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
# the Software, and to permit persons to whom the Software is furnished to do so,
10+
# subject to the following conditions:
11+
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
declare -ra KUBECTL_ARG_SHORT_OPTS=(
23+
'-s'
24+
)
25+
26+
declare -ra KUBECTL_ARG_LONG_OPTS=(
27+
'--container'
28+
'--cluster'
29+
'--password'
30+
'--request-timeout'
31+
'--server'
32+
'--token'
33+
'--user'
34+
'--username'
35+
'--kubeconfig'
36+
)
37+
38+
declare -ra KUBECTL_NOARG_LONG_OPTS=(
39+
'--insecure-skip-tls-verify'
40+
)
41+
42+
declare -ra ALL_NOARG_SHORT_OPTS=(
43+
'-A'
44+
'-h'
45+
'-V'
46+
'-i'
47+
'-t'
48+
'-d'
49+
'-C'
50+
)
51+
52+
declare -ra ALL_ARG_SHORT_OPTS=(
53+
'-c'
54+
'-l'
55+
'-f'
56+
'-n'
57+
"${KUBECTL_ARG_SHORT_OPTS[@]}"
58+
)
59+
60+
declare -ra ALL_SHORT_OPTS=(
61+
"${ALL_NOARG_SHORT_OPTS[@]}"
62+
"${ALL_ARG_SHORT_OPTS[@]}"
63+
)
64+
65+
declare -ra ALL_NOARG_LONG_OPTS=(
66+
'--help'
67+
'--version'
68+
'--dry-run'
69+
'--stdin'
70+
'--tty'
71+
'--detach'
72+
'--remain-on-exit'
73+
'--all-namespaces'
74+
'--enable-control-mode'
75+
"${KUBECTL_NOARG_LONG_OPTS[@]}"
76+
)
77+
78+
declare -ra ALL_ARG_LONG_OPTS=(
79+
'--container'
80+
'--selector'
81+
'--select-layout'
82+
'--session-mode'
83+
'--file'
84+
'--namespace'
85+
'--context'
86+
"${KUBECTL_ARG_LONG_OPTS[@]}"
87+
)
88+
89+
declare -ra ALL_LONG_OPTS=(
90+
"${ALL_NOARG_LONG_OPTS[@]}"
91+
"${ALL_ARG_LONG_OPTS[@]}"
92+
)
93+
94+
declare -ra ALL_ARG_OPTS=(
95+
"${ALL_ARG_SHORT_OPTS[@]}"
96+
"${ALL_ARG_LONG_OPTS[@]}"
97+
)
98+
99+
declare -ra FILE_OPTS=(
100+
'-f'
101+
'--file'
102+
'--kubeconfig'
103+
)
104+
105+
declare -ra ALL_OPTS=(
106+
"${ALL_SHORT_OPTS[@]}"
107+
"${ALL_LONG_OPTS[@]}"
108+
)
109+
110+
declare -ra TMUX_LAYOUTS=(
111+
'even-horizontal'
112+
'even-vertical'
113+
'main-horizontal'
114+
'main-vertical'
115+
'tiled'
116+
)
117+
118+
declare -ra SESSION_MODES=(
119+
'auto'
120+
'new-session'
121+
'current-session'
122+
)
123+
124+
# Cobra's Shell Comp Directive Constants
125+
# also see: https://github.com/spf13/cobra/blob/main/completions.go
126+
readonly NO_FILE_COMP_DIR=':4'
127+
readonly FILTER_FILE_COMP_DIR=':8'
128+
129+
function array_contains() {
130+
local occur="$1"
131+
local arr=("${@:2}")
132+
for e in "${arr[@]}"; do
133+
if [[ "${e}" == "${occur}" ]]; then
134+
return 0
135+
fi
136+
done
137+
return 1
138+
}
139+
140+
function __complete() {
141+
if [[ "$#" -eq 0 ]]; then
142+
exit 1
143+
fi
144+
145+
local state='INITIAL'
146+
while [[ "$#" -gt 0 ]]; do
147+
local arg="$1"
148+
shift
149+
case "${state}" in
150+
'INITIAL')
151+
case "${arg}" in
152+
'')
153+
;;
154+
'-')
155+
state='INITIAL'
156+
;;
157+
'--')
158+
state='LONG_OPTS_COMP'
159+
;;
160+
'--session-mode')
161+
state='SESSION_MODE_COMP'
162+
;;
163+
'--select-layout')
164+
state='TMUX_LAYOUT_COMP'
165+
;;
166+
*)
167+
if array_contains "${arg}" "${FILE_OPTS[@]}"; then
168+
state='FILE_COMP'
169+
elif array_contains "${arg}" "${ALL_ARG_OPTS[@]}"; then
170+
state='NO_COMP'
171+
fi
172+
;;
173+
esac
174+
;;
175+
'LONG_OPTS_COMP')
176+
state='EXEC_CMD_COMP'
177+
;;
178+
'SESSION_MODE_COMP' | 'TMUX_LAYOUT_COMP' | 'FILE_COMP' | 'NO_COMP')
179+
case "${arg}" in
180+
'')
181+
;;
182+
*)
183+
state='INITIAL'
184+
;;
185+
esac
186+
;;
187+
*)
188+
;;
189+
esac
190+
done
191+
192+
case "${state}" in
193+
'INITIAL')
194+
printf -- '%s\n' "${ALL_OPTS[@]}"
195+
;;
196+
'LONG_OPTS_COMP')
197+
printf -- '%s\n' "${ALL_LONG_OPTS[@]}"
198+
;;
199+
'SESSION_MODE_COMP')
200+
printf -- '%s\n' "${SESSION_MODES[@]}"
201+
;;
202+
'TMUX_LAYOUT_COMP')
203+
printf -- '%s\n' "${TMUX_LAYOUTS[@]}"
204+
;;
205+
'EXEC_CMD_COMP' | 'NO_COMP')
206+
echo "${NO_FILE_COMP_DIR}"
207+
;;
208+
'FILE_COMP')
209+
echo "${FILTER_FILE_COMP_DIR}"
210+
;;
211+
esac
212+
}
213+
214+
__complete "$@"

0 commit comments

Comments
 (0)