12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #!/bin/bash
-
- shellfu import exit
- shellfu import inigrep
- shellfu import pretty
-
- saturnin__help() {
- #
- # Print simple help message (basically just list of commands)
- #
- {
- echo "built-in commands:"
- echo " conf"
- echo " help"
- echo ""
- echo "installed commands:"
- saturnin__lssc \
- | sed -e 's/^/ /'
- } | mkhelp -E -f -
- return "$SHELLFU_EXIT_OK"
- }
-
- saturnin__lssc() {
- #
- # List subcommands
- #
- find "$SATURNIN_LIBEXEC" \
- -mindepth 1 \
- -maxdepth 1 \
- -executable \
- | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
- | sort
- }
-
- saturnin__runhook() {
- #
- # Run custom hook
- #
- local hname="$1"
- local hook_code
- test -n "$SATURNIN_SUBCOMMAND" || {
- warn "unknown subcommand, ignoring hook: $hname"
- return 0
- }
- hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
- debug -v SATURNIN_SUBCOMMAND hook_code hname
- bash -n <<<"$hook_code" || {
- warn "syntax errors, ignoring hook: $hname"
- return 0
- }
- eval "$hook_code"
- }
-
- saturnin__runsc() {
- #
- # Run subcommand $SATURNIN_SUBCOMMAND
- #
- local lexpath
- lexpath+="$SATURNIN_LIBEXEC/"
- lexpath+="$SATURNIN_LIBEXEC_PREFIX$SATURNIN_SUBCOMMAND"
- debug -v lexpath
- debug "\$*='$*'"
- test -x "$lexpath" || {
- warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
- saturnin__help
- return "$SHELLFU_EXIT_USAGE"
- }
- "$lexpath" "$@"
- }
-
- saturnin__wraphook() {
- #
- # Wrap command "$@" in hooks
- #
- # Run pre hook, then "$@", then post hook. Always exit
- # with status of "$@", even if hooks fail. Ignore
- # post-hook if "$@" failed.
- #
- local es=0
- saturnin__runhook pre
- "$@" || return $?
- es=$?
- saturnin__runhook post
- return $es
- }
|