-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
help-functions
executable file
·320 lines (274 loc) · 9.07 KB
/
help-functions
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common-functions"
export SUBCOMMAND_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/subcommands"
fn-help() {
declare CMD="$1"
local cmd EXIT_CODE
if [[ "$CMD" == "help" ]] || [[ "$CMD" == "$PLUGIN_COMMAND_PREFIX:help" ]] || [[ "$CMD" == "$PLUGIN_COMMAND_PREFIX" ]] || [[ "$CMD" == "$PLUGIN_COMMAND_PREFIX:default" ]]; then
fn-help-all "$@"
exit 0
fi
pushd "$SUBCOMMAND_ROOT" >/dev/null 2>&1
for cmd in *; do
if [[ "$CMD" == "${PLUGIN_COMMAND_PREFIX}:$cmd" ]]; then
"$SUBCOMMAND_ROOT/$cmd" "$@"
EXIT_CODE="$?"
exit "$EXIT_CODE"
fi
done
popd >/dev/null 2>&1
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
}
fn-help-all() {
declare CMD="$1" SUBCOMMAND="$2"
local CMD_OUTPUT BLUE BOLD FULL_OUTPUT NORMAL
FULL_OUTPUT=true
if [[ "$CMD" == "$PLUGIN_COMMAND_PREFIX:help" ]] || [[ "$CMD" == "$PLUGIN_COMMAND_PREFIX" ]] || [[ "$CMD" == "$PLUGIN_COMMAND_PREFIX:default" ]]; then
BOLD="$(fn-help-fancy-tput bold)"
NORMAL="$(fn-help-fancy-color "\033[m")"
BLUE="$(fn-help-fancy-color "\033[0;34m")"
CYAN="$(fn-help-fancy-color "\033[1;36m")"
if [[ -n "$SUBCOMMAND" ]] && [[ "$SUBCOMMAND" != "--all" ]]; then
fn-help-contents-subcommand "$SUBCOMMAND" "$FULL_OUTPUT"
return "$?"
fi
echo -e "${BOLD}usage${NORMAL}: dokku ${PLUGIN_COMMAND_PREFIX}[:COMMAND]"
echo ''
echo -e "${BOLD}List your $PLUGIN_COMMAND_PREFIX services.${NORMAL}"
echo ''
echo -e "${BLUE}Example:${NORMAL}"
echo ''
echo " \$ dokku $PLUGIN_COMMAND_PREFIX:list"
echo ''
fn-help-list-example | column -c5 -t -s,
echo ''
echo -e "dokku ${BOLD}${PLUGIN_COMMAND_PREFIX}${NORMAL} commands: (get help with ${CYAN}dokku ${PLUGIN_COMMAND_PREFIX}:help SUBCOMMAND${NORMAL})"
echo ''
fn-help-contents | sort | column -c2 -t -s,
echo ''
elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then
fn-help-contents
else
cat <<help_desc
$PLUGIN_COMMAND_PREFIX, Plugin for managing $PLUGIN_SERVICE services
help_desc
fi
return 0
}
fn-help-contents() {
pushd "$SUBCOMMAND_ROOT" >/dev/null 2>&1
for cmd in *; do
fn-help-contents-subcommand "$cmd" || true
done
}
fn-help-contents-subcommand() {
declare SUBCOMMAND="$1" FULL_OUTPUT="$2"
local HELP_TMPDIR=$(mktemp -d --tmpdir)
local UNCLEAN_FILE="${HELP_TMPDIR}/cmd-unclean" CLEAN_FILE="${HELP_TMPDIR}/cmd-clean"
local BOLD CMD_OUTPUT CYAN EXAMPLE LIGHT_GRAY NORMAL
trap 'rm -rf "$HELP_TMPDIR" > /dev/null' RETURN INT TERM EXIT
rm -rf "$UNCLEAN_FILE" "$CLEAN_FILE"
cat "$SUBCOMMAND_ROOT/$SUBCOMMAND" >"$UNCLEAN_FILE"
fn-help-subcommand-sanitize "$UNCLEAN_FILE" "$CLEAN_FILE"
if ! is_implemented_command "$SUBCOMMAND"; then
return 1
fi
args="$(fn-help-subcommand-args "$CLEAN_FILE" "$FULL_OUTPUT")"
SUBCOMMAND=":$SUBCOMMAND"
[[ "$SUBCOMMAND" == ":default" ]] && SUBCOMMAND=""
cmd_line="$(echo -e "${SUBCOMMAND} ${args}" | sed -e 's/[[:space:]]*$//')"
desc="$(grep desc "$CLEAN_FILE" | head -1)"
eval "$desc"
BLUE="$(fn-help-fancy-color "\033[0;34m")"
BOLD="$(fn-help-fancy-tput bold)"
CYAN="$(fn-help-fancy-color "\033[1;36m")"
NORMAL="$(fn-help-fancy-color "\033[m")"
LIGHT_GRAY="$(fn-help-fancy-color "\033[2;37m")"
LIGHT_RED="$(fn-help-fancy-color "\033[1;31m")"
CMD_OUTPUT="$(echo -e " ${PLUGIN_COMMAND_PREFIX}${cmd_line}, ${LIGHT_GRAY}${desc}${NORMAL}")"
if [[ "$FULL_OUTPUT" != "true" ]]; then
echo "$CMD_OUTPUT"
return 0
fi
echo -e "${BOLD}usage:${NORMAL} dokku ${PLUGIN_COMMAND_PREFIX}${cmd_line}"
echo ''
echo -e "${BOLD}${desc}${NORMAL}"
echo ''
ARGS="$(fn-help-subcommand-list-args "$CLEAN_FILE")"
if [[ -n "$ARGS" ]]; then
echo -e "${CYAN}arguments:${NORMAL}"
echo ''
echo "$ARGS" | column -c2 -t -s,
echo ''
fi
FLAGS="$(fn-help-subcommand-list-flags "$CLEAN_FILE")"
if [[ -n "$FLAGS" ]]; then
echo -e "${BLUE}flags:${NORMAL}"
echo ''
echo "$FLAGS" | column -c2 -t -s,
echo ''
fi
EXAMPLE="$(fn-help-subcommand-example "$CLEAN_FILE")"
if [[ -n "$EXAMPLE" ]]; then
echo -e "${LIGHT_RED}examples:${NORMAL}"
echo ''
echo "$EXAMPLE"
echo ''
fi
return 0
}
fn-help-fancy-tput() {
declare desc="a wrapper around tput"
if [[ -n "$DOKKU_NO_COLOR" ]] || [[ "$TERM" == "unknown" ]] || [[ "$TERM" == "dumb" ]]; then
return
fi
tput "$@"
}
fn-help-fancy-color() {
declare desc="a wrapper around colors"
if [[ -n "$DOKKU_NO_COLOR" ]] || [[ "$TERM" == "unknown" ]] || [[ "$TERM" == "dumb" ]]; then
return
fi
echo "$@"
}
fn-help-list-example() {
# shellcheck disable=SC2034
declare desc="return $PLUGIN_COMMAND_PREFIX plugin help content"
cat <<help_list
$PLUGIN_SERVICE services
service-name
help_list
}
fn-help-subcommand-args() {
declare FUNC_FILE="$1" FULL_OUTPUT="$2"
local argline arglist args argpos BLUE NORMAL
if [[ "$FULL_OUTPUT" == "true" ]]; then
BLUE="$(fn-help-fancy-color "\033[0;34m")"
NORMAL="$(fn-help-fancy-color "\033[m")"
fi
argline=$(grep declare "$FUNC_FILE" | grep -v "declare desc" | head -1 || true)
arglist=($(echo -e "${argline// /"\n"}" | awk -F= '/=/{print ""$1""}'))
args=""
argpos=0
for arg in "${arglist[@]}"; do
argpos=$((argpos + 1))
if [[ "$FULL_OUTPUT" != "true" ]] && [[ "$argpos" == 4 ]]; then
args+="..."
break
fi
if [[ "$arg" == *_FLAG ]]; then
arg="${arg/_FLAG/}"
if [[ $arg == "INFO" ]]; then
arg="SINGLE_INFO_FLAG"
args+=" ${BLUE}[--${arg//_/-}]${NORMAL}"
else
args+=" ${BLUE}[-${arg:0:1}|--${arg//_/-}]${NORMAL}"
fi
elif [[ "$arg" == *_FLAGS_LIST ]]; then
arg=${arg%_*}
args+=" [--${arg//_/-}...]"
elif [[ "$arg" == *_LIST ]]; then
arg=${arg%_*}
args+=" <${arg//_/-}...>"
elif [[ "$arg" == *_OPTIONAL ]]; then
argName="${arg/_OPTIONAL/}"
args+=" [<${argName//_/-}>]"
else
args+=" <${arg//_/-}>"
fi
done
args=$(echo "$args" | tr "\n" " ")
# shellcheck disable=SC2001
echo "${args,,}" | sed -e 's/^[[:space:]]*//'
}
fn-help-subcommand-example() {
declare FUNC_FILE="$1"
local EXAMPLE
EXAMPLE=$(grep "#E" "$FUNC_FILE" | cut -d' ' -f2- || true)
if [[ -z "$EXAMPLE" ]]; then
return 0
fi
BOLD="$(fn-help-fancy-tput bold)"
LAST_LINE=""
LIGHT_GRAY="$(fn-help-fancy-color "\033[2;37m")"
OTHER_GRAY="$(fn-help-fancy-color "\033[7;37m")"
NEWLINE=""
NORMAL="$(fn-help-fancy-color "\033[m")"
_fn-help-apply-shell-expansion "$EXAMPLE" | while read -r line; do
line="$(echo "$line" | cut -c 4-)"
if [[ "$line" == export* ]] || [[ "$line" == dokku* ]]; then
[[ "$LAST_LINE" == "command" ]] && NEWLINE=""
[[ "$LAST_LINE" == "sentence" ]] && NEWLINE="\n"
echo -e "${NEWLINE} ${LIGHT_GRAY}${line}${NORMAL}"
LAST_LINE="command"
else
[[ "$LAST_LINE" == "command" ]] && NEWLINE="\n"
[[ "$LAST_LINE" == "sentence" ]] && NEWLINE=""
[[ "$line" == \>* ]] && line="\n ${BOLD}${line}${NORMAL}"
# shellcheck disable=SC2001
[[ "$line" == " "* ]] && line=" ${OTHER_GRAY}$(echo "$line" | sed -e 's/^[[:space:]]*//')${NORMAL}"
echo -e "${NEWLINE}${line}"
LAST_LINE="sentence"
NEWLINE="\n"
fi
done
}
fn-help-subcommand-list-args() {
declare FUNC_FILE="$1"
local EXAMPLE LIGHT_GRAY NORMAL
FLAGS=$(grep "#A" "$FUNC_FILE" | cut -d'A' -f2- | sed -e 's/^[[:space:]]*//' || true)
if [[ -z "$FLAGS" ]]; then
return 0
fi
NORMAL="$(fn-help-fancy-color "\033[m")"
LIGHT_GRAY="$(fn-help-fancy-color "\033[2;37m")"
_fn-help-apply-shell-expansion "$FLAGS" | while read -r line; do
echo -e "$(echo "$line" | cut -d',' -f1),${LIGHT_GRAY}$(echo "$line" | cut -d',' -f2-)${NORMAL}"
done
}
fn-help-subcommand-list-flags() {
declare FUNC_FILE="$1"
local EXAMPLE LIGHT_GRAY NORMAL
FLAGS=$(grep "#F" "$FUNC_FILE" | cut -d'F' -f2- | sed -e 's/^[[:space:]]*//' || true)
if [[ -z "$FLAGS" ]]; then
return 0
fi
NORMAL="$(fn-help-fancy-color "\033[m")"
LIGHT_GRAY="$(fn-help-fancy-color "\033[2;37m")"
_fn-help-apply-shell-expansion "$FLAGS" | while read -r line; do
echo -e "$(echo "$line" | cut -d',' -f1),${LIGHT_GRAY}$(echo "$line" | cut -d',' -f2-)${NORMAL}"
done
}
fn-help-subcommand-sanitize() {
declare FUNC_FILE="$1" OUTGOING_FUNC_FILE="$2"
local FUNCTION_FOUND=false
local IFS OIFS
touch "$OUTGOING_FUNC_FILE"
OIFS="$IFS"
IFS=,
while read -r p; do
IFS="$OIFS"
if [[ "$p" == *"-cmd \"\$@\""* ]] || [[ "$p" == "" ]]; then
continue
fi
if [[ "$FUNCTION_FOUND" == true ]]; then
echo "$p" >>"$OUTGOING_FUNC_FILE"
continue
fi
if [[ "$p" == *"()"* ]]; then
FUNCTION_FOUND=true
echo "$p" >>"$OUTGOING_FUNC_FILE"
continue
fi
done <"$FUNC_FILE"
}
_fn-help-apply-shell-expansion() {
declare desc="expand environment variables for a shell command"
declare data="$1"
declare delimiter="__apply_shell_expansion_delimiter__"
declare command="cat <<$delimiter"$'\n'"$data"$'\n'"$delimiter"
eval "$command"
}