Skip to content

Commit 9984a00

Browse files
committed
add terraform-local version when using version flag
1 parent 0ea14b3 commit 9984a00

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

bin/tflocal

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,11 @@ def get_provider_version_from_lock_file() -> Optional[version.Version]:
640640
AWS_PROVIDER_VERSION = version.parse(provider_version)
641641

642642

643+
def get_tf_local_version():
644+
from importlib.metadata import version
645+
return version("terraform-local")
646+
647+
643648
def is_service_endpoint_supported(service_name: str) -> bool:
644649
if service_name not in VERSIONED_SERVICE_EXCLUSIONS or not AWS_PROVIDER_VERSION:
645650
return True
@@ -728,8 +733,17 @@ def main():
728733
print(f"Unable to determine version. See error message for details: {e}")
729734
exit(1)
730735

731-
if len(sys.argv) > 1 and sys.argv[1] != "init":
732-
get_provider_version_from_lock_file()
736+
if len(sys.argv) > 1:
737+
if sys.argv[1] != "init":
738+
get_provider_version_from_lock_file()
739+
if sys.argv[1] in ("--version", "-v", "-version"):
740+
try:
741+
# the version flag could be something else than the 1st argument, it is possible to do
742+
# `terraform init -version` and it will return the version only without init, but we should probably
743+
# only support the easy case
744+
print(f"terraform-local v{get_tf_local_version()}", file=sys.stderr)
745+
except Exception:
746+
pass
733747

734748
config_override_files = []
735749

tests/test_apply.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,23 @@ def check_override_file_content_for_alias(override_file):
674674
return True
675675

676676

677+
@pytest.mark.parametrize("version_flag", ["--version", "-v", "-version"])
678+
def test_version_command(monkeypatch, version_flag):
679+
def _run(cmd, **kwargs):
680+
kwargs["stderr"] = subprocess.STDOUT
681+
return subprocess.check_output(cmd, **kwargs)
682+
683+
with tempfile.TemporaryDirectory(delete=True) as temp_dir:
684+
# we need the `terraform init` command to create a lock file, so it cannot be a `DRY_RUN`
685+
output = _run([TFLOCAL_BIN, version_flag], cwd=temp_dir, env=dict(os.environ))
686+
assert b"terraform-local v" in output
687+
688+
monkeypatch.setenv("DRY_RUN", "1")
689+
output = _run([TFLOCAL_BIN, version_flag], cwd=temp_dir, env=dict(os.environ))
690+
691+
assert b"terraform-local v" in output
692+
693+
677694
###
678695
# UTIL FUNCTIONS
679696
###

0 commit comments

Comments
 (0)