mirror of https://github.com/kdabir/has.git
Compare commits
10 Commits
7446f451dd
...
a2d7c083c7
Author | SHA1 | Date |
---|---|---|
Kunal Dabir | a2d7c083c7 | |
Kunal Dabir | abb3decba7 | |
Maksym Bilyk | 54c334962c | |
wzy | 8b43040ce2 | |
Alex Meyer | c12a38bbdd | |
Kunal Dabir | 49444b3179 | |
Christian Clauss | 81a613a80c | |
Sylvain METAYER | 0f965b7713 | |
Christian Clauss | b6869f3f8d | |
Christian Clauss | fc3505077a |
|
@ -8,10 +8,10 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Clone Repo
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Run Shellcheck
|
||||
uses: ludeeus/action-shellcheck@0.1.0
|
||||
uses: ludeeus/action-shellcheck@1.1.0
|
||||
|
||||
test:
|
||||
name: test
|
||||
|
@ -26,7 +26,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Clone Repo
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: test
|
||||
run: make test
|
||||
|
@ -45,7 +45,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Clone Repo
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: test_all
|
||||
run: bats -t ./tests/test_all_packages.bats
|
||||
|
|
28
.travis.yml
28
.travis.yml
|
@ -1,28 +0,0 @@
|
|||
language: bash
|
||||
dist: xenial
|
||||
addons:
|
||||
apt:
|
||||
update: true
|
||||
packages:
|
||||
- bc
|
||||
- pv
|
||||
- xz-utils
|
||||
- unar
|
||||
|
||||
env:
|
||||
global:
|
||||
- export PATH="/usr/local/bin:$PATH"
|
||||
|
||||
before_install:
|
||||
- |
|
||||
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
|
||||
batstmp="$( mktemp -d -p /tmp bats-core.XXXXX)"
|
||||
pushd "${batstmp}"
|
||||
curl -sSLO https://github.com/bats-core/bats-core/archive/master.zip
|
||||
unzip -qo master.zip
|
||||
sudo bash "${batstmp}"/bats-core-master/install.sh /usr/local
|
||||
popd
|
||||
fi
|
||||
|
||||
script:
|
||||
- make test
|
13
README.md
13
README.md
|
@ -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)
|
||||
|
@ -91,6 +91,13 @@ curl -sL https://git.io/_has | sudo tee /usr/local/bin/has >/dev/null
|
|||
|
||||
These commands are safe to be called multiple times as well (to update `has`)
|
||||
|
||||
### asdf users
|
||||
|
||||
```
|
||||
asdf plugin add has https://github.com/sylvainmetayer/asdf-has
|
||||
asdf install has 1.4.0
|
||||
```
|
||||
|
||||
### Running directly off the Internet
|
||||
|
||||
If you are lazy, you can run `has` directly off the Internet as well:
|
||||
|
@ -121,7 +128,7 @@ has is aliased to `curl -sL https://git.io/_has | bash -s'
|
|||
|
||||
Let's say `$ has foobar` returns `foobar not understood`, because `has` may not have whitelisted `foobar`.
|
||||
|
||||
In such cases, pass `HAS_ALLOW_UNSAFE=y has foobar`. This should still check for existance of `foobar` and tries to detect version as well.
|
||||
In such cases, pass `HAS_ALLOW_UNSAFE=y has foobar`. This should still check for existence of `foobar` and tries to detect version as well.
|
||||
|
||||
> the value must exactly be `y` for it to work.
|
||||
|
||||
|
@ -198,6 +205,6 @@ to guide developers about what needs to be done to add more tools.
|
|||
If you are contributing a feature, please ensure to check current tests. Add test cases for your feature. Tests are
|
||||
executed using the excellent [bats](https://github.com/bats-core/bats-core) testing framework. Add tests and run `make test`
|
||||
|
||||
Raise the PR and **make sure the tests pass** on [Travis-CI](https://travis-ci.org/kdabir/has).
|
||||
Raise the PR and **make sure the tests pass** on [GitHub Actions](https://github.com/kdabir/has/actions).
|
||||
|
||||
### ♥
|
||||
|
|
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
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Not pre-installed to container, but repository cloned during CI
|
||||
has
|
||||
|
||||
# Tests are run as admin in CI. Code requires an extra paramater to get version as admin.
|
||||
# Tests are run as admin in CI. Code requires an extra parameter to get version as admin.
|
||||
# todo: add --user-data-dir=/tmp for code when has is run as admin
|
||||
code
|
||||
|
||||
|
|
Loading…
Reference in New Issue