From 01c8b8a8680ff2261cc9752e8d3ed21b3c7e35e1 Mon Sep 17 00:00:00 2001
From: Jigar Mehta <hello@jigarius.com>
Date: Mon, 28 Feb 2022 22:56:08 -0500
Subject: [PATCH] Support --drall-group with site:aliases command

---
 src/Commands/ExecCommand.php        |  1 -
 src/Commands/SiteAliasesCommand.php | 11 ++++++++++-
 src/Services/SiteDetector.php       | 29 +++++++++++++++++++----------
 3 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/Commands/ExecCommand.php b/src/Commands/ExecCommand.php
index 5d71485..acbaa50 100644
--- a/src/Commands/ExecCommand.php
+++ b/src/Commands/ExecCommand.php
@@ -28,7 +28,6 @@ protected function configure() {
       NULL,
       InputOption::VALUE_REQUIRED,
       'Site group identifier.'
-
     );
 
     $this->addUsage('core:status');
diff --git a/src/Commands/SiteAliasesCommand.php b/src/Commands/SiteAliasesCommand.php
index 370da23..de8b47e 100644
--- a/src/Commands/SiteAliasesCommand.php
+++ b/src/Commands/SiteAliasesCommand.php
@@ -3,6 +3,7 @@
 namespace Drall\Commands;
 
 use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 
 /**
@@ -14,10 +15,18 @@ protected function configure() {
     $this->setName('site:aliases');
     $this->setAliases(['sa']);
     $this->setDescription('Get a list of site aliases.');
+    $this->addOption(
+      'drall-group',
+      NULL,
+      InputOption::VALUE_REQUIRED,
+      'Site group identifier.'
+    );
   }
 
   protected function execute(InputInterface $input, OutputInterface $output) {
-    $aliases = $this->siteDetector()->getSiteAliases();
+    $aliases = $this->siteDetector()->getSiteAliases(
+      $input->getOption('drall-group')
+    );
 
     if (count($aliases) === 0) {
       $this->logger->warning('No site aliases found.');
diff --git a/src/Services/SiteDetector.php b/src/Services/SiteDetector.php
index 4f0b63f..8560c3b 100644
--- a/src/Services/SiteDetector.php
+++ b/src/Services/SiteDetector.php
@@ -46,11 +46,22 @@ public function getSiteDirNames(string $group = NULL): array {
   /**
    * Get site aliases.
    *
+   * @param string|null $group
+   *   A site group, if any.
+   *
    * @return Consolidation\SiteAlias\SiteAliasInterface[]
    *   Site aliases.
    */
-  public function getSiteAliases(): array {
-    return $this->siteAliasManager()->getMultiple();
+  public function getSiteAliases(string $group = NULL): array {
+    $result = $this->siteAliasManager()->getMultiple();
+
+    if (!$group) {
+      return $result;
+    }
+
+    return array_filter($result, function ($alias) use ($group) {
+      return in_array($group, $alias->get('drall.groups') ?? []);
+    });
   }
 
   /**
@@ -59,18 +70,16 @@ public function getSiteAliases(): array {
    * If there are aliases like @foo.dev and @foo.prod, then @foo part is
    * considered the site name.
    *
+   * @param string|null $group
+   *   A site group, if any.
+   *
    * @return array
    *   An array of site alias names with the @ prefix.
    */
   public function getSiteAliasNames(string $group = NULL): array {
-    $result = [];
-    foreach ($this->siteAliasManager()->getMultiple() as $siteAlias) {
-      if ($group && !in_array($group, $siteAlias->get('drall.groups') ?? [])) {
-        continue;
-      }
-
-      $result[] = explode('.', $siteAlias->name())[0];
-    }
+    $result = array_map(function ($siteAlias) {
+      return explode('.', $siteAlias->name())[0];
+    }, $this->getSiteAliases($group));
     return array_unique(array_values($result));
   }