Skip to content

Commit

Permalink
Parse "aws" command lines
Browse files Browse the repository at this point in the history
Fixes #114.
  • Loading branch information
walles committed Nov 4, 2023
1 parent 6d9e219 commit 8a8b9b9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
36 changes: 32 additions & 4 deletions px/px_commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,42 @@ def get_python_command(commandline: str) -> Optional[str]:
if len(array) == 1:
return python

if not array[1].startswith("-"):
return os.path.basename(array[1])

if len(array) > 2:
if array[1] == "-m" and not array[2].startswith("-"):
return os.path.basename(array[2])

return None
if array[1].startswith("-"):
return None

if os.path.basename(array[1]) == "aws":
# Drop the "python" part of "python aws"
return get_aws_command(array[1:])

return os.path.basename(array[1])


def get_aws_command(args: List[str]) -> Optional[str]:
'''Extract "aws command subcommand" from a command line starting with "aws"'''
result = ["aws"]
for arg in args[1:]:
if arg.startswith("--profile="):
continue
if arg.startswith("--region="):
continue
if arg.startswith("-"):
break
if os.path.sep in arg:
break

result.append(arg)
if len(result) >= 4:
# Got "aws command subcommand"
break

if len(result) == 4 and result[-1] != "help":
del result[-1]

return " ".join(result)


def get_sudo_command(commandline: str) -> Optional[str]:
Expand Down
30 changes: 30 additions & 0 deletions tests/px_commandline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ def test_get_command_python():
assert px_commandline.get_command("python ") == "python"


def test_get_command_aws():
assert px_commandline.get_command("Python /usr/local/bin/aws") == "aws"
assert px_commandline.get_command("python aws s3") == "aws s3"
assert px_commandline.get_command("python3 aws s3 help") == "aws s3 help"
assert (
px_commandline.get_command("/wherever/python3 aws s3 help flaska")
== "aws s3 help"
)
assert px_commandline.get_command("python aws s3 sync help") == "aws s3 sync help"
assert px_commandline.get_command("python aws s3 sync nothelp") == "aws s3 sync"
assert (
px_commandline.get_command(
" ".join(
[
"python3",
"/usr/local/bin/aws",
"--profile=system-admin-prod",
"--region=eu-west-1",
"s3",
"sync",
"--only-show-errors",
"s3://xxxxxx",
"./xxxxxx",
]
)
)
== "aws s3 sync"
)


def test_get_command_java():
assert px_commandline.get_command("java") == "java"
assert px_commandline.get_command("java -version") == "java"
Expand Down

0 comments on commit 8a8b9b9

Please sign in to comment.