From aa8e822c71e5fdb546f1673360fb555dbc9e9a63 Mon Sep 17 00:00:00 2001 From: William Beuil Date: Mon, 25 Jan 2021 15:36:44 +0100 Subject: [PATCH] Add uninstall command --- README.md | 10 ++++++++++ libexec/dctlenv-uninstall | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 libexec/dctlenv-uninstall diff --git a/README.md b/README.md index cff427b..8677248 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,16 @@ $ dctlenv list-remote 0.2.3 ``` +### `dctlenv uninstall []` + +Uninstall a specific version of driftctl. + +```shell +$ dctlenv uninstall 0.2.3 +Uninstall driftctl v0.2.3 +driftctl v0.2.3 is successfully uninstalled +``` + ### `dctlenv root` Display the root directory where dctlenv is. diff --git a/libexec/dctlenv-uninstall b/libexec/dctlenv-uninstall new file mode 100755 index 0000000..3e275d4 --- /dev/null +++ b/libexec/dctlenv-uninstall @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -uo pipefail + +[ "$#" -gt 1 ] && log_error "Need 1 version instead of $#" 'usage: dctlenv uninstall []' + +version_requested="${1:-""}" +[ -n "$version_requested" ] || log_error 'Version is not specified' 'usage: dctlenv uninstall []' + +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" + +dst_path="$DCTLENV_ROOT/versions/$version" +if [ -f "$dst_path/driftctl" ]; then + echo "Uninstall driftctl v$version" + rm -r "$dst_path" + + # If no versions remain, remove the versions directory + rmdir "$DCTLENV_ROOT/versions" 2>/dev/null + + echo "driftctl v$version is successfully uninstalled" +fi