saturnin.sh 4.5KB

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