Skip to content

Commit

Permalink
Create option: --no-buffer; refs #57
Browse files Browse the repository at this point in the history
  • Loading branch information
jigarius committed Dec 29, 2024
1 parent 181ee73 commit c50321c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Command/ExecCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drall\Command;

use Amp\ByteStream;
use Amp\ByteStream\WritableResourceStream;
use Amp\Pipeline\Pipeline;
use Amp\Process\Process;
use Drall\Model\Placeholder;
Expand All @@ -14,6 +15,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

/**
* A command to execute a shell command on multiple sites.
Expand Down Expand Up @@ -82,6 +84,13 @@ protected function configure() {
'Do not execute commands, only display them.'
);

$this->addOption(
'no-buffer',
'B',
InputOption::VALUE_NONE,
'Do not buffer output.'
);

$this->addOption(
'no-progress',
'P',
Expand Down Expand Up @@ -190,9 +199,14 @@ protected function preExecute(InputInterface $input, OutputInterface $output): v
if ($interval = $input->getOption('interval')) {
$this->logger->notice("Using a $interval-second interval between commands.", ['interval' => $interval]);
}

if ($input->getOption('no-buffer')) {
$this->logger->notice("Using no output buffering.");
}
}

protected function execute(InputInterface $input, OutputInterface $output): int {
/** @var \Symfony\Component\Console\Output\ConsoleOutput $output */
$this->preExecute($input, $output);

if (!$command = $this->getCommand($input, $output)) {
Expand Down Expand Up @@ -231,8 +245,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

// After this point, all output must go through the output sections.
// This keeps the text at the top and the progress bar at the bottom.
$textSection = $output->section();
$progressBar = new ProgressBar(
$input->getOption('no-progress') ? new NullOutput() : $output->section(),
Expand All @@ -241,6 +253,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$exitCode = Command::SUCCESS;

// Within the iteration, all output must go through the output sections.
// This keeps the text at the top and the progress bar at the bottom.
Pipeline::fromIterable($values)
->concurrent($input->getOption('workers'))
->unordered()
Expand All @@ -260,8 +274,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$pCommand = Placeholder::replace([$placeholder->value => $value], $command);
$process = Process::start("($pCommand) 2>&1");

$pOutput = rtrim(ByteStream\buffer($process->getStdout()));
if ($pOutput) {
// Send process output directly to the output stream.
if (
$input->getOption('no-buffer') &&
is_a($output, StreamOutput::class)
) {
$wStream = new WritableResourceStream($output->getStream());
ByteStream\pipe($process->getStdout(), $wStream);
}
// Buffer process output until it finishes.
elseif ($pOutput = rtrim(ByteStream\buffer($process->getStdout()))) {
// Always display command output, even in --quiet mode.
$textSection->writeln($pOutput, OutputInterface::VERBOSITY_QUIET);
}
Expand Down
15 changes: 15 additions & 0 deletions test/Integration/Command/ExecCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,21 @@ public function testWithProgressBar(): void {
EOF, $process->getOutput());
}

/**
* @testdox With no buffer.
*/
public function testWithNoBuffer(): void {
$process = Process::fromShellCommandline(
'drall exec --no-progress --no-buffer --verbose -- ./vendor/bin/drush st --field=site 2>&1',
static::PATH_DRUPAL,
);
$process->run();
$this->assertStringStartsWith(
'[notice] Using no output buffering.' . PHP_EOL,
$process->getOutput(),
);
}

/**
* @testdox With verbosity quiet.
*/
Expand Down

0 comments on commit c50321c

Please sign in to comment.