2
0
mirror of https://github.com/bpkg/bpkg synced 2024-11-08 01:10:36 +00:00
bpkg/bpkg.sh

124 lines
2.2 KiB
Bash
Raw Normal View History

2014-05-22 19:52:55 +00:00
#!/bin/bash
2014-05-29 16:45:24 +00:00
## prevent sourcing
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
echo >&2 "error: \`bpkg' cannot be sourced"
return 1
fi
## bpkg version
2014-09-11 18:30:45 +00:00
VERSION="0.2.6"
2014-05-22 19:52:55 +00:00
## output error to stderr
error () {
printf >&2 "error: %s\n" "${@}"
}
## output usage
usage () {
2014-05-25 06:31:04 +00:00
echo ""
echo " usage: bpkg [-hV] <command> [args]"
echo ""
2014-05-22 19:52:55 +00:00
}
## commands
commands () {
{
declare -a local cmds=( $(
bpkg-suggest 'bpkg-' |
2014-05-26 04:36:49 +00:00
tail -n+2 |
sort -u |
2015-02-21 19:43:24 +00:00
sed 's/.*\/bpkg-//g' |
tr '\n' ' '
) )
echo "${cmds[@]}"
}
}
2014-05-22 19:52:55 +00:00
## feature tests
features () {
declare -a local features=(bpkg-json bpkg-suggest)
2014-05-24 19:29:07 +00:00
for ((i = 0; i < ${#features[@]}; ++i)); do
local f="${features[$i]}"
if ! type "${f}" > /dev/null 2>&1; then
error "Missing "${f}" dependency"
return 1
fi
done
2014-05-22 19:52:55 +00:00
}
bpkg () {
local arg="$1"
local cmd=""
shift
2014-05-24 19:29:07 +00:00
## test for required features
features || return $?
2014-05-22 19:52:55 +00:00
case "${arg}" in
## flags
-V|--version)
echo "${VERSION}"
return 0
;;
-h|--help)
usage
echo
echo "Here are some commands available in your path:"
echo
local cmds=($(commands))
for cmd in ${cmds[@]}; do
echo " ${cmd}"
done
2014-05-22 19:52:55 +00:00
return 0
;;
*)
if [ -z "${arg}" ]; then
usage
return 1
fi
2014-05-22 19:52:55 +00:00
cmd="bpkg-${arg}"
if type -f "${cmd}" > /dev/null 2>&1; then
"${cmd}" "${@}"
return $?
else
echo >&2 "error: \`${arg}' is not a bpkg command."
{
2014-05-26 04:36:49 +00:00
declare -a local res=($(commands))
if [ ! -z "${res}" ]; then
echo
echo >&2 "Did you mean one of these?"
found=0
for r in "${res[@]}"; do
2014-05-26 04:36:49 +00:00
if [[ "$r" == *"${arg}"* ]]; then
echo " $ bpkg ${r}"
found=1
2014-05-26 04:36:49 +00:00
fi
done
if [ "$found" == "0" ]; then
for r in "${res[@]}"; do
echo " $ bpkg ${r}"
done
fi
return 1
else
usage
return 1
fi
}
2014-05-22 19:52:55 +00:00
fi
;;
esac
2014-05-25 06:31:04 +00:00
usage
2014-05-26 04:36:49 +00:00
return 1
2014-05-22 19:52:55 +00:00
}
2014-05-29 16:45:24 +00:00
bpkg "${@}"
exit $?