saturnin.sh 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. shellfu import exit
  3. shellfu import inigrep
  4. shellfu import pretty
  5. saturnin__get() {
  6. #
  7. # Show Saturnin Internal info by key $1 and exit
  8. #
  9. # Key $1 can be whole `--saturnin-get-stuff` argument or just
  10. # the part after `--saturnin-get-`.
  11. #
  12. # This is aimed to help debugging and testing the app (or
  13. # Saturnin itself) by showing packaging and deployment related
  14. # info.
  15. #
  16. local key=${1#--saturnin-get-}
  17. case "$key" in
  18. shellfu-path) echo "$SHELLFU_PATH" ;;
  19. inigrep-path) echo "$SHELLFU_INIGREP_PATH" ;;
  20. app-version) echo "$SATURNIN_APP_VERSION" ;;
  21. cache-home) echo "$SATURNIN_CACHE_HOME" ;;
  22. libexec) echo "$SATURNIN_LIBEXEC" ;;
  23. libexec-prefix) echo "$SATURNIN_LIBEXEC_PREFIX" ;;
  24. *) warn "unknown devel key: $key"
  25. exit "$SHELLFU_EXIT_USAGE" ;;
  26. esac
  27. exit "$SHELLFU_EXIT_OK"
  28. }
  29. saturnin__help() {
  30. #
  31. # Print simple help message (basically just list of commands)
  32. #
  33. {
  34. echo "built-in commands:"
  35. echo " conf"
  36. echo " help"
  37. echo ""
  38. echo "installed commands:"
  39. saturnin__lssc \
  40. | sed -e 's/^/ /'
  41. } | mkhelp -E -f -
  42. return "$SHELLFU_EXIT_OK"
  43. }
  44. saturnin__lssc() {
  45. #
  46. # List subcommands
  47. #
  48. find "$SATURNIN_LIBEXEC" \
  49. -mindepth 1 \
  50. -maxdepth 1 \
  51. -executable \
  52. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  53. | sort
  54. }
  55. saturnin__main() {
  56. local subcommand
  57. while true; do case $1 in
  58. -d|--debug) export SHELLFU_DEBUG=true; shift ;;
  59. -v|--verbose) export SHELLFU_VERBOSE=true; shift ;;
  60. --version) saturnin__version; exit ;;
  61. --version-semver) echo "$SATURNIN_APP_VERSION"; exit ;;
  62. --saturnin-get-*) saturnin__get "$1" ;;
  63. -*) saturnin__usage; ;;
  64. --*) saturnin__usage; ;;
  65. --) shift; break ;;
  66. "") saturnin__usage; ;;
  67. *) break; ;;
  68. esac done
  69. subcommand="$1"; shift
  70. debug -v subcommand
  71. debug -v SHELLFU_PATH SATURNIN_LIBEXEC SHELLFU_INIGREP_PATH
  72. debug "\$*='$*'"
  73. case "$subcommand" in
  74. conf) inigrep "$@" ;;
  75. help) saturnin__help ;;
  76. _ls_subcommands) saturnin__lssc ;;
  77. _lsfun) shellfu-get lsfun ;;
  78. _lsmod) shellfu-get lsmod ;;
  79. _ffrun) ffrun "$@" ;;
  80. *) saturnin__runsc "$subcommand" "$@" ;;
  81. esac
  82. }
  83. saturnin__runhook() {
  84. #
  85. # Run custom hook
  86. #
  87. local hname="$1"
  88. local hook_code
  89. test -n "$SATURNIN_SUBCOMMAND" || {
  90. warn "unknown subcommand, ignoring hook: $hname"
  91. return 0
  92. }
  93. hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
  94. debug -v SATURNIN_SUBCOMMAND hook_code hname
  95. bash -n <<<"$hook_code" || {
  96. warn "syntax errors, ignoring hook: $hname"
  97. return 0
  98. }
  99. eval "$hook_code"
  100. }
  101. saturnin__runsc() {
  102. #
  103. # Run subcommand $SATURNIN_SUBCOMMAND
  104. #
  105. local subcommand="$1"; shift
  106. local binpath # path to subcommand's binary
  107. binpath+="$SATURNIN_LIBEXEC/"
  108. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  109. debug -v binpath
  110. debug "\$*='$*'"
  111. test -x "$binpath" || {
  112. warn "invalid sub-command: $subcommand"
  113. saturnin__help
  114. return "$SHELLFU_EXIT_USAGE"
  115. }
  116. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  117. }
  118. saturnin__usage() {
  119. mkusage "[-d|-v] command [args...]" \
  120. "help" \
  121. "--version"
  122. }
  123. saturnin__version() {
  124. #
  125. # Print version info
  126. #
  127. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  128. local maybe_codename=""
  129. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  130. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  131. return "$SHELLFU_EXIT_OK"
  132. }
  133. saturnin__wraphook() {
  134. #
  135. # Wrap command "$@" in hooks
  136. #
  137. # Run pre hook, then "$@", then post hook. Always exit
  138. # with status of "$@", even if hooks fail. Ignore
  139. # post-hook if "$@" failed.
  140. #
  141. local es=0
  142. saturnin__runhook pre
  143. "$@" || return $?
  144. es=$?
  145. saturnin__runhook post
  146. return $es
  147. }