$ show tfutils/tfenv
tfenv is a command-line Terraform version manager written in Bash, modelled after rbenv. It provides a `terraform` shim that intercepts invocations and selects the appropriate Terraform binary based on a `.terraform-version` file, the `TFENV_TERRAFORM_VERSION` environment variable, or the default version file under `TFENV_CONFIG_DIR`. The repository implements full version lifecycle operations: install (download, verify SHA256 and optional PGP signatures, unzip), switch (`use`), uninstall (including multiple versions), list local and remote versions, pin current version, and a `min-required`/`latest-allowed` resolver that reads `required_version` constraints from `.tf` files. Architecturally it follows the rbenv pattern: a dispatcher (`bin/tfenv`), a shim (`bin/terraform`), shared libraries in `lib/` (logging, version-file discovery, version-name resolution, execution), and standalone subcommand scripts in `libexec/` that each include boilerplate to resolve `TFENV_ROOT`. The code deliberately avoids shellcheck and additional dependencies, maintaining portability across Linux, macOS, and Windows (git-bash). An extensive integration test suite downloads real Terraform binaries, and CI covers a broad OS matrix plus Docker validation. The codebase demonstrates solid engineering practice for a Bash project: clean separation of concerns, a capable logging library (`lib/bashlog.sh`) that supports file, JSON, syslog and coloured console output; careful handling of cross-platform differences (BSD vs GNU grep, CRLF line endings, ARM architecture detection); and a concurrency-safe install lock using atomic `mkdir`. Notable weaknesses are intentional boilerplate duplication across `libexec/` scripts, a test runner that historically exits 0 on failure (documented as a known bug being fixed), and regex-based HCL constraint parsing that is functional but fragile. A proposed ADR outlines a future Go edition, but the Bash implementation remains canonical.
The LLM identified 3 files worth highlighting for reuse:
A self-contained Bash logging library providing severity-based console output with colours, plus optional file, JSON, and syslog sinks. It supports custom date formats, custom eval prefixes (with a warning), and runtime debug-level gating. The library exports a `log` function and is used by all tfenv scripts. It is isolated (only depends on coreutils/awk/date), making it directly extractable to any Bash project needing structured logging.
Implements upward directory traversal to locate a per-project version marker (`.terraform-version`) from the current directory to filesystem root, then falls back to `$HOME/.terraform-version`, and finally to a default config file. The algorithm is a clean, reusable pattern for tools that need project-scoped configuration discovery, and is independent of tfenv-specific logic aside from the filename.
Contains `realpath-relative-to`, a portable Bash implementation of GNU's `realpath --relative-to` that resolves symlinks and computes relative paths without requiring GNU coreutils. This is a non-obvious utility for cross-platform shell scripts that need symlink-aware path manipulation. The function is self-contained and can be lifted into any Bash codebase.