-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
get
utility for obtaining binaries for use
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
# Use CLI | ||
# use CLI | ||
|
||
This simple program allows switching between versions of an installed binary. | ||
Assuming you have binaries `kubectl-1.13.4` and `kubectl-1.14.1` installed, | ||
`use` can create a symlink `kubectl` to one of them by invoking it as | ||
|
||
``` | ||
```sh | ||
use kubectl 1.14.1 | ||
``` | ||
|
||
The eligible binaries are taken from and installed to the directory | ||
`$HOME/.local/bin` by default. This behaviour can be adjusted with the | ||
environment variables `USECLI_BIN_DIR` and `USECLI_TARGET_DIR`, respectively. | ||
|
||
# get CLI | ||
|
||
This program invokes registered helpers to obtain a binary suitable for `use`. | ||
|
||
```sh | ||
get kubectl 1.14.1 | ||
``` | ||
|
||
Helpers are binaries in `GETCLI_HELPER_DIR` (default: `$HOME/.local/bin`) that | ||
match the pattern `get-<base name>`. They read the version number from the | ||
first command line argument and write the binary to stdout. Examples can be | ||
found in the [examples](examples) directory. Binaries will be installed to | ||
`USECLI_BIN_DIR`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/sh | ||
|
||
# This helper installs a program that prints its version. | ||
|
||
set -u | ||
|
||
cat <<EOF | ||
#!/bin/sh | ||
echo "$1" | ||
EOF |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
|
||
if [ $# -lt 1 ] | ||
then | ||
echo "Usage: $0 { version }" >&2 | ||
exit 1 | ||
fi | ||
|
||
curl -sSL https://dl.k8s.io/v$1/kubernetes-client-linux-arm64.tar.gz |\ | ||
tar -xz kubernetes/client/bin/kubectl --to-stdout |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
|
||
iam=`basename $0` | ||
|
||
if [ $# -lt 2 ] | ||
then | ||
echo "Usage: $iam {name} {version}" >&2 | ||
exit 1 | ||
fi | ||
|
||
base="$1" | ||
version="$2" | ||
|
||
helperdir="${GETCLI_HELPER_DIR:-$HOME/.local/bin}" | ||
bindirdefault="${USECLI_BIN_DIR:-$HOME/.local/bin}" | ||
bindir="${GETCLI_TARGET_DIR:-$bindirdefault}" | ||
|
||
#TODO: verify that directories exist | ||
|
||
target="$bindir/$base-$version" | ||
if [ -e "$target" ] | ||
then | ||
echo "Target binary '$target' already exists.'" >&2 | ||
exit 2 | ||
fi | ||
|
||
helper="$helperdir/$iam-$base" | ||
if [ ! -e "$helper" ] | ||
then | ||
echo "Helper '$helper' not found.'" >&2 | ||
exit 2 | ||
fi | ||
|
||
"$helper" "$version" >"$target" | ||
chmod +x "$target" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
set -ex | ||
|
||
./test/test-use.sh >/dev/null | ||
./test/test-get.sh >/dev/null |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/sh | ||
|
||
set -u | ||
|
||
export GETCLI_HELPER_DIR=`mktemp -d` | ||
export USECLI_BIN_DIR=`mktemp -d` | ||
export GETCLI_TESTDIR=`mktemp -d` | ||
|
||
cleanup () { | ||
set +u | ||
if [ -n "$GETCLI_DEBUG" ] | ||
then | ||
return 0 | ||
fi | ||
rm -rf "$GETCLI_HELPER_DIR" | ||
rm -rf "$USECLI_BIN_DIR" | ||
rm -rf "$GETCLI_TESTDIR" | ||
} | ||
|
||
fail () { | ||
echo "FAIL:" "$@" >&2 | ||
cleanup | ||
exit 1 | ||
} | ||
|
||
get="./get" | ||
|
||
sh -n "$get" || fail "did not compile" | ||
|
||
"$get" 2>&1 && fail "did not set error code" | ||
|
||
ln -s "`pwd`/examples/helper-dummy.sh" "$GETCLI_HELPER_DIR/get-dummy" | ||
"$get" dummy 1 2>&1 || fail "failed to produce dummy-1" | ||
"$get" dummy 1 2>&1 && fail "tried to overwrite existing dummy-1" | ||
"$get" dummy 2 2>&1 || fail "failed to produce dummy-2" | ||
|
||
for i in 1 2 | ||
do | ||
echo $i >"$GETCLI_TESTDIR/expected-$i" | ||
"$USECLI_BIN_DIR/dummy-$i" >"$GETCLI_TESTDIR/actual-$i" | ||
diff "$GETCLI_TESTDIR/expected-$i" "$GETCLI_TESTDIR/actual-$i" || fail "wrong binary $i" | ||
done | ||
|
||
cleanup |