Skip to content

Commit

Permalink
Support customizing the config directory via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
e-nomem committed May 29, 2020
1 parent 4f1ef20 commit 14608d4
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 38 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ To install from a remote other than the default
TFENV_REMOTE=https://example.jfrog.io/artifactory/hashicorp
```

##### `TFENV_CONFIG_DIR`

Path (Default: `$TFENV_ROOT`)

The path to a directory where the local terraform versions and configuration files exist.

```console
TFENV_CONFIG_DIR="$XDG_CONFIG_HOME/tfenv"
```

#### Bashlog Logging Library

##### `BASHLOG_COLOURS`
Expand Down
19 changes: 13 additions & 6 deletions lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ else
fi;
export TFENV_ROOT;

if [ -z "${TFENV_CONFIG_DIR:-""}" ]; then
TFENV_CONFIG_DIR="$TFENV_ROOT";
else
TFENV_CONFIG_DIR="${TFENV_CONFIG_DIR%/}";
fi
export TFENV_CONFIG_DIR;

if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
[ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ] || export DEBUG="${TFENV_DEBUG:-0}";
if [[ "${TFENV_DEBUG}" -gt 2 ]]; then
Expand All @@ -43,13 +50,13 @@ resolve_version () {
version_file="$(tfenv-version-file)";
log 'debug' "Version File: ${version_file}";

if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)";
if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";

elif [ -f "${version_file}" ]; then
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)";
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";

Expand All @@ -60,7 +67,7 @@ resolve_version () {
fi;

else
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version) but it doesn't exist";
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version) but it doesn't exist";
log 'info' 'No version requested on the command line or in the version file search path. Installing "latest"';
version_requested='latest';
fi;
Expand Down Expand Up @@ -115,14 +122,14 @@ export -f check_active_version;

check_installed_version() {
local v="${1}";
local bin="${TFENV_ROOT}/versions/${v}/terraform";
local bin="${TFENV_CONFIG_DIR}/versions/${v}/terraform";
[ -n "$(${bin} --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ];
};
export -f check_installed_version;

check_default_version() {
local v="${1}";
local def="$(cat "${TFENV_ROOT}/version")";
local def="$(cat "${TFENV_CONFIG_DIR}/version")";
[ "${def}" == "${v}" ];
};
export -f check_default_version;
Expand Down
4 changes: 2 additions & 2 deletions libexec/tfenv-exec
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ TFENV_VERSION="$(tfenv-version-name)" \
};
export TFENV_VERSION;

if [ ! -d "${TFENV_ROOT}/versions/${TFENV_VERSION}" ]; then
if [ ! -d "${TFENV_CONFIG_DIR}/versions/${TFENV_VERSION}" ]; then
if [ "${TFENV_AUTO_INSTALL:-true}" == "true" ]; then
log 'info' "version '${TFENV_VERSION}' is not installed (set by $(tfenv-version-file)). Installing now as TFENV_AUTO_INSTALL==true";
tfenv-install;
Expand All @@ -92,7 +92,7 @@ if [ ! -d "${TFENV_ROOT}/versions/${TFENV_VERSION}" ]; then
fi;
fi;

TF_BIN_PATH="${TFENV_ROOT}/versions/${TFENV_VERSION}/terraform";
TF_BIN_PATH="${TFENV_CONFIG_DIR}/versions/${TFENV_VERSION}/terraform";
export PATH="${TF_BIN_PATH}:${PATH}";
log 'debug' "TF_BIN_PATH added to PATH: ${TF_BIN_PATH}";
log 'debug' "Executing: ${TF_BIN_PATH} $@";
Expand Down
12 changes: 6 additions & 6 deletions libexec/tfenv-install
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ log 'debug' "Processing install for version ${version}, using regex ${regex}";
version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)";
[ -n "${version}" ] || log 'error' "No versions matching '${requested}' found in remote";

dst_path="${TFENV_ROOT}/versions/${version}";
dst_path="${TFENV_CONFIG_DIR}/versions/${version}";
if [ -f "${dst_path}/terraform" ]; then
echo "Terraform v${version} is already installed";
exit 0;
Expand Down Expand Up @@ -178,12 +178,12 @@ if [[ -n "${keybase_bin}" && -x "${keybase_bin}" ]]; then
|| log 'error' 'SHA256SUMS signature does not match!';
fi;

elif [[ -f "${TFENV_ROOT}/use-gnupg" ]]; then
elif [[ -f "${TFENV_CONFIG_DIR}/use-gnupg" ]]; then
# GnuPG uses the user's keyring, and any web-of-trust or local signatures or
# anything else they have setup. This is the crazy-powerful mode which is
# overly confusing to newcomers. We don't support it without the user creating
# the file use-gnupg, optionally with directives in it.
gnupg_command="$(sed -E -n -e 's/^binary: *//p' <"${TFENV_ROOT}/use-gnupg")";
gnupg_command="$(sed -E -n -e 's/^binary: *//p' <"${TFENV_CONFIG_DIR}/use-gnupg")";
[[ -n "${gnupg_command}" ]] || gnupg_command=gpg;

download_signature;
Expand All @@ -194,13 +194,13 @@ elif [[ -f "${TFENV_ROOT}/use-gnupg" ]]; then
"${download_tmp}/${shasums_name}" \
|| log 'error' 'PGP signature rejected by GnuPG';

elif [[ -f "${TFENV_ROOT}/use-gpgv" ]]; then
elif [[ -f "${TFENV_CONFIG_DIR}/use-gpgv" ]]; then
# gpgv is a much simpler interface to verification, but does require that the
# key have been downloaded and marked trusted.
# We don't force the caller to trust the tfenv repo's copy of their key, they
# have to choose to make that trust decision.
gpgv_command="$(sed -E -n -e 's/^binary: *//p' <"${TFENV_ROOT}/use-gpgv")";
trust_tfenv="$(sed -E -n -e 's/^trust.?tfenv: *//p' <"${TFENV_ROOT}/use-gpgv")";
gpgv_command="$(sed -E -n -e 's/^binary: *//p' <"${TFENV_CONFIG_DIR}/use-gpgv")";
trust_tfenv="$(sed -E -n -e 's/^trust.?tfenv: *//p' <"${TFENV_CONFIG_DIR}/use-gpgv")";
[[ -n "${gpgv_command}" ]] || gpgv_command=gpgv;

download_signature;
Expand Down
8 changes: 4 additions & 4 deletions libexec/tfenv-list
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ done;
[ "${#}" -ne 0 ] \
&& log 'error' "usage: tfenv list"

[ -d "${TFENV_ROOT}/versions" ] \
[ -d "${TFENV_CONFIG_DIR}/versions" ] \
|| log 'error' 'No versions available. Please install one with: tfenv install'

[[ -x "${TFENV_ROOT}/versions" && -r "${TFENV_ROOT}/versions" ]] \
|| log 'error' "tfenv versions directory is inaccessible: ${TFENV_ROOT}/versions";
[[ -x "${TFENV_CONFIG_DIR}/versions" && -r "${TFENV_CONFIG_DIR}/versions" ]] \
|| log 'error' "tfenv versions directory is inaccessible: ${TFENV_CONFIG_DIR}/versions";

version_name="$(tfenv-version-name)" \
&& log 'debug' "tfenv-version-name reported: ${version_name}" \
Expand All @@ -87,7 +87,7 @@ print_version () {
};

log 'debug' 'Listing versions...';
local_versions=($(\find "${TFENV_ROOT}/versions" -type d -exec basename {} \; \
local_versions=($(\find "${TFENV_CONFIG_DIR}/versions" -type d -exec basename {} \; \
| tail -n +2 \
| sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3));

Expand Down
8 changes: 4 additions & 4 deletions libexec/tfenv-resolve-version
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ if [ -z "${arg}" ]; then
version_file="$(tfenv-version-file)";
log 'debug' "Version File: ${version_file}";

if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)";
if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";

elif [ -f "${version_file}" ]; then
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)";
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";

Expand All @@ -86,7 +86,7 @@ if [ -z "${arg}" ]; then
fi;

else
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version) but it doesn't exist";
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version) but it doesn't exist";
log 'info' 'No version requested on the command line or in the version file search path. Installing "latest"';
version_requested='latest';
fi;
Expand Down
12 changes: 6 additions & 6 deletions libexec/tfenv-uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ declare arg="${1:-""}";
if [ -z "${arg:-""}" ]; then
version_file="$(tfenv-version-file)";
log 'debug' "Version File: ${version_file}";
if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)";
if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then
log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";
elif [ -f "${version_file}" ]; then
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)";
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)";
version_requested="$(cat "${version_file}")" \
|| log 'error' "Failed to open ${version_file}";
else
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version) but it doesn't exist";
log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version) but it doesn't exist";
log 'info' 'No version requested on the command line or in the version file search path. Installing "latest"';
version_requested='latest';
fi;
Expand Down Expand Up @@ -108,13 +108,13 @@ log 'debug' "Processing uninstall for version ${version}, using regex ${regex}";
version="$(tfenv-list | sed -E 's/^(\*| )? //g; s/ \(set by .+\)$//' | grep -e "${regex}" | head -n 1)";
[ -n "${version}" ] || log 'error' "No versions matching '${regex}' found in local";

dst_path="${TFENV_ROOT}/versions/${version}";
dst_path="${TFENV_CONFIG_DIR}/versions/${version}";
if [ -f "${dst_path}/terraform" ]; then
log 'info' "Uninstall Terraform v${version}";
rm -r "${dst_path}";

# If no versions remain, remove the versions directory
rmdir "${TFENV_ROOT}/versions" 2>/dev/null;
rmdir "${TFENV_CONFIG_DIR}/versions" 2>/dev/null;

log 'info' "Terraform v${version} is successfully uninstalled";
fi;
10 changes: 5 additions & 5 deletions libexec/tfenv-use
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ done;

[ "${#}" -gt 1 ] && log 'error' 'usage: tfenv use [<version>]';

[ -d "${TFENV_ROOT}/versions" ] \
[ -d "${TFENV_CONFIG_DIR}/versions" ] \
|| log 'error' 'No versions of terraform installed. Please install one with: tfenv install';

declare requested="${1:-""}";
Expand All @@ -71,8 +71,8 @@ declare resolved="$(tfenv-resolve-version ${requested})";
declare version="${resolved%%\:*}";
declare regex="${resolved##*\:}";

log 'debug' "Searching ${TFENV_ROOT}/versions for latest version matching ${regex}";
declare version="$(\find "${TFENV_ROOT}/versions" -type d -exec basename {} \; \
log 'debug' "Searching ${TFENV_CONFIG_DIR}/versions for latest version matching ${regex}";
declare version="$(\find "${TFENV_CONFIG_DIR}/versions" -type d -exec basename {} \; \
| tail -n +2 \
| sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \
| grep -e "${regex}" \
Expand All @@ -83,14 +83,14 @@ declare version="$(\find "${TFENV_ROOT}/versions" -type d -exec basename {} \; \
&& log 'debug' "Found version: ${version}" \
|| log 'error' "No installed versions of terraform matched '${1}'";

target_path="${TFENV_ROOT}/versions/${version}";
target_path="${TFENV_CONFIG_DIR}/versions/${version}";
[ -f "${target_path}/terraform" ] \
|| log 'error' "Version directory for ${version} is present, but the terraform binary is not! Manual intervention required.";
[ -x "${target_path}/terraform" ] \
|| log 'error' "Version directory for ${version} is present, but the terraform binary is not executable! Manual intervention required.";

log 'info' "Switching default version to v${version}";
version_file="${TFENV_ROOT}/version";
version_file="${TFENV_CONFIG_DIR}/version";
log 'debug' "Writing \"${version}\" to \"${version_file}\"";
echo "${version}" > "${version_file}" \
|| log 'error' "Switch to v${version} failed";
Expand Down
4 changes: 2 additions & 2 deletions libexec/tfenv-version-file
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ find_local_version_file() {

if ! find_local_version_file "${TFENV_DIR:-${PWD}}"; then
if ! find_local_version_file "${HOME:-/}"; then
log 'debug' "No version file found in search paths. Defaulting to TFENV_ROOT: ${TFENV_ROOT}/version";
echo "${TFENV_ROOT}/version";
log 'debug' "No version file found in search paths. Defaulting to TFENV_CONFIG_DIR: ${TFENV_CONFIG_DIR}/version";
echo "${TFENV_CONFIG_DIR}/version";
fi;
fi;
6 changes: 3 additions & 3 deletions libexec/tfenv-version-name
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ done;
# Begin Script Body #
#####################

[ -d "${TFENV_ROOT}/versions" ] \
[ -d "${TFENV_CONFIG_DIR}/versions" ] \
|| log 'error' 'No versions of terraform installed. Please install one with: tfenv install';

TFENV_VERSION_FILE="$(tfenv-version-file)" \
Expand All @@ -81,7 +81,7 @@ if [[ "${TFENV_VERSION}" =~ ^latest.*$ ]]; then
log 'debug' "'latest' keyword does not use regex";
fi;

version="$(\find "${TFENV_ROOT}/versions" -type d -exec basename {} \; \
version="$(\find "${TFENV_CONFIG_DIR}/versions" -type d -exec basename {} \; \
| tail -n +2 \
| sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \
| grep -e "${regex}" \
Expand All @@ -101,7 +101,7 @@ fi;
[ -z "${TFENV_VERSION}" ] \
&& log 'error' "Version could not be resolved (set by ${TFENV_VERSION_FILE} or tfenv use <version>)";

if [ ! -d "${TFENV_ROOT}/versions/${TFENV_VERSION}" ]; then
if [ ! -d "${TFENV_CONFIG_DIR}/versions/${TFENV_VERSION}" ]; then
log 'debug' "version '${TFENV_VERSION}' is not installed (set by ${TFENV_VERSION_FILE})";
fi;

Expand Down

0 comments on commit 14608d4

Please sign in to comment.