saturnin_meta.sh 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. ffoo import exit
  3. ffoo import inigrep
  4. ffoo import pretty
  5. saturnin_help() {
  6. {
  7. echo "built-in commands:"
  8. echo " conf"
  9. echo " help"
  10. echo ""
  11. echo "installed commands:"
  12. saturnin_lssc \
  13. | sed -e 's/^/ /'
  14. } | mkhelp -E -f -
  15. return "$FFOO_EXIT_OK"
  16. }
  17. saturnin_lssc() {
  18. find "$SATURNIN_LIBEXEC" \
  19. -mindepth 1 \
  20. -maxdepth 1 \
  21. -executable \
  22. | sed -e 's/.*saturnin-//' \
  23. | sort
  24. }
  25. saturnin_runhook() {
  26. #
  27. # Run custom hook
  28. #
  29. local hname="$1"
  30. local hook_code
  31. test -n "$SATURNIN_SUBCOMMAND" || {
  32. warn "unknown subcommand, ignoring hook: $hname"
  33. return 0
  34. }
  35. hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
  36. debug -v SATURNIN_SUBCOMMAND hook_code hname
  37. bash -n <<<"$hook_code" || {
  38. warn "syntax errors, ignoring hook: $hname"
  39. return 0
  40. }
  41. eval "$hook_code"
  42. }
  43. saturnin_runsc() {
  44. local lexpath="$SATURNIN_LIBEXEC/saturnin-$SATURNIN_SUBCOMMAND"
  45. debug -v lexpath
  46. debug "\$*='$*'"
  47. test -x "$lexpath" || {
  48. warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
  49. saturnin_help
  50. return "$FFOO_EXIT_USAGE"
  51. }
  52. "$lexpath" "$@"
  53. }
  54. saturnin_wraphook() {
  55. #
  56. # Wrap command "$@" in hooks
  57. #
  58. # Run pre hook, then "$@", then post hook. Always exit
  59. # with status of "$@", even if hooks fail. Ignore
  60. # post-hook if "$@" failed.
  61. #
  62. local es=0
  63. saturnin_runhook pre
  64. "$@" || return $?
  65. es=$?
  66. saturnin_runhook post
  67. return $es
  68. }