Skip to content

Commit

Permalink
[159] Improve apply arguments parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
borland667 authored and Osvaldo Demo committed Apr 11, 2024
1 parent 00cbdd1 commit d4e1ceb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
52 changes: 45 additions & 7 deletions leverage/modules/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,56 @@ def _plan(tf, args):
raise Exit(exit_code)


def handle_apply_arguments_parsing(args):
"""Parse and process the arguments for the 'apply' command."""
# Initialize new_args to handle both '-key=value' and '-key value'
new_args = []
skip_next = False # Flag to skip the next argument if it's part of '-key value'

for i, arg in enumerate(args):
if skip_next:
skip_next = False # Reset flag and skip this iteration
continue

if arg.startswith("-") and not arg.startswith("-var"):
if i + 1 < len(args) and not args[i + 1].startswith("-"):
# Detected '-key value' pair; append them without merging
new_args.append(arg)
new_args.append(args[i + 1])
skip_next = True # Mark to skip the next item as it's already processed
logger.debug(f"Detected '-key value' pair: {arg}, {args[i + 1]}")
else:
# Either '-key=value' or a standalone '-key'; just append
new_args.append(arg)
logger.debug(f"Appending standard -key=value or standalone argument: {arg}")
else:
# Handles '-var' and non '-' starting arguments
new_args.append(arg)
logger.debug(f"Appending argument (non '-' or '-var'): {arg}")

return new_args


@pass_container
def _apply(tf, args):
"""Build or change the infrastructure in this layer."""
# if there is a plan, remove all "-var" from the default args
tf_default_args = tf.tf_default_args
for arg in args:
if not arg.startswith("-"):
tf_default_args = [arg for index, arg in enumerate(tf_default_args) if not arg.startswith("-var")]
break
exit_code = tf.start_in_layer("apply", *tf_default_args, *args)
logger.debug(f"Original tf_default_args: {tf.tf_default_args}")

# Preserve the original `-var` removal logic and modify tf_default_args if necessary
tf_default_args = [arg for arg in tf.tf_default_args if not arg.startswith("-var") or arg in args]

# Process arguments using the new parsing logic
processed_args = handle_apply_arguments_parsing(args)

# Merge processed_args with tf_default_args, ensuring '-var' logic is preserved
final_args = tf_default_args + processed_args
logger.debug(f"Final argument list for execution: {final_args}")

# Execute the command with the modified arguments list
exit_code = tf.start_in_layer("apply", *final_args)

if exit_code:
logger.error(f"Command execution failed with exit code: {exit_code}")
raise Exit(exit_code)


Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from leverage.modules.terraform import handle_apply_arguments_parsing


class TestHandleApplyArgumentsParsing:
@pytest.mark.parametrize(
"input_args,expected_output",
[
# Test case: Single '-key=value'
(["-target=kubernetes_manifest.irfq"], ["-target=kubernetes_manifest.irfq"]),
# Test case: '-key value'
(["-target", "kubernetes_manifest.irfq"], ["-target", "kubernetes_manifest.irfq"]),
# Test case: Multiple mixed arguments
(
["-target", "kubernetes_manifest.irfq", "-lock=false"],
["-target", "kubernetes_manifest.irfq", "-lock=false"],
),
# Test case: '-var' arguments should be included as is
(["-var", "name=value"], ["-var", "name=value"]),
# Test case: Non-flag argument
(["some_value"], ["some_value"]),
# Test case: Mixed '-key=value' and '-key value' with '-var'
(
["-var", "name=value", "-target", "kubernetes_manifest.irfq", "-lock=false"],
["-var", "name=value", "-target", "kubernetes_manifest.irfq", "-lock=false"],
),
# Test case: No arguments
([], []),
# Test case: '-key=value' format with '-var'
(["-var", "name=value", "-lock=false"], ["-var", "name=value", "-lock=false"]),
],
)
def test_handle_apply_arguments_parsing(self, input_args, expected_output):
assert handle_apply_arguments_parsing(input_args) == expected_output

0 comments on commit d4e1ceb

Please sign in to comment.