-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.zsh
155 lines (147 loc) · 3.31 KB
/
init.zsh
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
() {
local -r target=${1}
shift
if [[ ! ( -s ${target} && ${target} -nt ${commands[${1}]} ) ]]; then
"${@}" >! ${target} || return 1
fi
if (( ${+functions[compdef]} && ! ${+functions[${target:t}]} )) source ${target}
} ${0:h}/functions/_kubectl kubectl completion zsh
# Positional parameters:
# 1: name of input associative array
# 2: maximum key lenght in input associative array
# 3: optional, minimum key lenght, default is 1
_k_parse_token() {
local -i klen
local kval
_kfound=0
# Try longer keys first
for klen in {$(( ${2} < ${#_kin} ? ${2} : ${#_kin} ))..${3:-1}}; do
kval=${${(P)1}[${_kin:0:${klen}}]}
if [[ -n ${kval} ]]; then
_k_parsed+=(${kval})
_kin=${_kin:${klen}}
_kfound=1
break
fi
done
}
_k_parse() {
local -rA kcommands=(
a apply
c create
d describe
e edit
E exec
g get
h help
k kustomize
l logs
t top
v version
x delete
)
local -rA kresources=(
apis apiservice
bi binding
cj cronjob
cm configmap
cr clusterrole
crb clusterrolebinding
crd customresourcedefinition
crv controllerrevision
cs componentstatus
csid csidriver
csin csinode
csisc csistoragecapacity
csr certificatesigningrequest
de deployment
ds daemonset
ep endpoint
eps endpointslice
ev event
fs flowschema
hpa horizontalpodautoscaler
ic ingressclass
ing ingress
jo job
le lease
lr limitrange
lsarv localsubjectaccessreview
mwc mutatingwebhookconfiguration
no node
np networkpolicy
ns namespace
pc priorityclass
pdb poddisruptionbudget
plc prioritylevelconfiguration
po pod
psp podsecuritypolicies
pt podtemplate
pv persistentvolume
pvc persistentvolumeclaim
rb rolebinding
rc replicationcontroller
ro role
rq resourcequota
rs replicaset
rtc runtimeclass
sa serviceaccount
sarv subjectaccessreview
sc storageclass
sec secret
ssarv selfsubjectaccessreview
ssrrv selfsubjectrulesreview
sts statefulset
svc service
tr tokenrequest
trv tokenreview
va volumeattachment
vo volume
vwc validatingwebhookconfiguration
)
local -rA koptions=(
A --all-namespaces
a --all
h --help
oj -o=json
on -o=name
ow -o=wide
oy -o=yaml
sl --show-labels
w --watch
)
local -rA ksuffixes=(
f -f
k -k
l -l
)
local _kin=${1}
local -i _kfound
typeset -ga _k_parsed
_k_parsed=(kubectl)
# Parse commands (1, required)
_k_parse_token kcommands 1
if (( ! _kfound )); then
print -u2 -PR "unknown command: %B${_kin[1]}%b"
return 2
fi
# Parse resources (0..n)
local -i kpos=$(( ${#_k_parsed} + 1 ))
while true; do
_k_parse_token kresources 5 2
if (( ! _kfound )) break
done
# Join resources with comma
if (( kpos < ${#_k_parsed} )) _k_parsed[${kpos},-1]=(${(j:,:)_k_parsed[${kpos},-1]})
# Parse options (0..n)
while true; do
_k_parse_token koptions 2
if (( ! _kfound )) break
done
# Parse suffixes (0..1)
_k_parse_token ksuffixes 1
if [[ -n ${_kin} ]]; then
print -u2 -PR "unknown token: %B${_kin}%b"
return 2
fi
}