saturnin.sh 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. shellfu import exit
  3. shellfu import inigrep
  4. shellfu import pretty
  5. saturnin__help() {
  6. #
  7. # Print simple help message (basically just list of commands)
  8. #
  9. {
  10. echo "built-in commands:"
  11. echo " conf"
  12. echo " help"
  13. echo ""
  14. echo "installed commands:"
  15. saturnin__lssc \
  16. | sed -e 's/^/ /'
  17. } | mkhelp -E -f -
  18. return "$SHELLFU_EXIT_OK"
  19. }
  20. saturnin__lssc() {
  21. #
  22. # List subcommands
  23. #
  24. find "$SATURNIN_LIBEXEC" \
  25. -mindepth 1 \
  26. -maxdepth 1 \
  27. -executable \
  28. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  29. | sort
  30. }
  31. saturnin__runhook() {
  32. #
  33. # Run custom hook
  34. #
  35. local hname="$1"
  36. local hook_code
  37. test -n "$SATURNIN_SUBCOMMAND" || {
  38. warn "unknown subcommand, ignoring hook: $hname"
  39. return 0
  40. }
  41. hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
  42. debug -v SATURNIN_SUBCOMMAND hook_code hname
  43. bash -n <<<"$hook_code" || {
  44. warn "syntax errors, ignoring hook: $hname"
  45. return 0
  46. }
  47. eval "$hook_code"
  48. }
  49. saturnin__runsc() {
  50. #
  51. # Run subcommand $SATURNIN_SUBCOMMAND
  52. #
  53. local binpath # path to subconnand's binary
  54. binpath+="$SATURNIN_LIBEXEC/"
  55. binpath+="$SATURNIN_LIBEXEC_PREFIX$SATURNIN_SUBCOMMAND"
  56. debug -v binpath
  57. debug "\$*='$*'"
  58. test -x "$binpath" || {
  59. warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
  60. saturnin__help
  61. return "$SHELLFU_EXIT_USAGE"
  62. }
  63. "$binpath" "$@"
  64. }
  65. saturnin__wraphook() {
  66. #
  67. # Wrap command "$@" in hooks
  68. #
  69. # Run pre hook, then "$@", then post hook. Always exit
  70. # with status of "$@", even if hooks fail. Ignore
  71. # post-hook if "$@" failed.
  72. #
  73. local es=0
  74. saturnin__runhook pre
  75. "$@" || return $?
  76. es=$?
  77. saturnin__runhook post
  78. return $es
  79. }