| 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 | # Definition of a "task"
|
| 14 | #
|
| 15 | # - File invokes task-five "$@"
|
| 16 | # - or maybe you can look at its source
|
| 17 | # - It's a shell function
|
| 18 | # - Has ### docstring
|
| 19 | # - Doesn't start with _
|
| 20 |
|
| 21 | : ${LIB_OSH=stdlib/osh}
|
| 22 | source $LIB_OSH/byo-server.sh
|
| 23 |
|
| 24 | _show-help() {
|
| 25 | # TODO:
|
| 26 | # - Use awk to find comments at the top of the file?
|
| 27 | # - Use OSH to extract docstrings
|
| 28 | # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
|
| 29 | # current file, not anything in a different file?
|
| 30 |
|
| 31 | echo "Usage: $0 TASK_NAME ARGS..."
|
| 32 | echo
|
| 33 | echo "To complete tasks, run:"
|
| 34 | echo " source devtools/completion.bash"
|
| 35 | echo
|
| 36 | echo "Tasks:"
|
| 37 |
|
| 38 | if command -v column >/dev/null; then
|
| 39 | _print-funcs | column
|
| 40 | else
|
| 41 | _print-funcs
|
| 42 | fi
|
| 43 | }
|
| 44 |
|
| 45 | task-five() {
|
| 46 | # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
|
| 47 | byo-maybe-run
|
| 48 |
|
| 49 | case ${1:-} in
|
| 50 | ''|--help|-h)
|
| 51 | _show-help
|
| 52 | exit 0
|
| 53 | ;;
|
| 54 | esac
|
| 55 |
|
| 56 | if ! declare -f "$1" >/dev/null; then
|
| 57 | echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
|
| 58 | exit 1
|
| 59 | fi
|
| 60 |
|
| 61 | "$@"
|
| 62 | }
|