Skip to content

Commit

Permalink
Merge pull request #84 from jigarius/feature/site-keys-command
Browse files Browse the repository at this point in the history
Create command: site:keys
  • Loading branch information
jigarius authored Oct 24, 2023
2 parents 0879344 + 3fddb41 commit 6c1e05f
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .docker/main/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"composer/installers": true,
"drupal/core-composer-scaffold": true,
"drupal/core-project-message": true,
"drupal/core-vendor-hardening": true
"drupal/core-vendor-hardening": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,32 @@ do
done;
```

### site:keys

Get a list of all keys in `$sites`. Usually, these are site URIs.

#### Example: Usage

```shell
$ drall site:keys
tmnt.com
donatello.com
leonardo.com
michelangelo.com
raphael.com
```

The output can then be iterated with scripts.

#### Example: Iterating

```shell
for site in $(drall site:keys)
do
echo "Current site: $site";
done;
```

### site:aliases

Get a list of site aliases.
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
},
"sort-packages": true
},
"extra": {
"phpcodesniffer-search-depth": 5
},
"scripts": {
"lint": "composer exec phpcs",
"test": "XDEBUG_MODE=coverage composer exec phpunit"
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Drall\Trait\SiteDetectorAwareTrait;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

abstract class BaseCommand extends Command {
Expand Down
5 changes: 3 additions & 2 deletions src/Command/ExecCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ protected function configure() {
$this->setAliases(['ex']);
$this->setDescription('Execute a command on multiple Drupal sites.');
$this->addUsage('drush core:status');
$this->addUsage('--drall-group=bluish drush core:status');
$this->addUsage('--drall-workers=4 drush cache:rebuild');
$this->addUsage('./vendor/bin/drush core:status');
$this->addUsage('--drall-group=GROUP drush core:status');
$this->addUsage('--drall-filter=FILTER drush core:status');
$this->addUsage('--drall-workers=4 drush cache:rebuild');
$this->addUsage('ls web/sites/@@dir/settings.php');
$this->addUsage('echo "Working on @@site" && drush @@site.local core:status');

Expand Down
3 changes: 2 additions & 1 deletion src/Command/SiteAliasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ protected function configure() {

$this->setName('site:aliases');
$this->setAliases(['sa']);
$this->setDescription('Get a list of site aliases.');
$this->setDescription('List all Drush site aliases.');
$this->addUsage('site:aliases');
$this->addUsage('--drall-group=GROUP site:aliases');
$this->addUsage('--drall-filter=FILTER site:aliases');
}

protected function execute(InputInterface $input, OutputInterface $output) {
Expand Down
4 changes: 2 additions & 2 deletions src/Command/SiteDirectoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* A command to get a list of site directories in the Drupal installation.
* A command to get a list of values in the $sites array.
*/
class SiteDirectoriesCommand extends BaseCommand {

Expand All @@ -15,7 +15,7 @@ protected function configure() {

$this->setName('site:directories');
$this->setAliases(['sd']);
$this->setDescription('Get a list of site directories.');
$this->setDescription('List the values of the $sites array.');
$this->addUsage('site:directories');
$this->addUsage('--drall-group=GROUP site:directories');
}
Expand Down
45 changes: 45 additions & 0 deletions src/Command/SiteKeysCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drall\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* A command to get a list of keys in the $sites array.
*/
class SiteKeysCommand extends BaseCommand {

protected function configure() {
parent::configure();

$this->setName('site:keys');
$this->setAliases(['sk']);
$this->setDescription('List the keys of the $sites array.');
$this->addUsage('site:keys');
$this->addUsage('--drall-group=GROUP site:keys');
$this->addUsage('--drall-filter=FILTER site:keys');
}

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

$keys = $this->siteDetector()
->getSiteKeys(
$this->getDrallGroup($input),
$this->getDrallFilter($input),
);

if (count($keys) === 0) {
$this->logger->warning('No Drupal sites found.');
return 0;
}

foreach ($keys as $key) {
$output->writeln($key);
}

return 0;
}

}
9 changes: 7 additions & 2 deletions src/Drall.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Consolidation\SiteAlias\SiteAliasManager;
use Drall\Command\ExecCommand;
use Drall\Command\SiteDirectoriesCommand;
use Drall\Command\SiteAliasesCommand;
use Drall\Command\SiteDirectoriesCommand;
use Drall\Command\SiteKeysCommand;
use Drall\Model\EnvironmentId;
use Drall\Service\SiteDetector;
use Drall\Trait\SiteDetectorAwareTrait;
use Drush\SiteAlias\SiteAliasFileLoader;
use DrupalFinder\DrupalFinder;
use Drush\SiteAlias\SiteAliasFileLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\ArgvInput;
Expand Down Expand Up @@ -52,6 +53,10 @@ public function __construct(
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new SiteKeysCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new SiteAliasesCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);
Expand Down
2 changes: 1 addition & 1 deletion src/Service/SiteDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Consolidation\Filter\LogicalOpFactory;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Consolidation\SiteAlias\SiteAliasManagerInterface;
use Drall\Model\SitesFile;
use Drall\Trait\DrupalFinderAwareTrait;
use DrupalFinder\DrupalFinder;
use Drall\Model\SitesFile;

class SiteDetector {

Expand Down
2 changes: 1 addition & 1 deletion test/Integration/Command/SiteAliasesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testExecute(): void {
}

/**
* Run site:aliases with --filter.
* Run site:aliases with --drall-filter.
*/
public function testExecuteWithFilter(): void {
$output = shell_exec('drall site:aliases --drall-filter="leo||ralph"');
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/Command/SiteDirectoriesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testExecute(): void {
}

/**
* Run site:directories with --filter.
* Run site:directories with --drall-filter.
*/
public function testExecuteWithFilter(): void {
$output = shell_exec('drall site:directories --drall-filter="leo||ralph"');
Expand Down
128 changes: 128 additions & 0 deletions test/Integration/Command/SiteKeysCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Drall\Test\Integration\Commands;

use Drall\IntegrationTestCase;

/**
* @covers \Drall\Command\SiteDirectoriesCommand
*/
class SiteKeysCommandTest extends IntegrationTestCase {

/**
* Run site:keys with no Drupal installation.
*/
public function testWithNoDrupal(): void {
chdir('/tmp');
$output = shell_exec('drall site:keys');
$this->assertOutputEquals("[warning] No Drupal sites found." . PHP_EOL, $output);
}

/**
* Run site:keys with a Drupal installation.
*/
public function testExecute(): void {
$output = shell_exec('drall site:keys');
$this->assertOutputEquals(<<<EOF
tmnt.com
cowabunga.com
tmnt.drall.local
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
michelangelo.com
mikey.drall.local
raphael.com
ralph.drall.local
EOF, $output);
}

/**
* Run site:keys with --drall-filter.
*/
public function testExecuteWithFilter(): void {
$output = shell_exec('drall site:keys --drall-filter="[email protected]\$@"');
$this->assertOutputEquals(<<<EOF
tmnt.drall.local
donnie.drall.local
leo.drall.local
mikey.drall.local
ralph.drall.local
EOF, $output);
}

/**
* Run site:keys with --drall-group.
*/
public function testWithGroup(): void {
$output = shell_exec('drall site:keys --drall-group=bluish');
$this->assertOutputEquals(<<<EOF
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
EOF, $output);
}

/**
* Run site:keys with DRALL_GROUP env var.
*/
public function testWithGroupEnvVar(): void {
$output = shell_exec('DRALL_GROUP=bluish drall site:keys');
$this->assertOutputEquals(<<<EOF
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
EOF, $output);
}

public function testWithComposerRoot() {
chdir('/');
$output = shell_exec('drall --root=' . $this->drupalDir() . ' site:keys');
$this->assertEquals(<<<EOF
tmnt.com
cowabunga.com
tmnt.drall.local
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
michelangelo.com
mikey.drall.local
raphael.com
ralph.drall.local
EOF, $output);
}

public function testWithDrupalRoot() {
chdir('/');
$output = shell_exec('drall --root=' . $this->drupalDir() . '/web site:keys');
$this->assertEquals(<<<EOF
tmnt.com
cowabunga.com
tmnt.drall.local
donatello.com
8080.donatello.com
donnie.drall.local
leonardo.com
leo.drall.local
michelangelo.com
mikey.drall.local
raphael.com
ralph.drall.local
EOF, $output);
}

}
8 changes: 4 additions & 4 deletions test/Unit/Command/ExecCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

use Consolidation\SiteAlias\SiteAliasManager;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use DrupalFinder\DrupalFinder;
use Drall\Drall;
use Drall\Service\SiteDetector;
use Drall\TestCase;
use DrupalFinder\DrupalFinder;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @covers \Drall\Command\ExecCommand
Expand Down
6 changes: 3 additions & 3 deletions test/Unit/Command/SiteAliasesCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use Consolidation\SiteAlias\SiteAliasManager;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Tester\CommandTester;
use DrupalFinder\DrupalFinder;
use Drall\Drall;
use Drall\Service\SiteDetector;
use Drall\TestCase;
use DrupalFinder\DrupalFinder;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @covers \Drall\Command\BaseCommand
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Command/SiteDirectoriesCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

use Consolidation\SiteAlias\SiteAliasManager;
use Symfony\Component\Console\Tester\CommandTester;
use DrupalFinder\DrupalFinder;
use Drall\Drall;
use Drall\Service\SiteDetector;
use Drall\TestCase;
use DrupalFinder\DrupalFinder;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @covers \Drall\Command\BaseCommand
Expand Down
Loading

0 comments on commit 6c1e05f

Please sign in to comment.