Skip to content

Commit 00d3927

Browse files
committed
feat: wildcard supporrt
1 parent 6679ff5 commit 00d3927

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/tfblocks/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def is_resource_match(
7171
If both filter_addrs and file_addrs are provided, the resource must match both (intersection).
7272
If only one type of filter is provided, the resource must match that filter.
7373
If no filters are provided, all resources match.
74+
75+
Supports wildcards (*) in filter addresses for pattern matching.
7476
"""
7577

7678
def extract_resource_type_and_name(addr: str) -> tuple:
@@ -85,6 +87,14 @@ def extract_resource_type_and_name(addr: str) -> tuple:
8587

8688
def matches_address_list(addr: str, addr_list: List[str]) -> bool:
8789
for filter_addr in addr_list:
90+
# Check for wildcard pattern matching
91+
if "*" in filter_addr:
92+
# Convert wildcard pattern to regex pattern
93+
# Escape special regex chars except '*'
94+
pattern = "^" + re.escape(filter_addr).replace("\\*", ".*") + "$"
95+
if re.match(pattern, addr):
96+
return True
97+
8898
# Exact match
8999
if addr == filter_addr:
90100
return True

tests/test_main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ def test_resource_type_name_match(self):
6666
)
6767
)
6868

69+
def test_wildcard_matching(self):
70+
"""Test matching resources using wildcards in filter addresses"""
71+
# Match all resources of a specific type
72+
self.assertTrue(
73+
main.is_resource_match("aws_s3_bucket.test", ["aws_s3_bucket.*"], [])
74+
)
75+
self.assertTrue(
76+
main.is_resource_match("aws_s3_bucket.other", ["aws_s3_bucket.*"], [])
77+
)
78+
self.assertFalse(
79+
main.is_resource_match("aws_lambda_function.test", ["aws_s3_bucket.*"], [])
80+
)
81+
82+
# Match resources using wildcards in module paths
83+
self.assertTrue(
84+
main.is_resource_match("module.my_module.aws_s3_bucket.test", ["*.aws_s3_bucket.test"], [])
85+
)
86+
87+
# Match more complex patterns
88+
self.assertTrue(
89+
main.is_resource_match("module.my_module.aws_s3_bucket.test", ["module.*.aws_s3_bucket.*"], [])
90+
)
91+
self.assertFalse(
92+
main.is_resource_match("other.my_module.aws_s3_bucket.test", ["module.*.aws_s3_bucket.*"], [])
93+
)
94+
6995
def test_intersection_filter(self):
7096
"""Test that both address and file filters must match"""
7197
self.assertTrue(

0 commit comments

Comments
 (0)