Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba58c00

Browse files
authored
Fix ##1371. Move drush_program_exists() to ExecTrait (#3801)
* Move drush_program_exists() to ExecTrait Use it in sql:* and config:export * CS. * Use string version of command - it avoids an inexplicable test fail.
1 parent 574c3e1 commit ba58c00

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

includes/filesystem.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ function drush_mkdir($path) {
212212
* Determine if program exists on user's PATH.
213213
*
214214
* @return bool|null
215+
*
216+
* @deprecated
217+
* See \Drush\Exec\ExecTrait::programExists.
215218
*/
216219
function drush_program_exists($program) {
217220
if (drush_has_bash()) {
@@ -220,7 +223,7 @@ function drush_program_exists($program) {
220223
// Remove environment variables (eg. PGPASSFILE=) before testing program.
221224
$program = preg_replace('#^([A-Z0-9]+=.+? )+#', '', $program);
222225

223-
// Dont't use drush_op_system() since we don't want output during tests.
226+
// Don't use drush_op_system() since we don't want output during tests.
224227
system("command -v $program > $bucket 2>&1", $result_code);
225228
return $result_code === 0 ? TRUE : FALSE;
226229
}

src/Commands/sql/SqlCommands.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<?php
22
namespace Drush\Commands\sql;
33

4+
use Consolidation\AnnotatedCommand\CommandData;
45
use Drupal\Core\Database\Database;
56
use Drush\Commands\DrushCommands;
67
use Drush\Drush;
78
use Drush\Exceptions\UserAbortException;
9+
use Drush\Exec\ExecTrait;
810
use Drush\Sql\SqlBase;
911
use Consolidation\OutputFormatters\StructuredData\PropertyList;
1012

1113
class SqlCommands extends DrushCommands
1214
{
15+
use ExecTrait;
1316

1417
/**
15-
* Print database connection details using print_r().
18+
* Print database connection details.
1619
*
1720
* @command sql:conf
1821
* @aliases sql-conf
@@ -229,4 +232,32 @@ public function dump($options = ['result-file' => self::REQ, 'create-db' => fals
229232
}
230233
return new PropertyList(['path' => $return]);
231234
}
235+
236+
/**
237+
* Assert that `mysql` or similar are on the user's PATH.
238+
*
239+
* @hook validate
240+
* @param CommandData $commandData
241+
* @return bool
242+
* @throws \Exception
243+
*/
244+
public function validate(CommandData $commandData)
245+
{
246+
if (in_array($commandData->annotationData()->get('command'), ['sql:connect', 'sql:conf'])) {
247+
// These commands don't require a program.
248+
return;
249+
}
250+
251+
$sql = SqlBase::create($commandData->options());
252+
$program = $sql->command();
253+
254+
// Remove environment variables (eg. PGPASSFILE=) before testing program.
255+
// @todo Remove once postgres is passing env variables via Process.
256+
$program = preg_replace('#^([A-Z0-9]+=.+? )+#', '', $program);
257+
258+
if (!$this->programExists($program)) {
259+
$this->logger->warning(dt('The shell command \'!command\' is required but cannot be found. Please install it and retry.', ['!command' => $program]));
260+
return false;
261+
}
262+
}
232263
}

src/Drupal/Commands/config/ConfigCommands.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\Core\Config\StorageInterface;
1212
use Drush\Commands\DrushCommands;
1313
use Drush\Drush;
14+
use Drush\Exec\ExecTrait;
1415
use Drush\Utils\FsUtils;
1516
use Symfony\Component\Console\Helper\Table;
1617
use Symfony\Component\Console\Input\InputInterface;
@@ -21,6 +22,7 @@
2122

2223
class ConfigCommands extends DrushCommands
2324
{
25+
use ExecTrait;
2426

2527
/**
2628
* @var ConfigFactoryInterface
@@ -507,12 +509,13 @@ public static function getDiff(StorageInterface $destination_storage, StorageInt
507509
$temp_source_storage = new FileStorage($temp_source_dir);
508510
self::copyConfig($source_storage, $temp_source_storage);
509511

510-
$prefix = 'diff';
511-
if (drush_program_exists('git') && $output->isDecorated()) {
512-
$prefix = 'git diff --color=always';
512+
$prefix = ['diff'];
513+
if (self::programExists('git') && $output->isDecorated()) {
514+
$prefix = ['git', 'diff', '--color=always'];
513515
}
514-
$args = [$prefix, '-u', $temp_destination_dir, $temp_source_dir];
516+
$args = array_merge($prefix, ['-u', $temp_destination_dir, $temp_source_dir]);
515517
$process = Drush::process($args);
516-
return drush_shell_exec_output();
518+
$process->run();
519+
return $process->getOutput();
517520
}
518521
}

src/Drupal/Commands/config/ConfigImportCommands.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Drupal\Core\Lock\LockBackendInterface;
1818
use Drupal\Core\StringTranslation\TranslationInterface;
1919
use Drush\Commands\DrushCommands;
20+
use Drush\Exceptions\UserAbortException;
2021
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2122
use Webmozart\PathUtil\Path;
2223

@@ -201,12 +202,13 @@ public function import($label = null, $options = ['preview' => 'list', 'source'
201202
} else {
202203
$output = ConfigCommands::getDiff($active_storage, $source_storage, $this->output());
203204

204-
$this->output()->writeln(implode("\n", $output));
205+
$this->output()->writeln($output);
205206
}
206207

207-
if ($this->io()->confirm(dt('Import the listed configuration changes?'))) {
208-
return drush_op([$this, 'doImport'], $storage_comparer);
208+
if (!$this->io()->confirm(dt('Import the listed configuration changes?'))) {
209+
throw new UserAbortException();
209210
}
211+
return drush_op([$this, 'doImport'], $storage_comparer);
210212
}
211213

212214
// Copied from submitForm() at /core/modules/config/src/Form/ConfigSync.php

src/Exec/ExecTrait.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function startBrowser($uri = null, $sleep = false, $port = false, $browse
6262
}
6363

6464
if ($browser) {
65-
drush_log(dt('Opening browser !browser at !uri', ['!browser' => $browser, '!uri' => $uri]), LogLevel::INFO);
65+
$this->logger()->info(dt('Opening browser !browser at !uri', ['!browser' => $browser, '!uri' => $uri]));
6666
$args = [];
6767
if (!Drush::simulate()) {
6868
if ($sleep) {
@@ -78,4 +78,17 @@ public function startBrowser($uri = null, $sleep = false, $port = false, $browse
7878
}
7979
return false;
8080
}
81+
82+
/*
83+
* Determine if program exists on user's PATH.
84+
*
85+
* @return bool
86+
* True if program exists on PATH.
87+
*/
88+
public static function programExists($program)
89+
{
90+
$process = Drush::process("command -v $program");
91+
$process->run();
92+
return $process->isSuccessful();
93+
}
8194
}

0 commit comments

Comments
 (0)