2021-01-23 16:58:29 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -uo pipefail
|
|
|
|
|
|
|
|
[ "$#" -gt 1 ] && log_error "Need 1 version instead of $#" 'usage: dctlenv use [<version>]'
|
|
|
|
|
|
|
|
version_requested="${1:-""}"
|
|
|
|
[ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv use [<version>]'
|
|
|
|
|
2021-03-08 10:48:56 +00:00
|
|
|
version_requested=$(echo $version_requested | sed 's/^v//')
|
|
|
|
|
2021-03-05 17:27:43 +00:00
|
|
|
if [[ $version_requested == "latest" ]]; then
|
|
|
|
version_requested="$(dctlenv-list-remote | tail -n 1)"
|
|
|
|
fi
|
|
|
|
|
2021-01-23 16:58:29 +00:00
|
|
|
log_debug "Searching $DCTLENV_ROOT/versions for version matching $version_requested"
|
2021-03-08 10:25:22 +00:00
|
|
|
version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; 2>/dev/null \
|
2021-01-23 16:58:29 +00:00
|
|
|
| tail -n +2 \
|
|
|
|
| sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \
|
|
|
|
| grep -e "$version_requested" \
|
|
|
|
| head -n 1
|
|
|
|
)"
|
|
|
|
|
2021-02-03 21:32:23 +00:00
|
|
|
if [ -n "$version" ]; then
|
|
|
|
log_debug "Found version: $version"
|
|
|
|
else
|
2021-03-05 17:27:43 +00:00
|
|
|
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
|
2021-02-03 21:32:23 +00:00
|
|
|
fi
|
2021-01-23 16:58:29 +00:00
|
|
|
|
|
|
|
target_path="$DCTLENV_ROOT/versions/$version"
|
|
|
|
[ -f "$target_path/driftctl" ] \
|
|
|
|
|| log_error "Version directory for $version is present, but the driftctl binary is not! Manual intervention required"
|
|
|
|
[ -x "$target_path/driftctl" ] \
|
|
|
|
|| log_error "Version directory for $version is present, but the driftctl binary is not executable! Manual intervention required"
|
|
|
|
|
|
|
|
echo "Switching version to v$version"
|
|
|
|
version_file="$(dctlenv-version-file)"
|
|
|
|
|
|
|
|
log_debug "Writing \"$version\" to \"$version_file\""
|
|
|
|
echo "$version" > "$version_file" || log_error "Failed to switch to v$version"
|
|
|
|
|
2021-02-01 18:16:55 +00:00
|
|
|
$(driftctl version 1>/dev/null) || log_error "'driftctl version' failed, something is wrong"
|
2021-01-23 16:58:29 +00:00
|
|
|
|
|
|
|
echo "Switching completed"
|