Add install|use of latest version

main
William Beuil 2021-03-05 18:27:43 +01:00
parent 08d03508d1
commit 259a23fe9f
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
4 changed files with 67 additions and 4 deletions

View File

@ -6,7 +6,12 @@ set -uo pipefail
version_requested="${1:-""}" version_requested="${1:-""}"
[ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv install [<version>]' [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv install [<version>]'
if [[ $version_requested == "latest" ]]; then
version="$(dctlenv-list-remote | tail -n 1)"
else
version="$(dctlenv-list-remote | grep "$version_requested" | head -n 1)" version="$(dctlenv-list-remote | grep "$version_requested" | head -n 1)"
fi
[ -n "$version" ] || log_error "No version $version_requested found in remote" [ -n "$version" ] || log_error "No version $version_requested found in remote"
dst_path="$DCTLENV_ROOT/versions/$version" dst_path="$DCTLENV_ROOT/versions/$version"

View File

@ -8,6 +8,10 @@ set -uo pipefail
version_requested="${1:-""}" version_requested="${1:-""}"
[ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv use [<version>]' [ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv use [<version>]'
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" log_debug "Searching $DCTLENV_ROOT/versions for version matching $version_requested"
version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \ version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \
| tail -n +2 \ | tail -n +2 \
@ -19,9 +23,9 @@ version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \
if [ -n "$version" ]; then if [ -n "$version" ]; then
log_debug "Found version: $version" log_debug "Found version: $version"
else else
echo "No installed versions of driftctl matched '$1', let's install it" echo "No installed versions of driftctl matched '$version_requested', let's install it"
$(dctlenv-install $1 1>/dev/null) || log_error "Installation of version $1 failed" $(dctlenv-install $version_requested 1>/dev/null) || log_error "Installation of version $version_requested failed"
version=$1 version=$version_requested
fi fi
target_path="$DCTLENV_ROOT/versions/$version" target_path="$DCTLENV_ROOT/versions/$version"

View File

@ -148,6 +148,27 @@ Installation of driftctl v0.3.1 successful. To make this your default version, r
OUT OUT
} }
@test "dctlenv install [<version>]: 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 <<OUT
Installing driftctl v0.3.1
Downloading release tarball from https://github.com/cloudskiff/driftctl/releases/download/v0.3.1/driftctl_linux_amd64
Downloading SHA256 hashes file from https://github.com/cloudskiff/driftctl/releases/download/v0.3.1/driftctl_SHA256SUMS
SHA256 hash matched!
Installation of driftctl v0.3.1 successful. To make this your default version, run 'dctlenv use 0.3.1'
OUT
}
teardown() { teardown() {
rm -rf "$DCTLENV_TMPDIR" rm -rf "$DCTLENV_TMPDIR"
} }

View File

@ -6,6 +6,18 @@ setup() {
export DCTLENV_TMPDIR="$BATS_TMPDIR/dctlenv" export DCTLENV_TMPDIR="$BATS_TMPDIR/dctlenv"
export DCTLENV_TMPDIR="$(mktemp -d "$DCTLENV_TMPDIR.XXX" 2>/dev/null || echo "$DCTLENV_TMPDIR")" export DCTLENV_TMPDIR="$(mktemp -d "$DCTLENV_TMPDIR.XXX" 2>/dev/null || echo "$DCTLENV_TMPDIR")"
export DCTLENV_ROOT="$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 [<version>]: prints an error message if we try to use more than one version" { @test "dctlenv use [<version>]: prints an error message if we try to use more than one version" {
@ -101,6 +113,27 @@ Switching completed
OUT OUT
} }
@test "dctlenv use [<version>]: 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 <<OUT
No installed versions of driftctl matched '0.3.1', let's install it
Switching version to v0.3.1
Switching completed
OUT
}
teardown() { teardown() {
rm -rf "$DCTLENV_TMPDIR" rm -rf "$DCTLENV_TMPDIR"
} }