Check if a command matches a pattern. Supports: - Exact match: "swift" - Prefix wildcard: "swift*" matches "swift", "swiftc", "swiftformat" - Local script paths: "./scripts/build.sh" or "scripts/test.sh" Args: command: The command to check pattern: The patt
(command: str, pattern: str)
| 439 | |
| 440 | |
| 441 | def matches_pattern(command: str, pattern: str) -> bool: |
| 442 | """ |
| 443 | Check if a command matches a pattern. |
| 444 | |
| 445 | Supports: |
| 446 | - Exact match: "swift" |
| 447 | - Prefix wildcard: "swift*" matches "swift", "swiftc", "swiftformat" |
| 448 | - Local script paths: "./scripts/build.sh" or "scripts/test.sh" |
| 449 | |
| 450 | Args: |
| 451 | command: The command to check |
| 452 | pattern: The pattern to match against |
| 453 | |
| 454 | Returns: |
| 455 | True if command matches pattern |
| 456 | """ |
| 457 | # Reject bare wildcards - security measure to prevent matching everything |
| 458 | if pattern == "*": |
| 459 | return False |
| 460 | |
| 461 | # Exact match |
| 462 | if command == pattern: |
| 463 | return True |
| 464 | |
| 465 | # Prefix wildcard (e.g., "swift*" matches "swiftc", "swiftlint") |
| 466 | if pattern.endswith("*"): |
| 467 | prefix = pattern[:-1] |
| 468 | # Also reject if prefix is empty (would be bare "*") |
| 469 | if not prefix: |
| 470 | return False |
| 471 | return command.startswith(prefix) |
| 472 | |
| 473 | # Path patterns (./scripts/build.sh, scripts/test.sh, etc.) |
| 474 | if "/" in pattern: |
| 475 | # Extract the script name from the pattern |
| 476 | pattern_name = os.path.basename(pattern) |
| 477 | return command == pattern or command == pattern_name or command.endswith("/" + pattern_name) |
| 478 | |
| 479 | return False |
| 480 | |
| 481 | |
| 482 | def _validate_command_list(commands: list, config_path: Path, field_name: str) -> bool: |
no outgoing calls