saturnin.sh 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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__version() {
  66. #
  67. # Print version info
  68. #
  69. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  70. local maybe_codename=""
  71. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  72. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  73. return "$SHELLFU_EXIT_OK"
  74. }
  75. saturnin__wraphook() {
  76. #
  77. # Wrap command "$@" in hooks
  78. #
  79. # Run pre hook, then "$@", then post hook. Always exit
  80. # with status of "$@", even if hooks fail. Ignore
  81. # post-hook if "$@" failed.
  82. #
  83. local es=0
  84. saturnin__runhook pre
  85. "$@" || return $?
  86. es=$?
  87. saturnin__runhook post
  88. return $es
  89. }