mirror of https://github.com/kdabir/has.git
Compare commits
7 Commits
10fe7b03fb
...
227c909067
Author | SHA1 | Date |
---|---|---|
Christian Clauss | 227c909067 | |
Kunal Dabir | a2d7c083c7 | |
Kunal Dabir | abb3decba7 | |
Maksym Bilyk | 54c334962c | |
wzy | 8b43040ce2 | |
Alex Meyer | c12a38bbdd | |
Kunal Dabir | 49444b3179 |
|
@ -17,6 +17,7 @@ jobs:
|
|||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
container:
|
||||
- debian # uses debian:buster-20200327-slim which is debian 10.3
|
||||
|
@ -36,6 +37,7 @@ jobs:
|
|||
name: test_all
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
container:
|
||||
- ubuntu # uses ubuntu:bionic-20200311 which is ubuntu 18.04
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
`has` checks presence of various command line tools on the PATH and reports their installed version.
|
||||
|
||||
[![Build Status](https://travis-ci.org/kdabir/has.svg?branch=master)](https://travis-ci.org/kdabir/has)
|
||||
[![Build Status](https://github.com/kdabir/has/actions/workflows/main.yml/badge.svg)](https://github.com/kdabir/has/actions/workflows/main.yml)
|
||||
[![Open Source Helpers](https://www.codetriage.com/kdabir/has/badges/users.svg)](https://www.codetriage.com/kdabir/has)
|
||||
|
||||
[![demo](demo.svg)](demo.svg)
|
||||
|
|
108
has
108
has
|
@ -7,10 +7,12 @@
|
|||
set -o pipefail
|
||||
|
||||
readonly BINARY_NAME="has"
|
||||
readonly VERSION="v1.6.0"
|
||||
readonly VERSION="v1.5.0"
|
||||
|
||||
## constants - symbols for success failure
|
||||
if [[ -z $TERM ]]; then
|
||||
if [[ ! -t 1 ]]; then
|
||||
TERM="dumb"
|
||||
elif [[ -z $TERM ]]; then
|
||||
TERM="xterm"
|
||||
fi
|
||||
readonly txtreset="$(tput -T $TERM sgr0)"
|
||||
|
@ -32,6 +34,14 @@ readonly checkmark='\342\234\223'
|
|||
readonly PASS="${txtbold}${txtgreen}${checkmark}${txtreset}"
|
||||
readonly FAIL="${txtbold}${txtred}${fancyx}${txtreset}"
|
||||
|
||||
## These variables are used to control decoration of output
|
||||
COLOR_AUTO="auto"
|
||||
COLOR_NEVER="never"
|
||||
COLOR_ALWAYS="always"
|
||||
COLOR_OPTS=(${COLOR_AUTO} ${COLOR_NEVER} ${COLOR_ALWAYS})
|
||||
COLOR="${COLOR_AUTO}"
|
||||
COLOR_PREFIX="--color"
|
||||
|
||||
## These variables are used to keep track of passed and failed commands
|
||||
OK=0
|
||||
KO=0
|
||||
|
@ -60,6 +70,15 @@ _version() {
|
|||
printf '%s\n' "${VERSION}"
|
||||
}
|
||||
|
||||
# function to parse color option
|
||||
_set_color() {
|
||||
local found=0;
|
||||
for opt in "${COLOR_OPTS[@]}"; do
|
||||
[ "${1}" == "${COLOR_PREFIX}-${opt}" ] && COLOR="${opt}" && found=1 && break
|
||||
done
|
||||
[ ${found} -eq 1 ] || >&2 echo "Error: wrong flag ${1}"
|
||||
}
|
||||
|
||||
# try to extract version by executing "${1}" with "${2}" arg
|
||||
# command name to be executed is dynamically passed to this function as ${1}
|
||||
# arg to ${1} is passed in ${2}
|
||||
|
@ -99,10 +118,17 @@ __dynamic_detect-arg_version(){
|
|||
__detect(){
|
||||
name="${1}"
|
||||
|
||||
# default values (for case when decoration applied)
|
||||
fail=${FAIL}
|
||||
pass=${PASS}
|
||||
wrapper_beg="${txtbold}${txtyellow}"
|
||||
wrapper_end="${txtreset}"
|
||||
|
||||
# setup aliases - maps commonly used name to exact command name
|
||||
case ${name} in
|
||||
rust ) command="rustc" ;;
|
||||
ssl ) command="openssl" ;;
|
||||
openssh ) command="ssh" ;;
|
||||
golang ) command="go" ;;
|
||||
jre ) command="java" ;;
|
||||
jdk ) command="javac" ;;
|
||||
|
@ -139,11 +165,9 @@ __detect(){
|
|||
ag|ack|rg) __dynamic_detect--version "${command}" ;;
|
||||
tree|autojump) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
## Package managers
|
||||
## OS Package managers
|
||||
apt|apt-get|aptitude) __dynamic_detect--version "${command}" ;;
|
||||
brew) __dynamic_detect--version "${command}" ;;
|
||||
composer) __dynamic_detect-V "${command}" ;;
|
||||
pip|pip3|conda) __dynamic_detect-V "${command}" ;;
|
||||
|
||||
## System tools
|
||||
sed|awk|grep|file|sudo) __dynamic_detect--version "${command}" ;;
|
||||
|
@ -152,7 +176,7 @@ __detect(){
|
|||
gunzip) __dynamic_detect--version "${command}" ;;
|
||||
tee) __dynamic_detect--version "${command}" ;;
|
||||
screen) __dynamic_detect-v "${command}" ;;
|
||||
|
||||
|
||||
# Container runtimes
|
||||
docker|podman) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
|
@ -160,23 +184,32 @@ __detect(){
|
|||
psql) __dynamic_detect--version "${command}" ;;
|
||||
sqlite3) __dynamic_detect-version "${command}" ;;
|
||||
|
||||
|
||||
########### Programming languages Build tools & Package managers ###########
|
||||
|
||||
|
||||
## Build and Compile
|
||||
gcc|make|cmake|bats) __dynamic_detect--version "${command}" ;;
|
||||
ninja) __dynamic_detect--version "${command}" ;;
|
||||
composer) __dynamic_detect-V "${command}" ;;
|
||||
pip|pip3|conda) __dynamic_detect-V "${command}" ;;
|
||||
lein|gradle|mvn) __dynamic_detect--version "${command}" ;;
|
||||
grunt|brunch) __dynamic_detect--version "${command}" ;;
|
||||
gem|rake|bundle) __dynamic_detect--version "${command}" ;;
|
||||
npm|yarn|pnpm) __dynamic_detect--version "${command}" ;;
|
||||
act) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
## Scripting Language / runtime
|
||||
ruby|R|python|python3) __dynamic_detect--version "${command}" ;;
|
||||
perl|perl6|php|php5) __dynamic_detect--version "${command}" ;;
|
||||
groovy|node) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
## Compile
|
||||
gcc|make|bats) __dynamic_detect--version "${command}" ;;
|
||||
ninja) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
## Build tools
|
||||
lein|gradle|mvn) __dynamic_detect--version "${command}" ;;
|
||||
grunt|brunch) __dynamic_detect--version "${command}" ;;
|
||||
gem|rake|bundle) __dynamic_detect--version "${command}" ;;
|
||||
npm|yarn) __dynamic_detect--version "${command}" ;;
|
||||
# JVM tools that need -version flag
|
||||
ant) __dynamic_detect-version "${command}" ;;
|
||||
java|javac|scala|kotlin) __dynamic_detect-version "${command}" ;;
|
||||
|
||||
## Rust
|
||||
rustc|cargo) __dynamic_detect--version "${command}" ;;
|
||||
rustc|cargo) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
## Cloud Tools
|
||||
aws|eb|sls|gcloud) __dynamic_detect--version "${command}" ;;
|
||||
|
@ -185,7 +218,8 @@ __detect(){
|
|||
netlifyctl) __dynamic_detect-arg_version "${command}" ;;
|
||||
|
||||
## GPG Tools
|
||||
gpg|gpgconf|gpg-agent|gpg-connect-agent) __dynamic_detect--version "${command}" ;;
|
||||
gpg|gpgconf|gpg-agent) __dynamic_detect--version "${command}" ;;
|
||||
gpg-connect-agent) __dynamic_detect--version "${command}" ;;
|
||||
gpgsm) __dynamic_detect--version "${command}" ;;
|
||||
|
||||
## Hashicorp Tools
|
||||
|
@ -202,13 +236,9 @@ __detect(){
|
|||
# commands that need -V flag
|
||||
ab) __dynamic_detect-V "${command}" ;;
|
||||
|
||||
# commands that need -version flag
|
||||
ant) __dynamic_detect-version "${command}" ;;
|
||||
java|javac|scala|kotlin) __dynamic_detect-version "${command}" ;;
|
||||
|
||||
# commands that need version arg
|
||||
go|hugo) __dynamic_detect-arg_version "${command}" ;;
|
||||
openssl) __dynamic_detect-arg_version "${command}" ;;
|
||||
|
||||
## Example of commands that need custom processing
|
||||
|
||||
|
@ -228,6 +258,18 @@ __detect(){
|
|||
status=$?
|
||||
;;
|
||||
|
||||
# openssl can have a letter at the end of the version number (eg. "1.1.1j")
|
||||
openssl)
|
||||
version=$( openssl version 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}[[:alnum:]]*" |head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
# openssh version output has a lot going on
|
||||
ssh)
|
||||
version=$(ssh -V 2>&1 |grep -Eo "OpenSSH_${REGEX_SIMPLE_VERSION}[[:alnum:]]*" |cut -d'_' -f2)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
## use 'readlink' to test for GNU coreutils
|
||||
# readlink (GNU coreutils) 8.28
|
||||
gnu_coreutils) __dynamic_detect--version readlink ;;
|
||||
|
@ -245,7 +287,7 @@ __detect(){
|
|||
;;
|
||||
|
||||
has)
|
||||
version=$( has 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
version=$( has -v 2>&1 | grep -Eo "${REGEX_SIMPLE_VERSION}" | head -1)
|
||||
status=$?
|
||||
;;
|
||||
|
||||
|
@ -262,20 +304,27 @@ __detect(){
|
|||
;;
|
||||
esac
|
||||
|
||||
if [ "${COLOR}" == "${COLOR_NEVER}" ] || [[ ! -t 1 && "${COLOR}" == "${COLOR_AUTO}" ]]; then
|
||||
pass=${checkmark}
|
||||
fail=${fancyx}
|
||||
wrapper_beg=""
|
||||
wrapper_end=""
|
||||
fi
|
||||
|
||||
if [ "$status" -eq "-1" ]; then ## When unsafe processing is not allowed, the -1 signifies
|
||||
printf '%b %s not understood\n' "${FAIL}" "${command}" > "$OUTPUT"
|
||||
printf '%b %s not understood\n' "${fail}" "${command}" > "$OUTPUT"
|
||||
KO=$(( KO+1 ))
|
||||
|
||||
elif [ ${status} -eq 127 ]; then ## command not installed
|
||||
printf '%b %s\n' "${FAIL}" "${command}" > "$OUTPUT"
|
||||
printf '%b %s\n' "${fail}" "${command}" > "$OUTPUT"
|
||||
KO=$(( KO+1 ))
|
||||
|
||||
elif [ ${status} -eq 0 ] || [ ${status} -eq 141 ]; then ## successfully executed
|
||||
printf "%b %s %b\n" "${PASS}" "${command}" "${txtbold}${txtyellow}${version}${txtreset}" > "$OUTPUT"
|
||||
printf "%b %s %b\n" "${pass}" "${command}" "${wrapper_beg}${version}${wrapper_end}" > "$OUTPUT"
|
||||
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}" > "$OUTPUT"
|
||||
printf '%b %s\n' "${pass}" "${command}" > "$OUTPUT"
|
||||
OK=$(( OK+1 ))
|
||||
fi
|
||||
} #end __detect
|
||||
|
@ -283,7 +332,7 @@ __detect(){
|
|||
OPTIND=1
|
||||
OUTPUT=/dev/stdout
|
||||
|
||||
while getopts ":qhV-" OPTION; do
|
||||
while getopts ":qhv-" OPTION; do
|
||||
case "$OPTION" in
|
||||
q)
|
||||
QUIET="true"
|
||||
|
@ -293,7 +342,7 @@ while getopts ":qhV-" OPTION; do
|
|||
_usage
|
||||
exit 0
|
||||
;;
|
||||
V)
|
||||
v)
|
||||
_version
|
||||
exit 0
|
||||
;;
|
||||
|
@ -311,6 +360,9 @@ while getopts ":qhV-" OPTION; do
|
|||
_usage
|
||||
exit 0
|
||||
;;
|
||||
${COLOR_PREFIX}*)
|
||||
_set_color "${OPTION}"
|
||||
;;
|
||||
*)
|
||||
printf '%s: unrecognized option '%s'\n' "${BINARY_NAME}" "${OPTARG}"
|
||||
_usage
|
||||
|
|
Loading…
Reference in New Issue