saturnin_meta.sh 1.6KB

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