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-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. *) 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. }