Created
October 14, 2023 22:45
-
-
Save roktas/c02575ed4582173d218ebd50b07e86fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -Eeuo pipefail; shopt -s nullglob; [[ -z ${TRACE:-} ]] || set -x; unset CDPATH; IFS=$' \t\n' | |
cd "$(dirname "$(readlink -f "$0")")"/../.. || exit | |
# shellcheck disable=2034 | |
declare -gr PROGNAME=${0##*/} # Program name | |
cry() { | |
echo "$*" >&2 | |
} | |
die() { | |
echo "$*" >&2 | |
exit 1 | |
} | |
is_valid() { | |
local dir=${1?${FUNCNAME[0]}: missing argument}; shift | |
[[ -d $dir/.local/bin ]] | |
} | |
is_valid-() { | |
is_valid "$*" || die "Directory not valid: $*" | |
} | |
single() { | |
local dir=${1?${FUNCNAME[0]}: missing argument}; shift | |
is_valid- "$dir" | |
local exe=.local/bin/$route; ( | |
cd "$dir" || exit | |
[[ -x $exe ]] || die "No executable found for $dir: $route" | |
cry "--> $dir $route $*" | |
exec "$exe" "$@" | |
) | |
} | |
all() { | |
local dir | |
for dir in *; do | |
is_valid "$dir" || continue | |
single "$dir" | |
done | |
} | |
main() { | |
[[ $# -gt 0 ]] || die "Usage: $PROGNAME <route> [<directory> [args...]]" | |
declare -gr route=$1 | |
shift | |
if [[ $# -gt 0 ]]; then | |
single "$@" | |
else | |
all | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment