From 259a23fe9f1893b68933c6603213940d9fc932b2 Mon Sep 17 00:00:00 2001 From: William Beuil Date: Fri, 5 Mar 2021 18:27:43 +0100 Subject: [PATCH] Add install|use of latest version --- libexec/dctlenv-install | 7 ++++++- libexec/dctlenv-use | 10 +++++++--- test/dctlenv-install.bats | 21 +++++++++++++++++++++ test/dctlenv-use.bats | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/libexec/dctlenv-install b/libexec/dctlenv-install index 9568867..55ff510 100755 --- a/libexec/dctlenv-install +++ b/libexec/dctlenv-install @@ -6,7 +6,12 @@ set -uo pipefail version_requested="${1:-""}" [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv install []' -version="$(dctlenv-list-remote | grep "$version_requested" | head -n 1)" +if [[ $version_requested == "latest" ]]; then + version="$(dctlenv-list-remote | tail -n 1)" +else + version="$(dctlenv-list-remote | grep "$version_requested" | head -n 1)" +fi + [ -n "$version" ] || log_error "No version $version_requested found in remote" dst_path="$DCTLENV_ROOT/versions/$version" diff --git a/libexec/dctlenv-use b/libexec/dctlenv-use index ae150cf..475d2cc 100755 --- a/libexec/dctlenv-use +++ b/libexec/dctlenv-use @@ -8,6 +8,10 @@ set -uo pipefail version_requested="${1:-""}" [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv use []' +if [[ $version_requested == "latest" ]]; then + version_requested="$(dctlenv-list-remote | tail -n 1)" +fi + log_debug "Searching $DCTLENV_ROOT/versions for version matching $version_requested" version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \ | tail -n +2 \ @@ -19,9 +23,9 @@ version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \ if [ -n "$version" ]; then log_debug "Found version: $version" else - echo "No installed versions of driftctl matched '$1', let's install it" - $(dctlenv-install $1 1>/dev/null) || log_error "Installation of version $1 failed" - version=$1 + echo "No installed versions of driftctl matched '$version_requested', let's install it" + $(dctlenv-install $version_requested 1>/dev/null) || log_error "Installation of version $version_requested failed" + version=$version_requested fi target_path="$DCTLENV_ROOT/versions/$version" diff --git a/test/dctlenv-install.bats b/test/dctlenv-install.bats index b0b7432..7ca0c83 100644 --- a/test/dctlenv-install.bats +++ b/test/dctlenv-install.bats @@ -148,6 +148,27 @@ Installation of driftctl v0.3.1 successful. To make this your default version, r OUT } +@test "dctlenv install []: prints a success message if it can install the latest version" { + uname() { echo "Linux"; }; export -f uname; + curlw() { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl_linux_amd64" + (cd "$DCTLENV_TMPDIR/versions/0.3.1"; sha256sum * > "$DCTLENV_TMPDIR/versions/0.3.1/driftctl_SHA256SUMS") + exit 0 + }; export -f curlw; + + run dctlenv install latest + + assert_success + assert_output </dev/null || echo "$DCTLENV_TMPDIR")" export DCTLENV_ROOT="$DCTLENV_TMPDIR" + + dctlenv-list-remote() { + echo "0.1.0 +0.1.1 +0.2.0 +0.2.1 +0.2.2 +0.2.3 +0.3.0 +0.3.1" + } + export -f dctlenv-list-remote; } @test "dctlenv use []: prints an error message if we try to use more than one version" { @@ -101,6 +113,27 @@ Switching completed OUT } +@test "dctlenv use []: prints a success message when using the latest version" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.0" + + dctlenv-install() { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + chmod +x $DCTLENV_TMPDIR/versions/0.3.1/driftctl + exit 0 + }; export -f dctlenv-install; + driftctl() { exit 0; }; export -f driftctl; + + run dctlenv use latest + + assert_success + assert_output <