Skip to content

Commit

Permalink
Merge pull request #340 from shunichironomura/add-shell-param
Browse files Browse the repository at this point in the history
Add `shell` parameter to `CommandContext`
  • Loading branch information
shunichironomura authored Sep 23, 2024
2 parents 3c37b38 + a9eb460 commit 3676f8e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions capsula/_context/_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def builder(
"It is recommended to set this to True in the configuration file.",
),
] = False,
shell: Annotated[
bool,
Doc(
"Whether to run the command using the shell. If True, the command will be run using the shell. "
"If False, the command will be run directly. "
"For more information, see the `shell` argument of `subprocess.run`. ",
),
] = True,
) -> Callable[[CapsuleParams], CommandContext]:
def build(params: CapsuleParams) -> CommandContext:
if cwd_relative_to_project_root and cwd is not None and not Path(cwd).is_absolute():
Expand All @@ -70,6 +78,7 @@ def build(params: CapsuleParams) -> CommandContext:
cwd=cwd_path,
check=check,
abort_on_error=abort_on_error,
shell=shell,
)

return build
Expand All @@ -81,22 +90,24 @@ def __init__(
cwd: Path | None = None,
check: bool = True,
abort_on_error: bool = True,
shell: bool = True,
) -> None:
"""Initialize the command context."""
self._command = command
self._cwd = cwd
self._check = check
self._abort_on_error = abort_on_error
self._shell = shell

@property
def abort_on_error(self) -> bool:
return self._abort_on_error

def encapsulate(self) -> _CommandContextData:
logger.debug(f"Running command: {self._command}")
output = subprocess.run( # noqa: S602
output = subprocess.run( # noqa: S603
self._command,
shell=True,
shell=self._shell,
text=True,
capture_output=True,
cwd=self._cwd,
Expand Down

0 comments on commit 3676f8e

Please sign in to comment.