You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement the tfenv use command for the Go edition — switching the active Terraform version.
Parent Epic
Part of #488 — Go Edition: Full Feature Parity Implementation
Motivation
tfenv use sets the active Terraform version by writing to the default version file. It must resolve version specifiers, verify the version is installed, and update the version file atomically.
Clean-Room Constraint
This is a clean-room implementation. Contributors MUST NOT read, reference, copy, or adapt source code from tofuutils/tenv, hashicorp/hc-install, or any other third-party tfenv-like tool. The sole reference is tfenv's own Bash source code, documentation, and test suite.
Proposed Design
Command Interface
tfenv use <version>
tfenv use latest
tfenv use latest:<regex>
tfenv use latest-allowed
tfenv use min-required
tfenv use - # Switch to previous version
When called without arguments, reads .terraform-version or default version file, or TFENV_TERRAFORM_VERSION.
Use Flow
If argument is -, read previous version from ${TFENV_CONFIG_DIR}/version.prev
If no argument and no TFENV_TERRAFORM_VERSION, read from version file chain
Resolve version specifier → concrete version number
Verify the version is installed in ${TFENV_CONFIG_DIR}/versions/${version}/
Verify the terraform binary exists and is executable
If not installed and TFENV_AUTO_INSTALL=true, install it first
Save current version as previous: If ${TFENV_CONFIG_DIR}/version exists and differs from the new version, copy its content to ${TFENV_CONFIG_DIR}/version.prev
Write new version to ${TFENV_CONFIG_DIR}/version (the default version file)
Print confirmation: Switching default version to v${version}
If the default version file is overridden by a .terraform-version file in the search path, print warning: Default version file overridden by <path>, changing the default version has no effect
Previous Version (tfenv use -)
Reads ${TFENV_CONFIG_DIR}/version.prev to get the last-used version
Error if version.prev does not exist ("No previous version to switch to")
Error if version.prev is empty
Strips carriage returns from the file content (Windows compatibility)
The version.prev file is updated on every successful use (including use -)
Version File Writing
Write to ${TFENV_CONFIG_DIR}/version
Atomic write: write to temp file, then rename
File contains just the version number (e.g., 1.5.0\n)
Version Resolution for latest Keywords
When use is called with latest, latest:<regex>, etc., the resolution searches locally installed versions first (not remote). Remote is only consulted if TFENV_AUTO_INSTALL=true and no local match is found. This is different from install, which always checks remote. Reference: libexec/tfenv-use lines 112-140.
Acceptance Criteria
tfenv use 1.5.0 sets the default version to 1.5.0
tfenv use latest resolves and sets the latest installed version
tfenv use latest:^1.5 resolves and sets the latest installed 1.5.x
tfenv use latest-allowed resolves from HCL constraints
tfenv use min-required resolves minimum from HCL constraints
tfenv use - switches to the previous version
tfenv use - errors clearly if no previous version exists
tfenv use (no args) reads from .terraform-version or default version file or TFENV_TERRAFORM_VERSION
Error if specified version is not installed (and auto-install is off)
Auto-install triggers if version is missing and TFENV_AUTO_INSTALL=true
Current version is saved to version.prev before overwriting
version.prev is NOT written when the new version equals the current version
Default version file is updated atomically
Warning printed when .terraform-version overrides the default version file
Terraform binary is verified present and executable before confirming switch
Exit code 0 on success, non-zero on failure
Confirmation message printed on success
Acceptance tests verify version switching, file content, and use -
Reference libexec/tfenv-use for the Bash implementation (190 lines including standalone boilerplate)
Key Bash behaviours to match:
Lines 78-88: tfenv use - reads version.prev file
Lines 89-102: No-arg resolution from version file or TFENV_TERRAFORM_VERSION
Lines 161-168: Save current version to version.prev before overwriting
Lines 175-178: Override warning when .terraform-version overrides default
The use command with latest keywords should filter against locally installed versions by default (not remote), unless the version needs to be installed
The Bash edition writes a plain version file (not a symlink). The Go edition should do the same.
Pre-release filtering: when latest is used with a numeric-only regex, exclude pre-release versions (containing -). When the regex contains letters, include pre-releases. Reference: libexec/tfenv-use lines 114-129.
Summary
Implement the
tfenv usecommand for the Go edition — switching the active Terraform version.Parent Epic
Part of #488 — Go Edition: Full Feature Parity Implementation
Motivation
tfenv usesets the active Terraform version by writing to the default version file. It must resolve version specifiers, verify the version is installed, and update the version file atomically.Clean-Room Constraint
This is a clean-room implementation. Contributors MUST NOT read, reference, copy, or adapt source code from
tofuutils/tenv,hashicorp/hc-install, or any other third-party tfenv-like tool. The sole reference is tfenv's own Bash source code, documentation, and test suite.Proposed Design
Command Interface
When called without arguments, reads
.terraform-versionor default version file, orTFENV_TERRAFORM_VERSION.Use Flow
-, read previous version from${TFENV_CONFIG_DIR}/version.prevTFENV_TERRAFORM_VERSION, read from version file chain${TFENV_CONFIG_DIR}/versions/${version}/TFENV_AUTO_INSTALL=true, install it first${TFENV_CONFIG_DIR}/versionexists and differs from the new version, copy its content to${TFENV_CONFIG_DIR}/version.prev${TFENV_CONFIG_DIR}/version(the default version file)Switching default version to v${version}.terraform-versionfile in the search path, print warning:Default version file overridden by <path>, changing the default version has no effectPrevious Version (
tfenv use -)${TFENV_CONFIG_DIR}/version.prevto get the last-used versionversion.prevdoes not exist ("No previous version to switch to")version.previs emptyuse(includinguse -)Version File Writing
${TFENV_CONFIG_DIR}/version1.5.0\n)Version Resolution for
latestKeywordsWhen
useis called withlatest,latest:<regex>, etc., the resolution searches locally installed versions first (not remote). Remote is only consulted ifTFENV_AUTO_INSTALL=trueand no local match is found. This is different frominstall, which always checks remote. Reference:libexec/tfenv-uselines 112-140.Acceptance Criteria
tfenv use 1.5.0sets the default version to 1.5.0tfenv use latestresolves and sets the latest installed versiontfenv use latest:^1.5resolves and sets the latest installed 1.5.xtfenv use latest-allowedresolves from HCL constraintstfenv use min-requiredresolves minimum from HCL constraintstfenv use -switches to the previous versiontfenv use -errors clearly if no previous version existstfenv use(no args) reads from.terraform-versionor default version file orTFENV_TERRAFORM_VERSIONTFENV_AUTO_INSTALL=trueversion.prevbefore overwritingversion.previs NOT written when the new version equals the current version.terraform-versionoverrides the default version fileuse -Dependencies
Implementation Notes
libexec/tfenv-usefor the Bash implementation (190 lines including standalone boilerplate)tfenv use -readsversion.prevfileTFENV_TERRAFORM_VERSIONversion.prevbefore overwriting.terraform-versionoverrides defaultusecommand withlatestkeywords should filter against locally installed versions by default (not remote), unless the version needs to be installedlatestis used with a numeric-only regex, exclude pre-release versions (containing-). When the regex contains letters, include pre-releases. Reference:libexec/tfenv-uselines 114-129.Labels
type:feature,priority:high,complexity:small,category:use