#!/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__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__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 }