41 lines
1.1 KiB
Bash
41 lines
1.1 KiB
Bash
|
|
# shellcheck shell=bash
|
||
|
|
#================================================================
|
||
|
|
#% SYNOPSIS
|
||
|
|
#+ current <cmd> [-f <flag>] [-r <regex>]
|
||
|
|
#%
|
||
|
|
#% OPTIONS
|
||
|
|
#% -f set command-line args to pass to <cmd>; single string.
|
||
|
|
#% Default: [--version]
|
||
|
|
#% -r set regex used to extract version string from cmd output
|
||
|
|
#% Default: [v?(\d+\.\d+\.\d+)]
|
||
|
|
#================================================================
|
||
|
|
current() {
|
||
|
|
flag="--version"
|
||
|
|
regex='v?(\d+\.\d+\.\d+)'
|
||
|
|
declare -a args
|
||
|
|
OPTIND=1
|
||
|
|
while [ $OPTIND -le "$#" ]; do
|
||
|
|
if getopts :f:r: opt; then
|
||
|
|
case "$opt" in
|
||
|
|
\?)
|
||
|
|
diag "$(red "error: called 'current' badly")"
|
||
|
|
;;
|
||
|
|
f) flag="$OPTARG" ;;
|
||
|
|
r) regex="$OPTARG" ;;
|
||
|
|
esac
|
||
|
|
else
|
||
|
|
args+=("${!OPTIND}")
|
||
|
|
(( OPTIND++ ))
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
set -- "${args[@]}"
|
||
|
|
|
||
|
|
cmd="$1"
|
||
|
|
|
||
|
|
if command -v "$cmd" >/dev/null; then
|
||
|
|
bash -c "$cmd $flag" | perl -ne 'm/'"$regex"'/ && print "$1\n"' || true
|
||
|
|
else
|
||
|
|
true
|
||
|
|
fi
|
||
|
|
}
|