From cacd45eb62b2c4c68cfe858a6a5e33021efc4ebf Mon Sep 17 00:00:00 2001 From: William Beuil Date: Mon, 8 Mar 2021 11:48:56 +0100 Subject: [PATCH] We can install|use|uninstall version that is prefixed with v --- README.md | 16 ++++++++++++++-- libexec/dctlenv-install | 2 ++ libexec/dctlenv-uninstall | 2 ++ libexec/dctlenv-use | 2 ++ test/dctlenv-install.bats | 21 +++++++++++++++++++++ test/dctlenv-uninstall.bats | 16 ++++++++++++++++ test/dctlenv-use.bats | 16 ++++++++++++++++ 7 files changed, 73 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3846c02..70d1971 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Install a specific version of driftctl. Available options: -- `x.y.z` to install the exact version of driftctl +- `(v)x.y.z` to install the exact version of driftctl - `latest` to install the latest version of driftctl ```console @@ -102,13 +102,17 @@ Switch a version to use. Available options: -- `x.y.z` to install and use the exact version of driftctl +- `(v)x.y.z` to install and use the exact version of driftctl - `latest` to install and use the latest version of driftctl ```console $ dctlenv use 0.2.3 Switching version to v0.2.3 Switching completed + +$ dctlenv use v0.6.0 +Switching version to v0.6.0 +Switching completed ``` ### `dctlenv list` @@ -139,10 +143,18 @@ $ dctlenv list-remote Uninstall a specific version of driftctl. +Available options: + +- `(v)x.y.z` to uninstall the exact version of driftctl + ```console $ dctlenv uninstall 0.2.3 Uninstall driftctl v0.2.3 driftctl v0.2.3 is successfully uninstalled + +$ dctlenv uninstall v0.6.0 +Uninstall driftctl v0.6.0 +driftctl v0.6.0 is successfully uninstalled ``` ### `dctlenv root` diff --git a/libexec/dctlenv-install b/libexec/dctlenv-install index 55ff510..0ef61ac 100755 --- a/libexec/dctlenv-install +++ b/libexec/dctlenv-install @@ -6,6 +6,8 @@ set -uo pipefail version_requested="${1:-""}" [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv install []' +version_requested=$(echo $version_requested | sed 's/^v//') + if [[ $version_requested == "latest" ]]; then version="$(dctlenv-list-remote | tail -n 1)" else diff --git a/libexec/dctlenv-uninstall b/libexec/dctlenv-uninstall index 3e275d4..9afd0c6 100755 --- a/libexec/dctlenv-uninstall +++ b/libexec/dctlenv-uninstall @@ -6,6 +6,8 @@ set -uo pipefail version_requested="${1:-""}" [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv uninstall []' +version_requested=$(echo $version_requested | sed 's/^v//') + version="$(dctlenv-list 2>/dev/null | sed -E 's/^(\*| )? //g; s/ \(set by .+\)$//' | grep -e "$version_requested" | head -n 1)" [ -n "$version" ] || log_error "No versions matching '$version_requested' found in local" diff --git a/libexec/dctlenv-use b/libexec/dctlenv-use index a64dba4..210d414 100755 --- a/libexec/dctlenv-use +++ b/libexec/dctlenv-use @@ -6,6 +6,8 @@ set -uo pipefail version_requested="${1:-""}" [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv use []' +version_requested=$(echo $version_requested | sed 's/^v//') + if [[ $version_requested == "latest" ]]; then version_requested="$(dctlenv-list-remote | tail -n 1)" fi diff --git a/test/dctlenv-install.bats b/test/dctlenv-install.bats index 7ca0c83..69e3285 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 v0.3.1" { + 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 v0.3.1 + + assert_success + assert_output <]: prints a success message if it can install the latest version" { uname() { echo "Linux"; }; export -f uname; curlw() { diff --git a/test/dctlenv-uninstall.bats b/test/dctlenv-uninstall.bats index 9ab8872..b8a6406 100644 --- a/test/dctlenv-uninstall.bats +++ b/test/dctlenv-uninstall.bats @@ -55,6 +55,22 @@ driftctl v0.3.1 is successfully uninstalled OUT } +@test "dctlenv uninstall []: prints a success message if it can uninstall v0.3.1" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.0" + mkdir -p "$DCTLENV_TMPDIR/versions/0.2.3" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + + run dctlenv uninstall v0.3.1 + + assert_success + assert_output <]: prints a success message when switching versions v0.3.1" { + 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 + + driftctl() { exit 0; }; export -f driftctl; + + run dctlenv use v0.3.1 + + assert_success + assert_output <]: prints a success message if we successfuly install and use a non-installed version" { mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1"