saturnin.sh 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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__main() {
  32. local subcommand
  33. while true; do case $1 in
  34. -d|--debug) export SHELLFU_DEBUG=true; shift ;;
  35. -v|--verbose) export SHELLFU_VERBOSE=true; shift ;;
  36. --version) saturnin__version; exit ;;
  37. --version-semver) echo "$SATURNIN_APP_VERSION"; exit ;;
  38. -*) saturnin__usage; ;;
  39. --*) saturnin__usage; ;;
  40. --) shift; break ;;
  41. "") saturnin__usage; ;;
  42. *) break; ;;
  43. esac done
  44. subcommand="$1"; shift
  45. debug -v subcommand
  46. debug -v SHELLFU_PATH SATURNIN_LIBEXEC SHELLFU_INIGREP_PATH
  47. debug "\$*='$*'"
  48. case "$subcommand" in
  49. conf) inigrep "$@" ;;
  50. help) saturnin__help ;;
  51. _ls_subcommands) saturnin__lssc ;;
  52. _lsfun) shellfu-get lsfun ;;
  53. _lsmod) shellfu-get lsmod ;;
  54. _ffrun) ffrun "$@" ;;
  55. *) saturnin__runsc "$subcommand" "$@" ;;
  56. esac
  57. }
  58. saturnin__runhook() {
  59. #
  60. # Run custom hook
  61. #
  62. local hname="$1"
  63. local hook_code
  64. test -n "$SATURNIN_SUBCOMMAND" || {
  65. warn "unknown subcommand, ignoring hook: $hname"
  66. return 0
  67. }
  68. hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
  69. debug -v SATURNIN_SUBCOMMAND hook_code hname
  70. bash -n <<<"$hook_code" || {
  71. warn "syntax errors, ignoring hook: $hname"
  72. return 0
  73. }
  74. eval "$hook_code"
  75. }
  76. saturnin__runsc() {
  77. #
  78. # Run subcommand $SATURNIN_SUBCOMMAND
  79. #
  80. local subcommand="$1"; shift
  81. local binpath # path to subcommand's binary
  82. binpath+="$SATURNIN_LIBEXEC/"
  83. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  84. debug -v binpath
  85. debug "\$*='$*'"
  86. test -x "$binpath" || {
  87. warn "invalid sub-command: $subcommand"
  88. saturnin__help
  89. return "$SHELLFU_EXIT_USAGE"
  90. }
  91. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  92. }
  93. saturnin__usage() {
  94. mkusage "[-d|-v] command [args...]" \
  95. "help" \
  96. "--version"
  97. }
  98. saturnin__version() {
  99. #
  100. # Print version info
  101. #
  102. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  103. local maybe_codename=""
  104. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  105. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  106. return "$SHELLFU_EXIT_OK"
  107. }
  108. saturnin__wraphook() {
  109. #
  110. # Wrap command "$@" in hooks
  111. #
  112. # Run pre hook, then "$@", then post hook. Always exit
  113. # with status of "$@", even if hooks fail. Ignore
  114. # post-hook if "$@" failed.
  115. #
  116. local es=0
  117. saturnin__runhook pre
  118. "$@" || return $?
  119. es=$?
  120. saturnin__runhook post
  121. return $es
  122. }