saturnin_meta.sh 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  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
  45. lexpath+="$SATURNIN_LIBEXEC/"
  46. lexpath+="$SATURNIN_LIBEXEC_PREFIX$SATURNIN_SUBCOMMAND"
  47. debug -v lexpath
  48. debug "\$*='$*'"
  49. test -x "$lexpath" || {
  50. warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
  51. saturnin_help
  52. return "$FFOO_EXIT_USAGE"
  53. }
  54. "$lexpath" "$@"
  55. }
  56. saturnin_wraphook() {
  57. #
  58. # Wrap command "$@" in hooks
  59. #
  60. # Run pre hook, then "$@", then post hook. Always exit
  61. # with status of "$@", even if hooks fail. Ignore
  62. # post-hook if "$@" failed.
  63. #
  64. local es=0
  65. saturnin_runhook pre
  66. "$@" || return $?
  67. es=$?
  68. saturnin_runhook post
  69. return $es
  70. }