Skip to content

Commit

Permalink
add get utility for obtaining binaries for use
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Sep 20, 2019
1 parent f349a54 commit 83f17cc
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
18 changes: 16 additions & 2 deletions README.md
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`.
10 changes: 10 additions & 0 deletions examples/helper-dummy.sh
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
12 changes: 12 additions & 0 deletions examples/helper-kubectl.sh
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
37 changes: 37 additions & 0 deletions get
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"
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
set -ex

./test/test-use.sh >/dev/null
./test/test-get.sh >/dev/null
44 changes: 44 additions & 0 deletions test/test-get.sh
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

0 comments on commit 83f17cc

Please sign in to comment.