Skip to content

Commit 7f27af8

Browse files
committed
process: enable stream_command to filter by pattern
1 parent 7bc5ba7 commit 7f27af8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/warnet/process.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
import re
23

34

45
def run_command(command: str) -> str:
@@ -8,7 +9,8 @@ def run_command(command: str) -> str:
89
return result.stdout
910

1011

11-
def stream_command(command: str) -> bool:
12+
def stream_command(command: str, grep_pattern: str = "") -> bool:
13+
"""Stream output and apply an optional pattern filter."""
1214
process = subprocess.Popen(
1315
["bash", "-c", command],
1416
stdout=subprocess.PIPE,
@@ -18,10 +20,15 @@ def stream_command(command: str) -> bool:
1820
universal_newlines=True,
1921
)
2022

21-
message = ""
23+
pattern = re.compile(grep_pattern) if grep_pattern else None
24+
25+
# Only display lines matching the pattern if grep is specified
2226
for line in iter(process.stdout.readline, ""):
23-
message += line
24-
print(line, end="")
27+
if pattern:
28+
if pattern.search(line):
29+
print(line, end="")
30+
else:
31+
print(line, end="")
2532

2633
process.stdout.close()
2734
return_code = process.wait()

0 commit comments

Comments
 (0)