mirror of https://github.com/kdabir/has.git
✨ add dynamic `-V` check
- [x] 👌 update 'ab' to use dynamic `-V` - [x] 👌 update 'go' to use dynamic `arg version` - [x] 🚨 general whitespace and whitespace in command subsitution - [x] [SC2004] 🚨 `$`/`${}` is unnecessary on arithmetic variables. - [x] ✅ make `hub` test conditional on command found
This commit is contained in:
parent
feb1413e6f
commit
5499d376fa
|
@ -58,7 +58,7 @@ teardown() {
|
|||
|
||||
@test "make update runs git fetch" {
|
||||
cd "${BATS_TEST_DIRNAME}"
|
||||
skip "make update overwrites my changes"
|
||||
skip "make update overwrites my git working tree"
|
||||
run make update
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
@ -157,7 +157,7 @@ teardown() {
|
|||
}
|
||||
|
||||
@test "testing coreutils commands" {
|
||||
run $has coreutils sed awk grep sudo file linux-utils
|
||||
run $has coreutils sed awk grep sudo file linuxutils
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$(echo "${lines[0]}" | grep "gnu_coreutils")" ]
|
||||
|
@ -166,6 +166,9 @@ teardown() {
|
|||
}
|
||||
|
||||
@test "testing hub version is different to git version" {
|
||||
if ! command -v hub; then
|
||||
skip "'hub' command not found. This passes for @virgilwashere locally."
|
||||
fi
|
||||
run $has hub git
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
|
96
has
96
has
|
@ -8,20 +8,20 @@ readonly BINARY_NAME="has"
|
|||
readonly VERSION="v1.4.0"
|
||||
|
||||
## constant - symbols for success failure
|
||||
readonly txtreset="$(tput sgr0)"
|
||||
readonly txtbold="$(tput bold)"
|
||||
readonly txtblack="$(tput setaf 0)"
|
||||
readonly txtred="$(tput setaf 1)"
|
||||
readonly txtgreen="$(tput setaf 2)"
|
||||
readonly txtyellow="$(tput setaf 3)"
|
||||
readonly txtblue="$(tput setaf 4)"
|
||||
readonly txtpurple="$(tput setaf 5)"
|
||||
readonly txtcyan="$(tput setaf 6)"
|
||||
readonly txtwhite="$(tput setaf 7)"
|
||||
readonly txtreset="$(tput sgr0)"
|
||||
readonly txtbold="$(tput bold)"
|
||||
readonly txtblack="$(tput setaf 0)"
|
||||
readonly txtred="$(tput setaf 1)"
|
||||
readonly txtgreen="$(tput setaf 2)"
|
||||
readonly txtyellow="$(tput setaf 3)"
|
||||
readonly txtblue="$(tput setaf 4)"
|
||||
readonly txtpurple="$(tput setaf 5)"
|
||||
readonly txtcyan="$(tput setaf 6)"
|
||||
readonly txtwhite="$(tput setaf 7)"
|
||||
# unicode "✗"
|
||||
readonly fancyx='\342\234\227'
|
||||
readonly fancyx='\342\234\227'
|
||||
# unicode "✓"
|
||||
readonly checkmark='\342\234\223'
|
||||
readonly checkmark='\342\234\223'
|
||||
# PASS="\e[1m\e[38;5;2m✔\e[m"
|
||||
# FAIL="\e[1m\e[38;5;1m✘\e[m"
|
||||
readonly PASS="${txtbold}${txtgreen}${checkmark}${txtreset}"
|
||||
|
@ -41,7 +41,7 @@ RC_FILE=".hasrc"
|
|||
__dynamic_detect(){
|
||||
cmd="${1}"
|
||||
params="${2}"
|
||||
version=$(eval "${cmd}" "${params}" "2>&1" | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( eval "${cmd}" "${params}" "2>&1" | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
}
|
||||
|
||||
|
@ -60,29 +60,33 @@ __dynamic_detect-v(){
|
|||
__dynamic_detect "${1}" "-v"
|
||||
}
|
||||
|
||||
# commands that use `-V` flag
|
||||
__dynamic_detect-V(){
|
||||
__dynamic_detect "${1}" "-V"
|
||||
}
|
||||
|
||||
# commands that use `version` argument
|
||||
__dynamic_detect-arg_version(){
|
||||
__dynamic_detect "${1}" "version"
|
||||
}
|
||||
|
||||
|
||||
## the main function
|
||||
__detect(){
|
||||
name="${1}"
|
||||
|
||||
# setup aliases maps commonly used name to exact command name
|
||||
# setup aliases - maps commonly used name to exact command name
|
||||
case ${name} in
|
||||
golang) command="go" ;;
|
||||
jre) command="java" ;;
|
||||
jdk) command="javac" ;;
|
||||
nodejs) command="node" ;;
|
||||
goreplay) command="gor";;
|
||||
httpie) command="http";;
|
||||
homebrew) command="brew";;
|
||||
awsebcli) command="eb";;
|
||||
awscli) command="aws";;
|
||||
*coreutils|linux-utils) command="gnu_coreutils" ;;
|
||||
*) command=${name} ;;
|
||||
golang ) command="go" ;;
|
||||
jre ) command="java" ;;
|
||||
jdk ) command="javac" ;;
|
||||
nodejs ) command="node" ;;
|
||||
goreplay ) command="gor" ;;
|
||||
httpie ) command="http" ;;
|
||||
homebrew ) command="brew" ;;
|
||||
awsebcli ) command="eb" ;;
|
||||
awscli ) command="aws" ;;
|
||||
*coreutils|linux*utils) command="gnu_coreutils" ;;
|
||||
* ) command=${name} ;;
|
||||
esac
|
||||
|
||||
case "${command}" in
|
||||
|
@ -113,40 +117,32 @@ __detect(){
|
|||
# commands that need -v flag
|
||||
unzip) __dynamic_detect-v "${command}" ;;
|
||||
|
||||
# commands that need -V flag
|
||||
ab) __dynamic_detect-V "${command}" ;;
|
||||
|
||||
# commands that need -version flag
|
||||
ant|java|javac) __dynamic_detect-version "${command}" ;;
|
||||
scala|kotlin) __dynamic_detect-version "${command}" ;;
|
||||
|
||||
# commands that need version arg
|
||||
hugo) __dynamic_detect-arg_version "${command}" ;;
|
||||
go|hugo) __dynamic_detect-arg_version "${command}" ;;
|
||||
|
||||
## Example of commands that need custom processing
|
||||
## go needs version arg
|
||||
go)
|
||||
version=$(go version 2>&1| grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
## TODO cleanup, currently need to add extra space in regex, otherwise the time gets selected
|
||||
gulp)
|
||||
version=$(gulp --version 2>&1| grep -Eo " ${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
## ab uses -V flag
|
||||
ab)
|
||||
version=$(ab -V 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( gulp --version 2>&1| grep -Eo " ${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
## gor returns version but does not return normal status code, hence needs custom processing
|
||||
gor)
|
||||
version=$(gor version 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( gor version 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
if [ $? -eq 1 ]; then status=0; else status=1; fi
|
||||
;;
|
||||
|
||||
sbt)
|
||||
version=$(sbt about 2>&1 | grep -Eo "([[:digit:]]{1,4}\.){2}[[:digit:]]{1,4}" | head -1)
|
||||
version=$( sbt about 2>&1 | grep -Eo "([[:digit:]]{1,4}\.){2}[[:digit:]]{1,4}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
|
@ -156,18 +152,18 @@ __detect(){
|
|||
|
||||
## hub uses --version but version string is on second line, or third if HUB_VERBOSE set
|
||||
hub)
|
||||
version=$(HUB_VERBOSE='' hub --version 2>&1 | sed -n 2p | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( HUB_VERBOSE='' hub --version 2>&1 | sed -n 2p | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
## zip uses -v but version string is on second line
|
||||
zip)
|
||||
version=$(zip -v 2>&1 | sed -n 2p | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( zip -v 2>&1 | sed -n 2p | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
has)
|
||||
version=$(has 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( has 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
|
@ -186,19 +182,19 @@ __detect(){
|
|||
|
||||
if [ "$status" -eq "-1" ]; then ## When unsafe processing is not allowed, the -1 signifies
|
||||
printf '%b %s not understood\n' "${FAIL}" "${command}"
|
||||
KO=$(($KO+1))
|
||||
KO=$(( KO+1 ))
|
||||
|
||||
elif [ ${status} -eq 127 ]; then ## command not installed
|
||||
printf '%b %s\n' "${FAIL}" "${command}"
|
||||
KO=$(($KO+1))
|
||||
KO=$(( KO+1 ))
|
||||
|
||||
elif [ ${status} -eq 0 ] || [ ${status} -eq 141 ]; then ## successfully executed
|
||||
printf "%b %s %b\n" "${PASS}" "${command}" "${txtbold}${txtyellow}${version}${txtreset}"
|
||||
OK=$(($OK+1))
|
||||
OK=$(( OK+1 ))
|
||||
|
||||
else ## as long as its not 127, command is there, but we might not have been able to extract version
|
||||
printf '%b %s\n' "${PASS}" "${command}"
|
||||
OK=$(($OK+1))
|
||||
OK=$(( OK+1 ))
|
||||
fi
|
||||
} #end __detect
|
||||
|
||||
|
@ -227,13 +223,13 @@ else
|
|||
## for all
|
||||
while read -r line; do
|
||||
__detect "${line}"
|
||||
done <<<"$(grep -Ev "^\s*(#|$)" "${RC_FILE}" )"
|
||||
done <<<"$( grep -Ev "^\s*(#|$)" "${RC_FILE}" )"
|
||||
fi
|
||||
|
||||
## max status code that can be returned
|
||||
MAX_STATUS_CODE=126
|
||||
|
||||
if [[ "$KO" -gt "${MAX_STATUS_CODE}" ]]; then
|
||||
if [[ "${KO}" -gt "${MAX_STATUS_CODE}" ]]; then
|
||||
exit "${MAX_STATUS_CODE}"
|
||||
else
|
||||
exit "${KO}"
|
||||
|
|
Loading…
Reference in New Issue