diff --git a/README.md b/README.md index 5eb3976..228eac1 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ ADDITIONAL_TF_OVERRIDE_LOCATIONS=/path/to/module1,path/to/module2 tflocal plan ## Change Log +* v0.24.0: Add support to return `terraform-local` version when calling `tflocal -version` and fix AWS provider detection * v0.23.1: Fix endpoint overrides for Terraform AWS provider >= 6.0.0-beta2 * 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 * v0.22.0: Fix S3 backend forcing DynamoDB State Lock to be enabled by default diff --git a/bin/tflocal b/bin/tflocal index 521078d..ffa5bb1 100755 --- a/bin/tflocal +++ b/bin/tflocal @@ -640,6 +640,11 @@ def get_provider_version_from_lock_file() -> Optional[version.Version]: AWS_PROVIDER_VERSION = version.parse(provider_version) +def get_tf_local_version(): + from importlib.metadata import version + return version("terraform-local") + + def is_service_endpoint_supported(service_name: str) -> bool: if service_name not in VERSIONED_SERVICE_EXCLUSIONS or not AWS_PROVIDER_VERSION: return True @@ -728,8 +733,17 @@ def main(): print(f"Unable to determine version. See error message for details: {e}") exit(1) - if len(sys.argv) > 1 and sys.argv[1] != "init": - get_provider_version_from_lock_file() + if len(sys.argv) > 1: + if sys.argv[1] != "init": + get_provider_version_from_lock_file() + if sys.argv[1] in ("--version", "-v", "-version"): + try: + # the version flag could be something else than the 1st argument, it is possible to do + # `terraform init -version` and it will return the version only without init, but we should probably + # only support the easy case + print(f"terraform-local v{get_tf_local_version()}", file=sys.stderr) + except Exception: + pass config_override_files = [] diff --git a/setup.cfg b/setup.cfg index f6840dc..d4617e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = terraform-local -version = 0.23.1 +version = 0.24.0 url = https://github.com/localstack/terraform-local author = LocalStack Team author_email = info@localstack.cloud diff --git a/tests/test_apply.py b/tests/test_apply.py index 7dd6d07..412be98 100644 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -674,6 +674,22 @@ def check_override_file_content_for_alias(override_file): return True +@pytest.mark.parametrize("version_flag", ["--version", "-v", "-version"]) +def test_version_command(monkeypatch, version_flag): + def _run(cmd, **kwargs): + kwargs["stderr"] = subprocess.STDOUT + return subprocess.check_output(cmd, **kwargs) + + with tempfile.TemporaryDirectory(delete=True) as temp_dir: + output = _run([TFLOCAL_BIN, version_flag], cwd=temp_dir, env=dict(os.environ)) + assert b"terraform-local v" in output + + monkeypatch.setenv("DRY_RUN", "1") + output = _run([TFLOCAL_BIN, version_flag], cwd=temp_dir, env=dict(os.environ)) + + assert b"terraform-local v" in output + + ### # UTIL FUNCTIONS ###