1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Common shell functions for task scripts.
|
4 | #
|
5 | # Usage:
|
6 | # source $LIB_OSH/task-five.sh
|
7 | #
|
8 | # test-foo() { # define task functions
|
9 | # echo foo
|
10 | # }
|
11 | # task-five "$@"
|
12 |
|
13 | # List all functions defined in this file (and not in sourced files).
|
14 | _bash-print-funcs() {
|
15 | local funcs=($(compgen -A function))
|
16 | # extdebug makes `declare -F` print the file path, but, annoyingly, only
|
17 | # if you pass the function names as arguments.
|
18 | shopt -s extdebug
|
19 | declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
|
20 | shopt -u extdebug
|
21 | }
|
22 |
|
23 | _show-help() {
|
24 | # TODO:
|
25 | # - Use awk to find comments at the top of the file?
|
26 | # - Use OSH to extract docstrings
|
27 |
|
28 | echo "Usage: $0 TASK_NAME ARGS..."
|
29 | echo
|
30 | echo "To complete tasks, run:"
|
31 | echo " source devtools/completion.bash"
|
32 | echo
|
33 | echo "Tasks:"
|
34 |
|
35 | if command -v column >/dev/null; then
|
36 | _bash-print-funcs | column
|
37 | else
|
38 | _bash-print-funcs
|
39 | fi
|
40 | }
|
41 |
|
42 | task-five() {
|
43 | if [[ $# -eq 0 || $1 =~ ^(--help|-h)$ ]]; then
|
44 | _show-help
|
45 | exit
|
46 | fi
|
47 |
|
48 | if ! declare -f "$1" >/dev/null; then
|
49 | echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
|
50 | exit 1
|
51 | fi
|
52 |
|
53 | "$@"
|
54 | }
|