Skip to content

Commit

Permalink
Ignore terraform's -chdir switch
Browse files Browse the repository at this point in the history
Fixes #113.

Also render "brew.rb" as "brew" in the output, as that's what the user
typed.
  • Loading branch information
walles committed May 9, 2023
1 parent 78cab5e commit 74162d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
43 changes: 28 additions & 15 deletions px/px_commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,14 @@ def get_command(commandline: str) -> str:
"pip",
"pip3",
"rustup",
"terraform",
]:
return faillog(commandline, get_with_subcommand(commandline))

if command == "terraform":
return faillog(
commandline, get_with_subcommand(commandline, ignore_switches=["-chdir"])
)

if command == "node":
return faillog(
commandline,
Expand Down Expand Up @@ -274,19 +278,6 @@ def get_sudo_command(commandline: str) -> Optional[str]:
return "sudo " + get_command(without_sudo)


def get_with_subcommand(commandline: str) -> Optional[str]:
array = to_array(commandline)
command = os.path.basename(array[0])

if len(array) == 1:
return command

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

return f"{command} {array[1]}"


def prettify_fully_qualified_java_class(class_name: str) -> str:
split = class_name.split(".")
if len(split) == 1:
Expand Down Expand Up @@ -382,6 +373,27 @@ def get_java_command(commandline: str) -> Optional[str]:
return None


def get_with_subcommand(
commandline: str, ignore_switches: Optional[List[str]] = None
) -> Optional[str]:
array = to_array(commandline)

if ignore_switches is None:
ignore_switches = []
while len(array) > 1 and array[1].split("=")[0] in ignore_switches:
del array[1]

command = os.path.basename(array[0])
if len(array) == 1:
return command

if array[1].startswith("-"):
# Unknown option, help!
return command

return f"{command} {array[1]}"


def get_generic_script_command(
commandline: str, ignore_switches: Optional[List[str]] = None
) -> Optional[str]:
Expand All @@ -397,16 +409,17 @@ def get_generic_script_command(
if len(array) == 1:
return vm

script = os.path.basename(array[1])
if array[1].startswith("-"):
# Unknown option, help!
return None

script = os.path.basename(array[1])
if len(array) == 2:
# vm + script
return script
if script not in ["brew.rb", "yarn.js"]:
return script
script = os.path.splitext(script)[0]

subcommand = array[2]
if subcommand.startswith("-"):
Expand Down
14 changes: 11 additions & 3 deletions tests/px_commandline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def test_get_command_ruby_switches():
px_commandline.get_command(
"/usr/bin/ruby -W0 /usr/local/bin/brew.rb install rust"
)
== "brew.rb install"
== "brew install"
)

# https://github.com/walles/px/issues/87
Expand Down Expand Up @@ -394,11 +394,11 @@ def test_get_homebrew_commandline():
]
)
)
== "brew.rb upgrade"
== "brew upgrade"
)


def test_get_terraform_commandline():
def test_get_terraform_provider_commandline():
# Source: https://github.com/walles/px/issues/105
assert (
px_commandline.get_command(
Expand All @@ -408,6 +408,14 @@ def test_get_terraform_commandline():
)


def test_get_terraform_commandline():
# Source: https://github.com/walles/px/issues/113
assert (
px_commandline.get_command("terraform -chdir=dev apply -target=abc123")
== "terraform apply"
)


def test_get_go_commandline():
assert px_commandline.get_command("go build ./...") == "go build"
assert px_commandline.get_command("go --version") == "go"
Expand Down

0 comments on commit 74162d3

Please sign in to comment.