Skip to content
Snippets Groups Projects
Commit ac0d1cab authored by nimrod's avatar nimrod
Browse files

Version 0.2.

Add the `TF_CLI` environment variable to use a different terraform
binary (like opentofu) and some documentation.
parent a9155064
No related branches found
No related tags found
No related merge requests found
Pipeline #3691 failed
...@@ -51,6 +51,12 @@ the same name as the workspace, for all the files inside that directory that ...@@ -51,6 +51,12 @@ the same name as the workspace, for all the files inside that directory that
end with :code:`.tfvars`, a :code:`-var-file` argument is added. For example: end with :code:`.tfvars`, a :code:`-var-file` argument is added. For example:
:code:`-var-file=prod/a.tfvars` and :code:`-var-file=prod/b.tfvars`. :code:`-var-file=prod/a.tfvars` and :code:`-var-file=prod/b.tfvars`.
By default :code:`tf` invokes :code:`terraform`, but if you're using a
different tool (like `OpenTofu <https://opentofu.org/>`_ you can set the
:code:`TF_CLI` environment variable to that tool name. If you wish to know the
exact command :code:`tf` is running set the :code:`TF_DEBUG` environment
variable to :code:`1` and the command will printed before the it's invoked.
License License
------- -------
......
...@@ -8,19 +8,24 @@ import pathlib ...@@ -8,19 +8,24 @@ import pathlib
import subprocess # nosec import subprocess # nosec
import sys import sys
__version__ = "0.1.0" __version__ = "0.2.0"
TF_COMMANDS_TO_MODIFY = ["plan", "console", "import", "refresh"] TF_COMMANDS_TO_MODIFY = ["plan", "console", "import", "refresh"]
TF_CLI = os.getenv("TF_CLI", "terraform")
def get_workspace(): def get_workspace():
"""Return the Terraform workspace.""" """Return the Terraform workspace."""
try:
proc = subprocess.run( # nosec proc = subprocess.run( # nosec
["terraform", "workspace", "show"], [TF_CLI, "workspace", "show"],
capture_output=True, capture_output=True,
check=True, check=True,
text=True, text=True,
) )
except FileNotFoundError:
print(f"Can't find {TF_CLI}.", file=sys.stderr)
sys.exit(1)
return proc.stdout.strip() return proc.stdout.strip()
...@@ -33,11 +38,15 @@ def is_debug_set(): ...@@ -33,11 +38,15 @@ def is_debug_set():
def wrapper(): def wrapper():
# In case tf was run with no arguments. # In case tf was run with no arguments.
if len(sys.argv) == 1: if len(sys.argv) == 1:
os.execlp("terraform", "terraform") # nosec try:
os.execlp(TF_CLI, TF_CLI) # nosec
except FileNotFoundError:
print(f"Can't find {TF_CLI}.", file=sys.stderr)
sys.exit(1)
# Build a new argument list. # Build a new argument list.
args = sys.argv[:] args = sys.argv[:]
args[0] = "terraform" args[0] = TF_CLI
# Check if the Terraform command is one that we need to modify (include # Check if the Terraform command is one that we need to modify (include
# tfvars files). If not, execute Terraform with the same arguments. # tfvars files). If not, execute Terraform with the same arguments.
...@@ -45,7 +54,11 @@ def wrapper(): ...@@ -45,7 +54,11 @@ def wrapper():
if command in sys.argv: if command in sys.argv:
break break
else: else:
os.execvp("terraform", args) # nosec try:
os.execvp(TF_CLI, args) # nosec
except FileNotFoundError:
print(f"Can't find {TF_CLI}.", file=sys.stderr)
sys.exit(1)
# We need to add the var files after the Terraform command (if we add it # We need to add the var files after the Terraform command (if we add it
# before Terraform doesn't accept them) but not at the end (not to modify # before Terraform doesn't accept them) but not at the end (not to modify
...@@ -65,7 +78,11 @@ def wrapper(): ...@@ -65,7 +78,11 @@ def wrapper():
if is_debug_set(): if is_debug_set():
print(args, file=sys.stderr) print(args, file=sys.stderr)
os.execvp("terraform", args) # nosec try:
os.execvp(TF_CLI, args) # nosec
except FileNotFoundError:
print(f"Can't find {TF_CLI}.", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment