Skip to content

Commit 8600263

Browse files
committed
Command line pattern detection for docker run is now more robust
1 parent 662bae0 commit 8600263

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

treecript/parser.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@
4848

4949
logger = logging.getLogger(__name__)
5050

51+
SINGLE_DOCKER_FLAGS = {
52+
"-d",
53+
"--detach",
54+
"--init",
55+
"-i",
56+
"--interactive",
57+
"--no-healthcheck",
58+
"--oom-kill-disable",
59+
"--privileged",
60+
"-P",
61+
"--publish-all",
62+
"-q",
63+
"--quiet",
64+
"--read-only",
65+
"--rm",
66+
"--sig-proxy",
67+
"-t",
68+
"--tty",
69+
"--use-api-socket",
70+
# A couple of common combinations
71+
"-ti",
72+
"-it",
73+
}
74+
5175

5276
def process_command(command: "Sequence[str]") -> "str":
5377
basename_command = os.path.basename(command[0])
@@ -65,13 +89,23 @@ def process_command(command: "Sequence[str]") -> "str":
6589
retlabel += "\n" + os.path.basename(token)
6690
break
6791
elif basename_command == "docker":
92+
is_docker_run = False
93+
prev_flag = False
6894
for token in command[1:]:
69-
if token == "stats":
95+
if is_docker_run:
96+
if token.startswith("-"):
97+
prev_flag = token not in SINGLE_DOCKER_FLAGS
98+
elif prev_flag:
99+
prev_flag = False
100+
else:
101+
retlabel += "\n" + token
102+
break
103+
elif token == "stats":
70104
retlabel = "docker stats"
71105
break
72-
elif not token.startswith("-") and token != "run":
73-
retlabel = "docker run\n" + token
74-
break
106+
elif token == "run":
107+
retlabel = "docker run"
108+
is_docker_run = True
75109

76110
return retlabel
77111

0 commit comments

Comments
 (0)