-
Notifications
You must be signed in to change notification settings - Fork 0
/
.q-func.bash
256 lines (202 loc) · 4.91 KB
/
.q-func.bash
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# =========================================================================
#
# q as kubectl
#
# Copyright 2024 Ildar Shaimordanov
# MIT License
#
# =========================================================================
qq() {
Q_DEBUG=1 q "$@"
}
# =========================================================================
q() {
[ $# -gt 0 ] || {
echo "Usage: q DECK [SQUAD] [watch|names|...]" >&2
return 1
}
# DECK validation
: "${1:?Deck name required}"
declare -p Q_DECK >/dev/null 2>&1 || {
echo "No any deck found" >&2
return 1
}
[ -n "${Q_DECK["$1/config"]+ok}" ] \
|| [ -n "${Q_DECK["$1/context"]+ok}" ] \
|| [ -n "${Q_DECK["$1/namespace"]+ok}" ] \
|| {
echo "Deck does not exist: '$1'" >&2
return 1
}
local cfg
local ctx
local nsp
cfg="${Q_DECK[$1/config]:-${Q_DECK[?/config]:-}}"
ctx="${Q_DECK[$1/context]:-${Q_DECK[?/context]:-}}"
nsp="${Q_DECK[$1/namespace]:-${Q_DECK[?/namespace]:-}}"
[ -n "$cfg" ] \
|| [ -n "$ctx" ] \
|| [ -n "$nsp" ] \
|| {
echo "Deck without any equipment: '$1'" >&2
return 1
}
shift
# SQUAD validation
local sel
# The common use
# q DECK [SQUAD] ...
# kubectl DECK [SQUAD] ...
[ -n "${1+ok}" ] \
&& [ "$1" != "watch" ] \
&& [ "$1" != "names" ] \
&& declare -p Q_SQUAD >/dev/null 2>&1 \
&& [ -n "${Q_SQUAD[$1]+ok}" ] \
&& sel="${Q_SQUAD[$1]}" \
&& shift
# Special cases detection and handling
if [ $# -eq 0 ]
then
# The special use 1: show pods
# q DECK [SQUAD]
# kubectl DECK [SQUAD] get pods
set -- get pods
elif [ $# -eq 1 ] && [ "$1" = "watch" ]
then
# The special use 2: show pods in watch mode
# q DECK [SQUAD] watch
# kubectl DECK [SQUAD] get pods --watch
set -- get pods --watch
elif [ $# -eq 1 ] && [ "$1" = "names" ]
then
# The special use 3: show pod names
# q DECK [SQUAD] names
# kubectl DECK [SQUAD] get pods -o jsonpath=...
set -- get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}'
fi
# One more trick for commands or plugins
# q DECK [SQUAD] CMD [...]
# kubectl CMD DECK [SQUAD] [...]
# Options are allowed before kubectl builtin commands only. In
# the cases when the options precede a plugin name, they are
# immediately declined with error. Let's become a bit friendly
# and make it configurable: enable putting the deck and squad
# options after the first argument if it looks like a command or
# plugin name - a string is not empty and not starting with the
# "-" dash symbol.
local cmd
[ "${Q_PUT_NAME_FIRST_THEN_OPTIONS:+ok}" ] \
&& [[ ! "$1" =~ ^- ]] \
&& cmd="$1" \
&& shift
# That's the final step now -- constructing the command.
set -- kubectl \
${cmd:+"$cmd"} \
${cfg:+--kubeconfig="$cfg"} \
${ctx:+--context="$ctx"} \
${nsp:+--namespace="$nsp"} \
${sel:+--selector="$sel"} \
"$@"
# Let's run it in the subshell. It makes much simpler checking
# if we need echoing to STDERR, when debugging is turned on.
(
[ "${Q_DEBUG:+ok}" ] \
&& set -- echo "$@" \
&& exec >&2
"$@"
)
}
# =========================================================================
qcheck() {
local qedit=''
local qupdate=''
local OPTIND=1
local qarg
while getopts ':eu' qarg
do
case "$qarg" in
e ) qedit=1 ;;
u ) qupdate=1 ;;
* ) echo "Illegal option -- $OPTARG" >&2 ; return 1 ;;
esac
done
shift $(( OPTIND-1 ))
[ $# -eq 0 ] \
&& set -- ~/.q-decl.ini
[ -n "$qedit" ] && {
"${Q_EDITOR:-${EDITOR:-vi}}" "$@" \
|| return $?
}
[ -n "$qupdate" ] && {
local qdecl
# shellcheck disable=SC1090
qdecl="$( qcheck "$@" )" \
&& echo "$qdecl" > ~/.q-decl.bash \
&& . ~/.q-decl.bash \
&& echo "q declarations updated" >&2
return $?
}
awk '
# Trim whitespaces, skip empty lines and comments
{
sub(/^\s*(#.*)?/, "");
sub(/\s*$/, "");
if ( ! $0 ) next;
}
# Check sections
match($0, /^\[deck\/(\?|\w+)\]/, m) {
what = "deck";
name = m[1];
next;
}
$0 == "[squad]" {
what = "squad";
next;
}
/^\[/ {
error = "Illegal section";
exit;
}
# Check parameters
match($0, /^(\w+)\s*=\s*("(.*)"|(.*))/, m) {
key = m[1];
val = m[3] m[4];
gsub(/\\/, "\\\\", val);
gsub(/"/, "\\\"", val);
}
what == "deck" && key ~ /^(config|context|namespace)$/ {
if ( key == "config" ) sub(/^~\//, "$HOME/", val);
deck[name "/" key] = val;
next;
}
what == "squad" {
squad[key] = val;
next;
}
{
error = "Illegal parameter";
exit;
}
# Finalize
function print_decl(name, values, d, n, i) {
printf "\ndeclare -A Q_%s=(\n", name;
n = asorti(values, d);
for (i = 1; i <= n; i++) printf " [%s]=\"%s\"\n", d[i], values[ d[i] ];
printf ")\n";
}
END {
if ( error ) {
format = "Error: %s in file %s at line %s\n%s\n";
printf format, error, FILENAME, NR, $0 > "/dev/stderr";
exit 1;
}
print "# This file was created automatically.";
print "# DO NOT EDIT IT DIRECTLY. Instead use qcheck -u.";
print "# The source file is " FILENAME ".";
print_decl("DECK", deck);
print_decl("SQUAD", squad);
}
' "$@"
}
# =========================================================================
# EOF