123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #!/bin/bash
-
- shellfu import exit
- shellfu import inigrep
- shellfu import pretty
-
- saturnin__get() {
- #
- # Show Saturnin Internal info by key $1 and exit
- #
- # Key $1 can be whole `--saturnin-get-stuff` argument or just
- # the part after `--saturnin-get-`.
- #
- # This is aimed to help debugging and testing the app (or
- # Saturnin itself) by showing packaging and deployment related
- # info.
- #
- local key=${1#--saturnin-get-}
- case "$key" in
- shellfu-path) echo "$SHELLFU_PATH" ;;
- inigrep-path) echo "$SHELLFU_INIGREP_PATH" ;;
- app-version) echo "$SATURNIN_APP_VERSION" ;;
- cache-home) echo "$SATURNIN_CACHE_HOME" ;;
- libexec) echo "$SATURNIN_LIBEXEC" ;;
- libexec-prefix) echo "$SATURNIN_LIBEXEC_PREFIX" ;;
- *) warn "unknown devel key: $key"
- exit "$SHELLFU_EXIT_USAGE" ;;
- esac
- exit "$SHELLFU_EXIT_OK"
- }
-
- saturnin__help() {
- #
- # Print simple help message (basically just list of commands)
- #
- {
- echo "built-in commands:"
- echo " conf"
- echo " help"
- echo ""
- echo "installed commands:"
- saturnin__lssc \
- | sed -e 's/^/ /'
- } | mkhelp -E -f -
- return "$SHELLFU_EXIT_OK"
- }
-
- saturnin__lssc() {
- #
- # List subcommands
- #
- find "$SATURNIN_LIBEXEC" \
- -mindepth 1 \
- -maxdepth 1 \
- -executable \
- | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
- | sort
- }
-
- saturnin__main() {
- local subcommand
- while true; do case $1 in
- -d|--debug) export SHELLFU_DEBUG=true; shift ;;
- -v|--verbose) export SHELLFU_VERBOSE=true; shift ;;
- --version) saturnin__version; exit ;;
- --version-semver) echo "$SATURNIN_APP_VERSION"; exit ;;
- --saturnin-get-*) saturnin__get "$1" ;;
- -*) saturnin__usage; ;;
- --*) saturnin__usage; ;;
- --) shift; break ;;
- "") saturnin__usage; ;;
- *) break; ;;
- esac done
- subcommand="$1"; shift
- debug -v subcommand
- debug -v SHELLFU_PATH SATURNIN_LIBEXEC SHELLFU_INIGREP_PATH
- debug "\$*='$*'"
- case "$subcommand" in
- conf) inigrep "$@" ;;
- help) saturnin__help ;;
- _ls_subcommands) saturnin__lssc ;;
- _lsfun) shellfu-get lsfun ;;
- _lsmod) shellfu-get lsmod ;;
- _ffrun) ffrun "$@" ;;
- *) saturnin__runsc "$subcommand" "$@" ;;
- esac
- }
-
- saturnin__runhook() {
- #
- # Run custom hook
- #
- local hname="$1"
- local hook_code
- test -n "$SATURNIN_SUBCOMMAND" || {
- warn "unknown subcommand, ignoring hook: $hname"
- return 0
- }
- hook_code="$(inigrep -j -p "hook.$SATURNIN_SUBCOMMAND.$hname")"
- debug -v SATURNIN_SUBCOMMAND hook_code hname
- bash -n <<<"$hook_code" || {
- warn "syntax errors, ignoring hook: $hname"
- return 0
- }
- eval "$hook_code"
- }
-
- saturnin__runsc() {
- #
- # Run subcommand $SATURNIN_SUBCOMMAND
- #
- local subcommand="$1"; shift
- local binpath # path to subcommand's binary
- binpath+="$SATURNIN_LIBEXEC/"
- binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
- debug -v binpath
- debug "\$*='$*'"
- test -x "$binpath" || {
- warn "invalid sub-command: $subcommand"
- saturnin__help
- return "$SHELLFU_EXIT_USAGE"
- }
- SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
- }
-
- saturnin__usage() {
- mkusage "[-d|-v] command [args...]" \
- "help" \
- "--version"
- }
-
- saturnin__version() {
- #
- # Print version info
- #
- local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
- local maybe_codename=""
- test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
- echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
- return "$SHELLFU_EXIT_OK"
- }
-
- saturnin__wraphook() {
- #
- # Wrap command "$@" in hooks
- #
- # Run pre hook, then "$@", then post hook. Always exit
- # with status of "$@", even if hooks fail. Ignore
- # post-hook if "$@" failed.
- #
- local es=0
- saturnin__runhook pre
- "$@" || return $?
- es=$?
- saturnin__runhook post
- return $es
- }
|