2017-08-28 17:41:32 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
## Important so that version is not extracted for failed commands (not found)
|
|
|
|
set -o pipefail
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
## constant - symbols for success failure
|
2017-08-28 17:41:32 +02:00
|
|
|
PASS='✔'
|
|
|
|
FAIL='✘'
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
## These variables are used to keep track of passed and failed commands
|
2017-08-29 05:44:08 +02:00
|
|
|
OK=0
|
|
|
|
KO=0
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
## Regex to extract simple version - extracts numeric sem-ver style versions
|
2017-08-28 17:41:32 +02:00
|
|
|
REGEX_SIMPLE_VERSION="([[:digit:]]+\.?){2,3}"
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
|
|
|
|
## try to extract version by executing $1 with $2 arg
|
2017-08-28 17:41:32 +02:00
|
|
|
__dynamic_detect(){
|
|
|
|
cmd=$1
|
|
|
|
params=$2
|
|
|
|
version=$(eval ${cmd} ${params} "2>&1" | egrep -o "$REGEX_SIMPLE_VERSION" | head -1)
|
|
|
|
status=$?
|
|
|
|
}
|
|
|
|
|
|
|
|
# commands that use `--version` flag
|
|
|
|
__dynamic_detect--version(){
|
|
|
|
__dynamic_detect $1 "--version"
|
|
|
|
}
|
|
|
|
|
|
|
|
## commands that use `-version` flag
|
|
|
|
__dynamic_detect-version(){
|
|
|
|
__dynamic_detect $1 "-version"
|
|
|
|
}
|
|
|
|
|
|
|
|
# commands that use `-v` flag
|
|
|
|
__dynamic_detect-v(){
|
|
|
|
__dynamic_detect $1 "-v"
|
|
|
|
}
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
# commands that use `version` argument
|
|
|
|
__dynamic_detect-arg_version(){
|
|
|
|
__dynamic_detect $1 "version"
|
|
|
|
}
|
|
|
|
|
2017-08-28 17:41:32 +02:00
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
## the main function
|
2017-08-28 17:41:32 +02:00
|
|
|
__detect(){
|
|
|
|
name=$1
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
# setup aliases maps commonly used name to exact command name
|
2017-08-28 17:41:32 +02:00
|
|
|
case ${name} in
|
|
|
|
golang) command="go" ;;
|
|
|
|
jre) command="java" ;;
|
|
|
|
jdk) command="javac" ;;
|
|
|
|
nodejs) command="node" ;;
|
|
|
|
goreplay) command="gor";;
|
2017-10-14 08:58:23 +02:00
|
|
|
httpie) command="http";;
|
2017-08-28 17:41:32 +02:00
|
|
|
*) command=${name} ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case ${command} in
|
2017-08-29 08:31:11 +02:00
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
# commands that need --version flag
|
2017-10-14 08:58:23 +02:00
|
|
|
bash|zsh) __dynamic_detect--version ${command} ;;
|
2017-08-29 08:31:11 +02:00
|
|
|
git|hg|svn|bzr) __dynamic_detect--version ${command} ;;
|
2017-10-14 08:58:23 +02:00
|
|
|
gcc|make) __dynamic_detect--version ${command} ;;
|
|
|
|
curl|wget|http) __dynamic_detect--version ${command} ;;
|
|
|
|
vim|emacs|nano|subl) __dynamic_detect--version ${command} ;;
|
|
|
|
bats|tree|ack|autojump) __dynamic_detect--version ${command} ;;
|
|
|
|
jq|ag|brew) __dynamic_detect--version ${command} ;;
|
|
|
|
|
2017-10-14 09:05:23 +02:00
|
|
|
node|npm|yarn) __dynamic_detect--version ${command} ;;
|
2017-08-29 08:31:11 +02:00
|
|
|
ruby|gem|rake|bundle) __dynamic_detect--version ${command} ;;
|
|
|
|
python|python3) __dynamic_detect--version ${command} ;;
|
2017-08-31 20:08:11 +02:00
|
|
|
perl|php) __dynamic_detect--version ${command} ;;
|
2017-08-29 08:31:11 +02:00
|
|
|
groovy|gradle|mvn) __dynamic_detect--version ${command} ;;
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
# commands that need -version flag
|
2017-08-29 08:31:11 +02:00
|
|
|
ant|java|javac) __dynamic_detect-version ${command} ;;
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
# commands that need version arg
|
|
|
|
hugo) __dynamic_detect-arg_version ${command} ;;
|
2017-08-29 08:31:11 +02:00
|
|
|
|
|
|
|
## Example of commands that need custom processing
|
2017-10-14 08:47:01 +02:00
|
|
|
## go needs version arg
|
2017-08-28 17:41:32 +02:00
|
|
|
go)
|
|
|
|
version=$(go version 2>&1| egrep -o "$REGEX_SIMPLE_VERSION" | head -1)
|
|
|
|
status=$?
|
|
|
|
;;
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
## ab uses -V flag
|
2017-08-28 17:41:32 +02:00
|
|
|
ab)
|
|
|
|
version=$(ab -V 2>&1 | egrep -o "$REGEX_SIMPLE_VERSION" | head -1)
|
|
|
|
status=$?
|
|
|
|
;;
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
## gor returns version but does not return normal status code, hence needs custom processing
|
2017-08-28 17:41:32 +02:00
|
|
|
gor)
|
|
|
|
version=$(gor version 2>&1 | egrep -o "$REGEX_SIMPLE_VERSION" | head -1)
|
|
|
|
if [ $? -eq 1 ]; then status=0; else status=1; fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
2017-10-14 08:47:01 +02:00
|
|
|
## Can allow dynamic checking here, i.e. checking commands that are not listed above
|
2017-08-28 17:41:32 +02:00
|
|
|
if [[ "${HAS_ALLOW_UNSAFE}" == "y" ]]; then
|
|
|
|
__dynamic_detect--version ${command}
|
2017-10-14 08:47:01 +02:00
|
|
|
## fallback checking based on status!=127 (127 means command not found)
|
|
|
|
## TODO can check other type of supported version-checks if status was not 127
|
2017-08-28 17:41:32 +02:00
|
|
|
else
|
2017-10-14 08:47:01 +02:00
|
|
|
## -1 is special way to tell command is not supported/whitelisted by `has`
|
|
|
|
status="-1"
|
2017-08-28 17:41:32 +02:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2017-10-14 08:47:01 +02:00
|
|
|
|
|
|
|
if [ "$status" -eq "-1" ]; then ## When unsafe processing is not allowed, the -1 signifies
|
2017-08-28 17:41:32 +02:00
|
|
|
|
|
|
|
echo ${FAIL} ${command} "not understood"
|
2017-08-29 05:44:08 +02:00
|
|
|
KO=$(($KO+1))
|
2017-10-14 08:47:01 +02:00
|
|
|
|
|
|
|
elif [ ${status} -eq 127 ]; then ## command not installed
|
2017-08-28 17:41:32 +02:00
|
|
|
|
|
|
|
echo ${FAIL} ${command}
|
2017-08-29 05:44:08 +02:00
|
|
|
KO=$(($KO+1))
|
2017-10-14 08:47:01 +02:00
|
|
|
|
|
|
|
elif [ ${status} -eq 0 ]; then ## successfully executed
|
2017-08-28 17:41:32 +02:00
|
|
|
|
|
|
|
echo ${PASS} ${command} ${version}
|
2017-08-29 05:44:08 +02:00
|
|
|
OK=$(($OK+1))
|
2017-10-14 08:47:01 +02:00
|
|
|
|
|
|
|
else ## as long as its not 127, command is there, but we might not have been able to extract version
|
2017-08-28 17:41:32 +02:00
|
|
|
|
|
|
|
echo ${PASS} ${command}
|
2017-08-29 05:44:08 +02:00
|
|
|
OK=$(($OK+1))
|
2017-08-28 17:41:32 +02:00
|
|
|
fi
|
2017-08-29 05:44:08 +02:00
|
|
|
|
2017-08-28 17:41:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# if no arguments passed to script
|
|
|
|
if [ "$#" -eq 0 ]; then
|
|
|
|
# print help
|
2017-08-29 08:31:11 +02:00
|
|
|
BINARY_NAME="has"
|
2017-10-14 08:58:23 +02:00
|
|
|
VERSION="v1.1.0"
|
|
|
|
echo "${BINARY_NAME} ${VERSION}"
|
2017-08-29 08:31:11 +02:00
|
|
|
echo "USAGE: ${BINARY_NAME} <command-names>.."
|
|
|
|
echo "EXAMPLE: ${BINARY_NAME} git curl node"
|
2017-08-28 17:41:32 +02:00
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
# for each arg
|
|
|
|
for cmd in "$@"; do
|
|
|
|
__detect $cmd
|
|
|
|
done
|
|
|
|
|
2017-08-29 05:44:08 +02:00
|
|
|
# echo ${OK} / $(($OK+$KO))
|
|
|
|
exit ${KO}
|
2017-08-28 17:41:32 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
|