#!/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-git-hash) echo "$SATURNIN_APP_GIT_HASH" ;; 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) saturnin__get app-version ;; --saturnin-get-*) saturnin__get "$1" ;; -*) saturnin__usage; ;; --*) saturnin__usage; ;; --) shift; break ;; "") saturnin__usage; ;; *) break; ;; esac done subcommand="$1"; shift debug -v SHELLFU_PATH SATURNIN_LIBEXEC SHELLFU_INIGREP_PATH case "$subcommand" in conf) inigrep "$@" ;; help) saturnin__help ;; _ls_subcommands) saturnin__lssc ;; _lsfun) shellfu-get lsfun ;; _lsmod) shellfu-get lsmod ;; *) 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 }