123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/bin/bash
-
- shellfu import exit
- shellfu import inigrep
- shellfu import pretty
-
- 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__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 binpath # path to subconnand's binary
- binpath+="$SATURNIN_LIBEXEC/"
- binpath+="$SATURNIN_LIBEXEC_PREFIX$SATURNIN_SUBCOMMAND"
- debug -v binpath
- debug "\$*='$*'"
- test -x "$binpath" || {
- warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
- saturnin__help
- return "$SHELLFU_EXIT_USAGE"
- }
- "$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
- }
|