saturnin.sh 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 SHELLFU_PATH SATURNIN_LIBEXEC SHELLFU_INIGREP_PATH
  72. case "$subcommand" in
  73. conf) inigrep "$@" ;;
  74. help) saturnin__help ;;
  75. _ls_subcommands) saturnin__lssc ;;
  76. _lsfun) shellfu-get lsfun ;;
  77. _lsmod) shellfu-get lsmod ;;
  78. *) saturnin__runsc "$subcommand" "$@" ;;
  79. esac
  80. }
  81. saturnin__runhook() {
  82. #
  83. # Run custom hook
  84. #
  85. local hname="$1"
  86. local hook_code
  87. test -n "$SATURNIN_SUBCOMMAND" || {
  88. warn "unknown subcommand, ignoring hook: $hname"
  89. return 0
  90. }
  91. hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
  92. debug -v SATURNIN_SUBCOMMAND hook_code hname
  93. bash -n <<<"$hook_code" || {
  94. warn "syntax errors, ignoring hook: $hname"
  95. return 0
  96. }
  97. eval "$hook_code"
  98. }
  99. saturnin__runsc() {
  100. #
  101. # Run subcommand $SATURNIN_SUBCOMMAND
  102. #
  103. local subcommand="$1"; shift
  104. local binpath # path to subcommand's binary
  105. binpath+="$SATURNIN_LIBEXEC/"
  106. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  107. debug -v binpath
  108. debug "\$*='$*'"
  109. test -x "$binpath" || {
  110. warn "invalid sub-command: $subcommand"
  111. saturnin__help
  112. return "$SHELLFU_EXIT_USAGE"
  113. }
  114. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  115. }
  116. saturnin__usage() {
  117. mkusage "[-d|-v] command [args...]" \
  118. "help" \
  119. "--version"
  120. }
  121. saturnin__version() {
  122. #
  123. # Print version info
  124. #
  125. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  126. local maybe_codename=""
  127. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  128. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  129. return "$SHELLFU_EXIT_OK"
  130. }
  131. saturnin__wraphook() {
  132. #
  133. # Wrap command "$@" in hooks
  134. #
  135. # Run pre hook, then "$@", then post hook. Always exit
  136. # with status of "$@", even if hooks fail. Ignore
  137. # post-hook if "$@" failed.
  138. #
  139. local es=0
  140. saturnin__runhook pre
  141. "$@" || return $?
  142. es=$?
  143. saturnin__runhook post
  144. return $es
  145. }