-
-
Save peterwwillis/755002d6d3849af5bbc6cb8ce8bbfd5f to your computer and use it in GitHub Desktop.
cliv: Command-LIne wrapper to execute different Versions of binaries in different directories using different environment variables
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
#!/usr/bin/env sh | |
# cliv - Execute commands using a specific .env and directory | |
set -eu | |
_err () { printf "%s\n" "$0: Error: $*" ; exit 1 ; } | |
HOME="${HOME:-$(getent passwd $(id -u) | cut -d : -f 6)}" | |
[ -d "$HOME/.cliv" ] || mkdir -p "$HOME/.cliv" | |
if [ $# -lt 1 ] || [ "$1" = "-h" ] ; then | |
printf "%s\n" " | |
Usage: $0 [OPTS] | |
$0 [-i] VERSION [CMD [ARGS ..]]\n | |
Opts: | |
-l List versions | |
-h This screen | |
-n Create a new $HOME/.cliv/VERSION | |
-i Clear current environment | |
" | |
exit 1 | |
fi | |
if [ "$1" = "-i" ] ; then | |
shift | |
exec env -i "$0" "$@" | |
elif [ "$1" = "-l" ] ; then | |
for d in "$HOME/.cliv/"* ; do [ -d "$d" ] && basename "$d" ; done | |
elif [ "$1" = "-n" ] ; then | |
vers="$2"; shift 2 | |
[ -d "$HOME/.cliv/$vers" ] && _err "Version '$HOME/.cliv/$vers' already exists" | |
mkdir -p "$HOME/.cliv/$vers/bin" | |
echo "PATH=$HOME/.cliv/$vers/bin:$PATH" > "$HOME/.cliv/$vers/.env" | |
elif [ $# -ge 1 ] ; then | |
vers="$1" ; shift | |
[ -d "$HOME/.cliv/$vers" ] || _err "No version '$HOME/.cliv/$vers' found" | |
set -a ; . "$HOME/.cliv/$vers/.env" | |
exec env "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment