#!/bin/bash
# functions - Bash function support
set -e
######
# Function API
# is_function() - Returns success if $1 names a function
is_function() {
has_args 1 "$@"
declare -pf "$1" >/dev/null 2>&1
}
# func_deprecated() - Declare a deprectated function ($1) as an alias
# to another function.
func_deprecate() {
has_args 2 "$@"
local old=$1
local new=$2
eval "$old() { warn \"$old: deprecated, use $new\"; $new \"\$@\"; }"
}
# func_copy() - Makes a copy of named function ($1) as a new name ($2)
func_copy() {
has_args 2 "$@"
local src=$1
local dst=$2
local scode=$(declare -f $src)
local dcode="$dst${scode#$src}"
eval "$dcode"
}
# func_chain() - Overrides an existing function with a new body, copying
# the original function such that it can be called by the new function.
# e.g. 'func chain a b' installs 'a_b' as 'b'; 'a_b' can use 'a_b_next'
# to call the original 'b' (which may itself be a chained function).
func_chain() {
min_args 2 "$@"
local name=$1
shift
local func
for func in "$@"; do
func_copy "$func" "${name}_${func}_next"
func_copy "${name}_${func}" "$func"
done
}
# func_delete() - Delets the name function ($1)
func_delete() { min_args 1 "$@"; unset -f "$@"; }
# func_save() - Saves a copy of the function ($1) using a new name ($2).
# Currently, this is an alias for func_copy(), but that may change.
func_save() { func_copy "$@"; }
# func_restore() - Restores the function ($1) using a saved copy ($2),
# deleting the copy when finished.
func_restore() { func_copy "$2" "$1"; unset -f "$2"; }
# func_trace() - Prints debug string of the caller function and arguments ($@)
func_trace() {
local -a args=( "$@" )
list_quote args
debug "FUNC: ${FUNCNAME[1]} ${args[*]}"
}
######
# Boolean Function Wrappers
# func_bool() - Executes arguments ($@) and prints a boolean command
# that produces the same result: ``true`` or ``false``. This function
# $1 - Command
# $@ - Command arguments (optional)
func_bool() { "$@" && echo "true" || echo "false"; }
# func bool_not() - Like ``func_bool()``, executes a command ($@), but
# prints a boolean command that produces the complement of its result.
# $1 - Command
# $@ - Command arguments (optional)
func_bool_not() { "$@" && echo "false" || echo "true"; }
Generated on Thu May 4 18:59:21 PDT 2017 by mcsh i7 v0.19.0.