Skip to content

Commit 72f5a2a

Browse files
authored
add terraform-local version when using version flag (#84)
1 parent 0ea14b3 commit 72f5a2a

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ADDITIONAL_TF_OVERRIDE_LOCATIONS=/path/to/module1,path/to/module2 tflocal plan
7777

7878
## Change Log
7979

80+
* v0.24.0: Add support to return `terraform-local` version when calling `tflocal -version` and fix AWS provider detection
8081
* v0.23.1: Fix endpoint overrides for Terraform AWS provider >= 6.0.0-beta2
8182
* v0.23.0: Add support for `terraform_remote_state` with `s3` backend to read the state stored in local S3 backend; fix S3 backend config detection with multiple Terraform blocks
8283
* v0.22.0: Fix S3 backend forcing DynamoDB State Lock to be enabled by default

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

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = terraform-local
3-
version = 0.23.1
3+
version = 0.24.0
44
url = https://github.com/localstack/terraform-local
55
author = LocalStack Team
66
author_email = info@localstack.cloud

tests/test_apply.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,22 @@ 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+
output = _run([TFLOCAL_BIN, version_flag], cwd=temp_dir, env=dict(os.environ))
685+
assert b"terraform-local v" in output
686+
687+
monkeypatch.setenv("DRY_RUN", "1")
688+
output = _run([TFLOCAL_BIN, version_flag], cwd=temp_dir, env=dict(os.environ))
689+
690+
assert b"terraform-local v" in output
691+
692+
677693
###
678694
# UTIL FUNCTIONS
679695
###

0 commit comments

Comments
 (0)