-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline-player
executable file
·686 lines (591 loc) · 17.5 KB
/
cmdline-player
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#!/bin/bash
# Info from:
# https://serverfault.com/questions/178457/can-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi
# Playback commands using screen's 'stuff' command.
# User changeable variables
# The extension to use for the recording.
# Use any extension supported by byzanz-record.
#EXT="webm"
EXT="gif"
# Don't change any variables below
declare -rg STOP=3 OK=0 ERROR=1 STDERR='/dev/stderr'
DELAY=18 # not <-- less than 1
FILE=
WAITING=
E2E=
PID=
VIDEO="screencast.${EXT}"
PROMPTLIST='$#%>'
STOPPROMPT='&'
SCR2MD=
MDFILE=
main() {
# Set up the parser
setup_parser
trap cleanup EXIT
PA_run "$@"
retval=$?
if [[ ${retval} -eq ${ERROR} ]]; then
return "${ERROR}"
elif [[ ${retval} -eq ${STOP} ]]; then
return "${OK}"
fi
sanity_checks
kill_old_gnu_screencasts
[[ -z ${E2E} ]] && choose_recording_window # <- using xwininfo
start_gnu_screen
[[ -z ${E2E} ]] && start_recording &
OLDWAITING="${WAITING}"
pause=0
linenum=0
IFS='' # <- keep all spaces
while true; do
((linenum++))
# screencast commands
while read -r line; do
screen -ls screencast &>/dev/null || break
((linenum++))
if [[ ${line} == "screencast clear" ]]; then
screen -S screencast -p 0 -X stuff "clear^M"
continue
elif [[ ${line} == ".TS" || ${line} == ".TS"* ]]; then
# Skip markdown lines
continue
elif [[ ${line} == ".MD" || ${line} == ".MD"* ]]; then
# Skip markdown lines
continue
elif [[ ${line} == "screencast delay"* ]]; then
DELAY="${line##* }"
continue
elif [[ ${line} == "screencast pause" ]]; then
printf -- '\n- PAUSED - press any key to unpause...\n'
pause=1
break
elif [[ ${line} == "screencast paste" || ${line} == "screencast paste"* ]]; then
marker="${line##* }"
if [[ ${marker} == "paste" ]]; then
paste=true
continue
elif [[ ${marker} == "start" || ${marker} == "begin" ]]; then
pastetoggle=true
wait_for_prompt
continue
elif [[ ${marker} == "end" ]]; then
pastetoggle=
continue
fi
elif [[ ${line} == "screencast sleep"* ]]; then
sleep "${line##* }"
continue
elif [[ ${line} == "screencast subshell" ]]; then
unset waitforprompt
subshell=true
continue
elif [[ ${line} == "screencast prompt"* ]]; then
PROMPTLIST="${line##* }"
waitforprompt=true
waitforsilence=
continue
elif [[ ${line} == "screencast waitforsilence"* ]]; then
waitforsilence=true
waitforprompt=
continue
elif [[ ${line} == "screencast firstps" ]]; then
unset waitforprompt
WAITING="${OLDWAITING}"
continue
fi
# Wait for prompt
if [[ -n ${waitforprompt} ]]; then
[[ -z ${pastetoggle} ]] && wait_for_prompt
elif [[ -n ${waitforsilence} ]]; then
[[ -z ${pastetoggle} ]] && wait_for_silence
else
while [[ ${WAITING} != $(fingerprint) ]]; do
sleep 1
done
sleep .5
fi
[[ ${line} == '"'* && -z ${pastetoggle} ]] && {
continue
}
if [[ -n ${pastetoggle} ]]; then
paste_line "${line}" nowait
continue
fi
[[ -n ${subshell} ]] && {
unset subshell
sleep .5
WAITING=$(fingerprint)
}
if [[ -n ${paste} ]]; then
paste_line "${line}"
unset paste
else
print_line "${line}"
fi
done <<<"$(sed -n ' '"${linenum}"',$ {p}' "${FILE}")"
[[ ${pause} -eq 1 ]] && {
read -r
pause=0
continue
}
break
done
sleep 4
[[ -z ${E2E} ]] && end_recording
[[ -n ${SCR2MD} ]] && scr2md "${FILE}" "${MDFILE}"
echo
}
set_filename() {
FILE="$1"
}
setup_parser() {
# Program the parser's state machine
PA_set_state "ARG1"
PA_add_state "ARG1" "" "END" "set_filename"
# Set up the parser's option callbacks
PA_add_option_callback "" "process_options" || return
# Set up the parser's usage callbacks
PA_add_usage_callback "" "usage" || return
}
cleanup() {
[[ -n ${E2E} ]] && screen -S screencast -X hardcopy -h hardcopy
}
usage() {
cat <<EnD
cmdline-player - play commands from a .scr file.\n'
Usage: cmdline-player [ -hq ] | [ [-wn] FILE ]
FILE - The name of the file containing screencast and shell commands.
-h
--help - This help text.
-w
--window "NAME" - Specify the NAME of the window to record.
-n
--norecord - Don't record the playback.
-q
--query - Retrieve the name of a window by clicking on it.
-c
--create-mds "NAME" - Create a markdown file named NAME from the screencast
file. NAME can contain the full path and file name.
Paths not starting with '/' are relative to the
current working directory. A transcript file is
also created in the same directory. The video is
moved to a directory named 'images' in the current
directory.
Examples:
Choose a window to record and start playing back all of KTHW:
$ # In the terminal to be recorded:
$ while true; do screen -r -e ^Oo screencast; sleep .5; done
$ # In the controller terminal:
$ cd kubernete-the-hard-way-in-containers/kthw-docker/
$ wname=\$(cmdline-player -q)
$ for i in {2..14}; do cmdline-player -w "\$wname" -c kthw/kthw-\$i.md screencasts/kthw-\$i.scr <<<"\n"; sleep 1; done
Create a single screencast recording:
$ $(basename "$0") install-mokctl-linux.scr
EnD
}
sanity_checks() {
[[ -z ${FILE} ]] && {
usage
printf 'ERROR: Please provide the file name to play.\n'
exit 1
}
}
process_options() {
case "$1" in
-h | --help)
usage
return "${STOP}"
;;
-w | --window)
WINDOWNAME="$2"
return "$(PA_shift)"
;;
-c | --create-mds)
MDFILE="$2"
SCR2MD="Yes"
return "$(PA_shift)"
;;
-n | --norecord)
E2E="yes"
;;
-q | --query)
xwininfo | sed -n '/Window id:/ {s/^[^"]*"\(.*\)"/\1/p}'
exit 0
;;
*)
usage
printf 'ERROR: "%s" is not a valid option.\n' "${1}" \
>"${STDERR}"
return "${ERROR}"
;;
esac
}
wait_for_prompt() {
while true; do
sleep .4
screen -S screencast -p 0 -X hardcopy latest.hardcopy
if prompt_found "${PROMPTLIST}"; then break; fi
if screencast_stop_found; then exit 1; fi
done
}
wait_for_silence() {
local last latest
last=$(md5sum latest.hardcopy)
while true; do
sleep 1
screen -S screencast -p 0 -X hardcopy latest.hardcopy
latest=$(md5sum latest.hardcopy)
if [[ ${latest} == "${last}" ]]; then break; fi
last="${latest}"
done
}
screencast_stop_found() {
lastcharinfile=$(grep --text -v '^$' latest.hardcopy | tail -n 1 | grep -o '. *$')
[[ ${lastcharinfile} == "${STOPPROMPT}" && -n ${lastcharinfile} ]] && return 0
return 1
}
prompt_found() {
PROMPTLIST="$1"
lastcharinfile=$(grep --text -v '^$' latest.hardcopy | tail -n 1 | grep -o '. *$')
[[ ${PROMPTLIST} == *"${lastcharinfile}"* && -n ${lastcharinfile} ]] && return 0
return 1
}
kill_old_gnu_screencasts() {
local pid
while true; do
pid=$(screen -ls | grep screencast | sed -r 's/[ \t]+//g' | cut -f 1 -d.)
[[ -z ${pid} ]] && break
screen -XS "${pid}" quit
done
}
choose_recording_window() {
if [[ -n ${WINDOWNAME} ]]; then
wininfo="$(xwininfo -name "${WINDOWNAME}")" || exit 1
else
printf 'Click on the window to be recorded\n'
wininfo=$(xwininfo)
fi
declare -g X Y W H
eval "$(awk 'BEGIN {X=0;Y=0;} /Abs.*left X/ {X=$NF;} /Abs.*left Y/ {Y=$NF;} /Width/ {W=$NF;} /Height/ {H=$NF;} END { printf "X=%d\nY=%d\nW=%d\nH=%d\n",X,Y,W,H; }' \
<<<"${wininfo}")"
}
start_recording() {
[[ -e ${VIDEO} ]] && rm "${VIDEO}"
byzanz-record -d 1200 --x="${X}" --y="${Y}" --width="${W}" --height="${H}" "${VIDEO}"
}
end_recording() {
killall byzanz-record &>/dev/null
}
paste_line() {
local line="$1"
line="${line//\\/\\\\}"
screen -S screencast -p 0 -X stuff "${line//\$/\\$}\n"
[[ -z $2 ]] && sleep .2
}
print_line() {
local chararray char
readarray -t chararray <<<"$(printf '%s' "$1" | grep -o .)"
for char in "${chararray[@]}"; do
[[ ${char} == '^' ]] && {
controlchar=yes
continue
}
if [[ -n ${controlchar} ]]; then
# [[ $char != 'c' ]] && screen -S screencast -p 0 -X stuff "\^$char"
screen -S screencast -p 0 -X stuff "^${char}"
controlchar=
elif [[ ${char} == \\ ]]; then
screen -S screencast -p 0 -X stuff \\\\
else
screen -S screencast -p 0 -X stuff "${char}"
fi
sleep ."$(printf '%02d' $((RANDOM % DELAY)))"
done
sleep .5
[[ -z ${controlchar} ]] && {
screen -S screencast -p 0 -X stuff "^M"
}
controlchar=
}
start_gnu_screen() {
printf 'Starting a screen session\n'
if [[ -n ${E2E} ]]; then
opts="-L"
[[ -e hardcopy ]] && rm -f hardcopy
[[ -e screenlog.0 ]] && rm -f screenlog.0
screen -d -m -S screencast "${opts}"
else
screen -d -m -S screencast
fi
PID=$(screen -ls | grep screencast | sed -r 's/[ \t]+//g' | cut -f 1 -d.)
sleep 1
WAITING=$(fingerprint)
[[ -z ${E2E} ]] && {
cat <<EnD
In another terminal type:
screen -r screencast
Then press 'enter' here and the screencast will start
in the other terminal.
EnD
printf '>'
read -r
}
}
fingerprint() {
pstree "${PID}" | tr -d 0-9 | md5sum
}
scr2md() {
local file="$1" mdfile="$2"
printf 'Writing markdown file: %s\n' "${mdfile%.*}-transcript.md"
# Create the screencast markdown file
grep -Ev '(^screencast|^.MD)' "${file}" |
sed 's/^\.TS *//' >"${mdfile%.*}-transcript.md"
printf 'Writing transcript file: %s\n' "${mdfile}"
# Create the markdown document
[[ -n ${mdfile} ]] && {
# Create the Markdown document
grep -Ev '(^screencast |^#|^ *$|^\.TS)' "${file}" |
sed 's/^\.MD *//' >"${mdfile}"
}
printf 'Moving %s to %s.\n' "${VIDEO}" "images/$(basename "${mdfile%.*}.${EXT}")"
[[ -e ${VIDEO} ]] && {
mv "${VIDEO}" "images/$(basename "${mdfile%.*}.${EXT}")"
}
}
# PA - PArser
# _PA holds data specific to parsing the command line arguments.
declare -A _PA
# Declare externally defined variables ----------------------------------------
# Defined in ER (globals.sh)
declare OK ERROR STDERR
# Getters/Setters -------------------------------------------------------------
# PA_command getter outputs the _PA[command] array member. This contains the
# command the user requested.
PA_command() {
printf '%s' "${_PA[command]}"
}
# PA_subcommand getter outputs the _PA[subcommand] array member. This contains
# the subcommand the user requested.
PA_subcommand() {
printf '%s' "${_PA[subcommand]}"
}
# PA_shift outputs the _PA[shift] array member, returned by the caller
# when an extra shift is required whilst consuming option values.
#
# Example code:
# ```
#XX_process_options_callback() {
#
# case "$1" in
# -h | --help)
# CC_usage
# return "${STOP}"
# ;;
# ... omitted ...
# --k8sver)
# _CC[k8sver]="$2"
# return "$(PA_shift)"
# ;;
# --with-lb)
# ... omitted ..
# ```
PA_shift() {
printf '%s' "${_PA[shift]}"
}
# PA_set_state setter sets the initial state of the parser, which should be one
# of COMMAND, SUBCOMMAND, or ARG1.
# Args: arg1 - the initial state to set.
PA_set_state() {
_PA[state]="$1"
}
# Public Functions ------------------------------------------------------------
# PA_add_option_callback adds a callback to the list of callbacks used for
# processing options.
# Args: arg1 - Null string (for global options), COMMAND or COMMANDSUBCOMMAND.
# arg2 - The function to call.
PA_add_option_callback() {
_PA[optscallbacks]+="$1,$2 "
}
# PA_add_usage_callback adds a callback to the list of callbacks used for
# output of help text.
# Args: arg1 - Null string (for global help), COMMAND or COMMANDSUBCOMMAND.
# arg2 - The function to call.
PA_add_usage_callback() {
_PA[usagecallbacks]+="$1,$2 "
}
# PA_add_state adds a callback to the list of callbacks used for
# programming the state machine.
# Args: arg1 - Current state to match.
# arg2 - The value of the state to match.
# arg3 - The new state if arg1 and arg2 match.
# arg4 - The function to call, optional.
PA_add_state() {
_PA[statecallbacks]+="$1,$2,$3,$4 "
}
# PA_run implements an interleaved state machine to process the
# user request. It allows for strict checking of arguments and args. All
# command line arguments are processed in order from left to right.
#
# Each COMMAND can have a different set of requirements which are controlled
# by setting the next state at each transition.
#
# --global-options COMMAND --command-options SUBCOMMAND --subcommand-options \
# ARG1 --subcommand-options ARG2 --subcommand-options ...
#
# --global-options are those before COMMAND.
# --command-options can be after the COMMAND but before SUBCOMMAND.
# --subcommand-options can be anywhere after the SUBCOMMAND.
#
# Args: arg1-N - The arguments given to mokctl by the user on the command line
PA_run() {
set -- "$@"
local ARGN=$# ARGNUM=0 retval=0
while [ "${ARGN}" -ne 0 ]; do
case "$1" in
--* | -*)
_PA_process_option "$1" "$2" || {
retval=$?
if [[ ${retval} == "${_PA[shift]}" ]]; then
ARGN=$((ARGN - 1))
shift
else
return "${retval}"
fi
}
;;
*)
case "${_PA[state]}" in
COMMAND)
_PA_check_token "${1}" "COMMAND" "command"
[[ $? -eq ${ERROR} ]] && {
_PA_usage
printf 'Invalid COMMAND, "%s".\n\n' "$1" >"${STDERR}"
return "${ERROR}"
}
;;
SUBCOMMAND)
_PA_check_token "$1" "SUBCOMMAND" "subcommand"
[[ $? -eq ${ERROR} ]] && {
_PA_usage
printf 'Invalid SUBCOMMAND for %s, "%s".\n\n' "${_PA[command]}" "${1}" \
>"${STDERR}"
return "${ERROR}"
}
;;
ARG*)
((ARGNUM++))
_PA_check_token "${1}" "ARG${ARGNUM}"
[[ $? -eq ${ERROR} ]] && {
_PA_usage
printf 'Invalid ARG1 for %s %s, "%s".\n\n' "${_PA[command]}" \
"${_PA[subcommand]}" "${1}" >"${STDERR}"
return "${ERROR}"
}
;;
END)
_PA_usage
printf 'ERROR No more args expected, "%s" is unexpected for "%s %s"\n' \
"${1}" "${_PA[command]}" "${_PA[subcommand]}" >"${STDERR}"
return "${ERROR}"
;;
*)
printf 'Internal ERROR. Invalid state "%s"\n' "${_PA[state]}" >"${STDERR}"
return "${ERROR}"
;;
esac
;;
esac
shift 1
ARGN=$((ARGN - 1))
done
return "${OK}"
}
# Private Functions -----------------------------------------------------------
# PA_new sets the initial values for the PArser's associative array.
# Args: None expected.
_PA_new() {
_PA[command]=
_PA[subcommand]=
_PA[state]="COMMAND"
_PA[optscallbacks]=
_PA[usagecallbacks]=
# The return value if the caller asked for an extra shift:
_PA[shift]=126
}
# _PA_check_token checks for a valid token in arg2 state. The logic is
# most easily understood by reading the full original version at:
# https://github.com/mclarkson/my-own-kind/blob/master/docs/package.md#scripted-cluster-creation-and-deletion
# This function is a reduction of all the check_xxx_token functions.
# Args: arg1 - the token to check.
# arg2 - the current state.
# arg3 - the state value to set, optional. This should only be sent
# for command and subcommand states.
_PA_check_token() {
local item
if [[ -n ${_PA[subcommand]} ]]; then
cmdsubcommand="${_PA[command]}${_PA[subcommand]}"
elif [[ -n ${_PA[command]} && ${_PA[state]} != "ARG"* ]]; then
cmdsubcommand="${_PA[command]}$1"
elif [[ -n ${_PA[command]} && ${_PA[state]} == "ARG"* ]]; then
cmdsubcommand="${_PA[command]}"
elif [[ -z ${_PA[command]} && ${_PA[state]} == "ARG"* ]]; then
cmdsubcommand=
else
cmdsubcommand="$1"
fi
for item in ${_PA[statecallbacks]}; do
IFS=, read -r state component newstate func <<<"${item}"
[[ ${state} == "$2" ]] && {
[[ ${component} == "${cmdsubcommand}" ]] && {
[[ -n $3 ]] && _PA["$3"]="$1"
_PA[state]="${newstate}"
[[ -n ${func} ]] && {
eval "${func} $1" || return
}
return "${OK}"
}
}
done
}
# _PA_process_option checks that the user-provided option is valid for the
# command-subcommand or global states.
# Args: arg1 - The option to check.
# arg2 - TODO The value of the option if present, optional.
_PA_process_option() {
local item curcmdsubcmd
curcmdsubcmd="${_PA[command]}${_PA[subcommand]}"
for item in ${_PA[optscallbacks]}; do
IFS=, read -r cmdsubcmd func <<<"${item}"
[[ ${curcmdsubcmd} == "${cmdsubcmd}" ]] && {
eval "${func} \"$1\" \"$2\""
return $?
}
done
return "${ERROR}"
}
# _PA_usage outputs help text for a single component if help was asked for when
# a command was specified, or for all components otherwise.
# Args: None expected.
_PA_usage() {
curcmdsubcmd="${_PA[command]}${_PA[subcommand]}"
for item in ${_PA[usagecallbacks]}; do
IFS=, read -r cmdsubcmd func <<<"${item}"
[[ ${curcmdsubcmd} == "${cmdsubcmd}" ]] && {
eval "${func}"
return
}
done
eval "${_PA[usage]}"
}
# Initialise _PA
_PA_new
# vim helpers -----------------------------------------------------------------
#include globals.sh
# vim:ft=sh:sw=2:et:ts=2:
# Calls main() if we're called from the command line
if [ "$0" = "${BASH_SOURCE[0]}" ] || [ -z "${BASH_SOURCE[0]}" ]; then
main "$@"
fi
# vim:ft=sh:sw=2:et:ts=2: