Skip to content

Allow a list of strings for commands #1030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions invoke/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Optional,
Tuple,
Type,
Union,
)

# Import some platform-specific things at top level so they can be mocked for
Expand Down Expand Up @@ -122,7 +123,9 @@ def __init__(self, context: "Context") -> None:
self._asynchronous = False
self._disowned = False

def run(self, command: str, **kwargs: Any) -> Optional["Result"]:
def run(
self, command: Union[str, List[str]], **kwargs: Any
) -> Optional["Result"]:
"""
Execute ``command``, returning an instance of `Result` once complete.

Expand All @@ -144,7 +147,7 @@ def run(self, command: str, **kwargs: Any) -> Optional["Result"]:
the ``echo`` keyword, etc). The base default values are described
in the parameter list below.

:param str command: The shell command to execute.
:param Union[str, List[str]] command: The shell command to execute.

:param bool asynchronous:
When set to ``True`` (default ``False``), enables asynchronous
Expand Down Expand Up @@ -397,10 +400,13 @@ def run(self, command: str, **kwargs: Any) -> Optional["Result"]:
if not (self._asynchronous or self._disowned):
self.stop()

def echo(self, command: str) -> None:
print(self.opts["echo_format"].format(command=command))
def echo(self, command: Union[str, List[str]]) -> None:
command_string = (
command if isinstance(command, str) else " ".join(command)
)
print(self.opts["echo_format"].format(command=command_string))

def _setup(self, command: str, kwargs: Any) -> None:
def _setup(self, command: Union[str, List[str]], kwargs: Any) -> None:
"""
Prepare data on ``self`` so we're ready to start running.
"""
Expand Down Expand Up @@ -428,7 +434,9 @@ def _setup(self, command: str, kwargs: Any) -> None:
encoding=self.encoding,
)

def _run_body(self, command: str, **kwargs: Any) -> Optional["Result"]:
def _run_body(
self, command: Union[str, List[str]], **kwargs: Any
) -> Optional["Result"]:
# Prepare all the bits n bobs.
self._setup(command, kwargs)
# If dry-run, stop here.
Expand Down Expand Up @@ -1043,7 +1051,9 @@ def process_is_finished(self) -> bool:
"""
raise NotImplementedError

def start(self, command: str, shell: str, env: Dict[str, Any]) -> None:
def start(
self, command: Union[str, List[str]], shell: str, env: Dict[str, Any]
) -> None:
"""
Initiate execution of ``command`` (via ``shell``, with ``env``).

Expand Down Expand Up @@ -1305,7 +1315,9 @@ def close_proc_stdin(self) -> None:
"Unable to close missing subprocess or stdin!"
)

def start(self, command: str, shell: str, env: Dict[str, Any]) -> None:
def start(
self, command: Union[str, List[str]], shell: str, env: Dict[str, Any]
) -> None:
if self.using_pty:
if pty is None: # Encountered ImportError
err = "You indicated pty=True, but your platform doesn't support the 'pty' module!" # noqa
Expand All @@ -1332,7 +1344,10 @@ def start(self, command: str, shell: str, env: Dict[str, Any]) -> None:
# for now.
# NOTE: stdlib subprocess (actually its posix flavor, which is
# written in C) uses either execve or execv, depending.
os.execve(shell, [shell, "-c", command], env)
command_parts = (
[command] if isinstance(command, str) else command
)
os.execve(shell, [shell, "-c", *command_parts], env)
else:
self.process = Popen(
command,
Expand Down