def do(
ctx: click.Context,
cmd: typing.List[str],
cwd: str = '.',
mute_stdout: bool = False,
mute_stderr: bool = False,
# @formatter:off
filter_output: typing.Union[None, typing.Iterable[str]]=None
# @formatter:on
) -> str:
"""
Executes a command and returns the result
Args:
ctx: click context
cmd: command to execute
cwd: working directory (defaults to ".")
mute_stdout: if true, stdout will not be printed
mute_stderr: if true, stderr will not be printed
filter_output: gives a list of partial strings to filter out from the output (stdout or stderr)
Returns: stdout
"""
def _filter_output(input_):
def _filter_line(line):
# noinspection PyTypeChecker
for filter_str in filter_output:
if filter_str in line:
return False
return True
if filter_output is None:
return input_
return '\n'.join(filter(_filter_line, input_.split('\n')))
if not isinstance(cmd, (list, tuple)):
cmd = shlex.split(cmd)
out, err, ret = do_ex(ctx, cmd, cwd)
if out and not mute_stdout:
click.secho(f'{_filter_output(out)}', fg='cyan')
if err and not mute_stderr:
click.secho(f'{_filter_output(err)}', fg='red')
if ret:
click.secho(f'command failed: {cmd}', err=True, fg='red')
exit(ret)
return out
评论列表
文章目录