-
Notifications
You must be signed in to change notification settings - Fork 299
/
kubectl-examples
executable file
·74 lines (63 loc) · 1.51 KB
/
kubectl-examples
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
#!/usr/bin/env bash
debug() {
[[ "$DEBUG" ]] && echo "-----> $*" 1>&2
}
print-example() {
declare res=$1 pattern=$2
dir=$(dirname $(readlink $BASH_SOURCE))
debug "dir=${dir}"
command fzf --version >/dev/null 2>/dev/null && FZF_INSTALLED=true
if [[ $res == "" ]]; then
if [[ $FZF_INSTALLED ]];then
res=$(cd ${dir}; ls -1 $dir|grep -v kubectl-examples | fzf -e --prompt="Resource Type>" )
else
PS3="Resource Type: "
select res in $(ls -1 $dir|grep -v kubectl-examples); do
debug "res type choosen: ${res}"
break
done
fi
fi
# resorce names can be abreviated
[[ -d ${dir}/${res} ]] || res="${res}*"
match=0
for f in ${dir}/${res}/**/*${pattern}*.yaml; do
[[ -e ${f} ]] && : $(( match++ ))
done
case ${match} in
0)
debug "no match ..."
return
;;
1)
debug "exact match"
yaml=${dir}/${res}*/**/*${pattern}*.yaml
;;
*)
debug "multiple choice"
PS3="Yaml example: "
if [[ $FZF_INSTALLED ]];then
yaml=$(
cd ${dir}/${res}
ls -1 **/*${pattern}*.yaml \
| fzf -e --preview='cat {}'
)
yaml="${dir}/${res}/${yaml}"
else
select yaml in ${dir}/${res}/**/*${pattern}*.yaml; do
debug "YAML selected: ${yaml}"
break
done
fi
;;
esac
if [ -f ${yaml} ]; then
cat ${yaml}
fi
}
main() {
: ${DEBUG:=1}
shopt -s globstar
print-example "$@"
}
[[ "$0" == "$BASH_SOURCE" ]] && main "$@" || true