Skip to content

Commit

Permalink
Remove prefix from --drall-verbose and --drall-debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jigarius committed Dec 27, 2024
1 parent faddffb commit 0ac7d82
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 154 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ drall exec --drall-filter="leo||ralph" core:status
For more on using filter expressions, refer to the documentation on
[consolidation/filter-via-dot-access-data](https://github.com/consolidation/filter-via-dot-access-data).

### --drall-verbose
### --verbose

Whether Drall should display verbose output.
Display verbose output.

### --drall-debug
### --debug

Whether Drall should display debugging output.
Display debug-level output.

## Auto-detect sites

Expand Down
79 changes: 30 additions & 49 deletions src/Command/ExecCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Drall\Drall;
use Drall\Model\EnvironmentId;
use Drall\Model\Placeholder;
use Drall\Model\RawCommand;
use Drall\Trait\SignalAwareTrait;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -91,41 +90,10 @@ protected function configure() {
$this->ignoreValidationErrors();
}

/**
* Sets an array to be treated as $argv, mostly for testing.
*
* The $argv array contains:
* - Script name as the first parameter, i.e. drall.
* - The Drall command as the second parameter, e.g. exec.
* - Options for the Drall command, e.g. --drall-group=bluish.
* - The Drush command and its arguments, e.g. pmu devel
* - Options for the Drush command, e.g. --fields=site.
*
* @code
* $command->setArgv([
* '/opt/drall/bin/drall',
* 'exec',
* '--drall-group=bluish',
* 'core:status',
* '--fields=site',
* ]);
* @endcode
*
* @param array $argv
* An array matching the $argv array format.
*
* @return self
* The command.
*/
public function setArgv(array $argv): self {
$this->argv = $argv;
return $this;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$this->preExecute($input, $output);

$command = $this->getCommand();
$command = $this->getCommand($input);
$group = $this->getDrallGroup($input);
$filter = $this->getDrallFilter($input);

Expand Down Expand Up @@ -235,22 +203,35 @@ function ($value) use ($command, $placeholder, $output, $progressBar, &$exitCode
return $exitCode;
}

protected function getCommand(): RawCommand {
// Symfony Console only recognizes options that are defined in the
// ::configure() method. Since our goal is to catch all arguments and
// options and send them to drush, we do it ourselves using $argv.
//
// @todo Is there a way to catch all options from $input?
$command = RawCommand::fromArgv($this->argv);

if (!str_contains($command, 'drush')) {
return $command;
}
/**
* Extracts the command to be executed by Drall.
*
* All drall-specific components are removed from the command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* Console input.
*
* @return string
* The command without Drall elements.
*
* @example
* Input: /path/to/drall exec --verbose -- drush st --fields=site
* Output: drush st --fields=site
*/
protected function getCommand(InputInterface $input): string {
// @todo Force -- for clarity if options are present.
// @todo Throw an error if --drall-* options are present.
// Everything after the first "--" is treated as an argument. All such
// arguments are treated as parts of the command to be executed.
$command = implode(' ', $input->getArguments()['cmd']);
$this->logger->debug("Command: {command}", ['command' => $command]);

// Inject --uri=@@dir for Drush commands without placeholders.
if (!Placeholder::search($command)) {
$sCommand = preg_replace('/\b(drush) /', 'drush --uri=@@dir ', $command, -1);
$command = new RawCommand($sCommand);
if (
str_contains($command, 'drush') &&
!Placeholder::search($command)
) {
// Inject --uri=@@dir for Drush commands without placeholders.
$command = preg_replace('/\b(drush) /', 'drush --uri=@@dir ', $command, -1);
$this->logger->debug('Injected --uri parameter for Drush command.');
}

Expand Down Expand Up @@ -284,7 +265,7 @@ protected function getWorkerCount(InputInterface $input): int {
/**
* Get unique placeholder from a command.
*/
private function getUniquePlaceholder(RawCommand $command): ?Placeholder {
private function getUniquePlaceholder(string $command): ?Placeholder {
if (!$placeholders = Placeholder::search($command)) {
$this->logger->error('The command contains no placeholders. Please run it directly without Drall.');
return NULL;
Expand Down
26 changes: 15 additions & 11 deletions src/Drall.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,38 @@ public function __construct() {
protected function configureIO(InputInterface $input, OutputInterface $output): void {
parent::configureIO($input, $output);

if ($input->hasParameterOption('--drall-debug', TRUE)) {
if ($input->hasParameterOption('--debug', TRUE)) {
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
}
elseif ($input->hasParameterOption('--drall-verbose', TRUE)) {
elseif ($input->hasParameterOption('--verbose', TRUE)) {
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
}
else {
$output->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
}

// The parent::configureIO sets verbosity in a SHELL_VERBOSITY. This causes
// other Symfony Console apps to become verbose, for example, Drush. To
// prevent such behavior, we force the SHELL_VERBOSITY to be normal.
$shellVerbosity = 0;
if (\function_exists('putenv')) {
@putenv("SHELL_VERBOSITY=$shellVerbosity");
}
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;
}

protected function getDefaultInputDefinition(): InputDefinition {
$definition = parent::getDefaultInputDefinition();

// Remove unneeded options.
$options = $definition->getOptions();
unset($options['verbose'], $options['quiet']);
unset($options['quiet']);
$definition->setOptions($options);

$definition->addOption(new InputOption(
'drall-verbose',
NULL,
InputOption::VALUE_NONE,
'Display verbose output for Drall.'
));
$definition->addOption(new InputOption(
'drall-debug',
NULL,
'debug',
'd',
InputOption::VALUE_NONE,
'Display debugging output for Drall.'
));
Expand Down
39 changes: 0 additions & 39 deletions src/Model/RawCommand.php

This file was deleted.

Loading

0 comments on commit 0ac7d82

Please sign in to comment.