Created
February 14, 2019 17:08
-
-
Save yogan/6323f125f468f17becb6e76000a8c25d to your computer and use it in GitHub Desktop.
cli-tools-check.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
get_installed_version() { | |
cmd=$1 | |
case $cmd in | |
"fish") | |
fish --version | cut -d" " -f3- | |
;; | |
"lsd") | |
lsd --version | cut -d" " -f2- | |
;; | |
"fd") | |
fd --version | cut -d" " -f2- | |
;; | |
"rg") | |
# no trailing '-' in cut to strip rev from version line: | |
# "ripgrep 0.10.0 (rev 8a7db1a918)" | |
rg --version | head -1 | cut -d" " -f2 | |
;; | |
"bat") | |
bat --version | cut -d" " -f2- | |
;; | |
*) | |
echo "ERROR: given unknown command \"$cmd\"" 1>&2 | |
exit 1 | |
esac | |
} | |
get_latest_version() { | |
cmd=$1 | |
repo=$2 | |
case $cmd in | |
"rg") | |
get_latest_github_release_rg "$repo" | |
;; | |
*) | |
get_latest_github_release "$repo" | |
;; | |
esac | |
} | |
get_latest_github_release() { | |
repo=$1 | |
curl --silent "https://api.github.com/repos/$repo/releases/latest" \ | |
| grep '"tag_name":' \ | |
| sed -E 's/.*"([^"]+)".*/\1/' \ | |
| sed -E 's/^v//' | |
} | |
# rg has releases like: | |
# - "grep-searcher-0.1.2" | |
# - "0.10.0" | |
# - "testrelease" | |
# - "0.9.0" | |
# - "globset-0.4.1" | |
# - "ignore-0.4.3" | |
# - "0.8.1" | |
# We only want the topmost numerical one. | |
get_latest_github_release_rg() { | |
repo=$1 | |
curl --silent "https://api.github.com/repos/$repo/releases" \ | |
| grep '"tag_name":' \ | |
| sed -E 's/.*"([^"]+)".*/\1/' \ | |
| grep '^[0-9]' \ | |
| head -1 | |
} | |
print_release_url() { | |
cmd=$1 | |
repo=$2 | |
echo "[$cmd] https://github.com/$repo/releases" | |
} | |
check_cli_tool() { | |
cmd=$1 | |
repo=$2 | |
if [ -x "`which $cmd`" ] ; then | |
installed=`get_installed_version $cmd` | |
latest=`get_latest_version $cmd $repo` | |
if [ "$installed" = "$latest" ] ; then | |
echo "[$cmd] Up-to-date! ($installed)" | |
else | |
echo "[$cmd] Installed: $installed" | |
echo "[$cmd] Latest: $latest" | |
print_release_url $cmd $repo | |
fi | |
else | |
echo "[$cmd] NOT INSTALLED" | |
print_release_url $cmd $repo | |
fi | |
echo | |
} | |
check_cli_tool "fish" "fish-shell/fish-shell" | |
check_cli_tool "lsd" "Peltoche/lsd" | |
check_cli_tool "fd" "sharkdp/fd" | |
check_cli_tool "rg" "BurntSushi/ripgrep" | |
check_cli_tool "bat" "sharkdp/bat" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment