From 3297004380ced5ec2f20677b7419b55cb94831af Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 09:30:03 +1000 Subject: [PATCH 1/7] Adds Lagoon env checks to commands --- src/LagoonCommands.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 3d3595b..889ab23 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -4,6 +4,7 @@ use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Commands\DrushCommands; +use Drush\Exceptions\CommandFailedException; use Drush\Drush; use Drush\SiteAlias\SiteAliasManagerAwareInterface; use GuzzleHttp\Client; @@ -64,8 +65,9 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter * {@inheritdoc} */ public function __construct() { - if (getenv('LAGOON')) { - // Get default config. + // Get default config + if($this->isLagoonEnvironment()) { + $this->isLagoonEnvironment = TRUE; $lagoonyml = $this->getLagoonYml(); $this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; $this->endpoint = $lagoonyml['ssh'] ?? 'ssh.lagoon.amazeeio.cloud:32222'; @@ -90,6 +92,7 @@ public function __construct() { * @aliases la */ public function aliases() { + $this->preCommandChecks(); // Project still not defined, throw a warning. if ($this->projectName === FALSE) { $this->logger()->warning('ERROR: Could not discover project name, you should define it inside your .lagoon.yml file'); @@ -127,6 +130,7 @@ public function aliases() { * @aliases jwt */ public function generateJwt() { + $this->preCommandChecks(); $this->io()->writeln($this->getJwtToken()); } @@ -136,6 +140,7 @@ public function generateJwt() { * @command lagoon:pre-rollout-tasks */ public function preRolloutTasks() { + $this->preCommandChecks(); $this->runRolloutTasks('pre'); } @@ -145,6 +150,7 @@ public function preRolloutTasks() { * @command lagoon:post-rollout-tasks */ public function postRolloutTasks() { + $this->preCommandChecks(); $this->runRolloutTasks('post'); } @@ -264,4 +270,18 @@ public function getLagoonEnvs() { return json_decode($response); } + /** + * Will check whether the current environment is Lagoon or Lagoon dev envs. + * + * @return bool + */ + private function isLagoonEnvironment() { + return !empty(getenv("LAGOON")); + } + private function preCommandChecks() { + if($this->isLagoonEnvironment() == FALSE) { + throw new CommandFailedException(dt("Attempting to run a Lagoon command in a non-Lagoon environment.")); + } + } + } From f946f88e671d32ebd56166ba5149bf408ca09f1d Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 09:31:09 +1000 Subject: [PATCH 2/7] Spaces and comments --- src/LagoonCommands.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 889ab23..0091cab 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -278,6 +278,13 @@ public function getLagoonEnvs() { private function isLagoonEnvironment() { return !empty(getenv("LAGOON")); } + + /** + * This should be run before any Lagoon commands. + * Checks whether we have a valid environment before running. + * + * @return void + */ private function preCommandChecks() { if($this->isLagoonEnvironment() == FALSE) { throw new CommandFailedException(dt("Attempting to run a Lagoon command in a non-Lagoon environment.")); From 0e1927be365c526368953d6f0f7d3fdb5d80c89f Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 09:31:09 +1000 Subject: [PATCH 3/7] phpcbf auto-fixes --- src/LagoonCommands.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 0091cab..7f4a7d9 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -4,8 +4,8 @@ use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Commands\DrushCommands; -use Drush\Exceptions\CommandFailedException; use Drush\Drush; +use Drush\Exceptions\CommandFailedException; use Drush\SiteAlias\SiteAliasManagerAwareInterface; use GuzzleHttp\Client; use Symfony\Component\HttpKernel\Kernel; @@ -65,8 +65,8 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter * {@inheritdoc} */ public function __construct() { - // Get default config - if($this->isLagoonEnvironment()) { + // Get default config. + if ($this->isLagoonEnvironment()) { $this->isLagoonEnvironment = TRUE; $lagoonyml = $this->getLagoonYml(); $this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; @@ -286,7 +286,7 @@ private function isLagoonEnvironment() { * @return void */ private function preCommandChecks() { - if($this->isLagoonEnvironment() == FALSE) { + if ($this->isLagoonEnvironment() == FALSE) { throw new CommandFailedException(dt("Attempting to run a Lagoon command in a non-Lagoon environment.")); } } From e062c1e5ad12d933b3b50ac71f2ad3b554fd3ddf Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 09:31:09 +1000 Subject: [PATCH 4/7] Further fixes --- src/LagoonCommands.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 7f4a7d9..361441d 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -274,6 +274,7 @@ public function getLagoonEnvs() { * Will check whether the current environment is Lagoon or Lagoon dev envs. * * @return bool + * is a functional Lagoon env */ private function isLagoonEnvironment() { return !empty(getenv("LAGOON")); @@ -281,9 +282,8 @@ private function isLagoonEnvironment() { /** * This should be run before any Lagoon commands. - * Checks whether we have a valid environment before running. * - * @return void + * Checks whether we have a valid environment before running. */ private function preCommandChecks() { if ($this->isLagoonEnvironment() == FALSE) { From 32161d496a62ebe721f10e9e388fa3229accdbd9 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 11:08:16 +1000 Subject: [PATCH 5/7] Adds Lagoon env checks to commands --- src/LagoonCommands.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 361441d..638328a 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -4,6 +4,7 @@ use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Commands\DrushCommands; +use Drush\Exceptions\CommandFailedException; use Drush\Drush; use Drush\Exceptions\CommandFailedException; use Drush\SiteAlias\SiteAliasManagerAwareInterface; From 9f66b65a83a1711c69d83ef420b4d03dc89c4dbb Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 11:09:04 +1000 Subject: [PATCH 6/7] phpcbf auto-fixes --- src/LagoonCommands.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 638328a..361441d 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -4,7 +4,6 @@ use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Commands\DrushCommands; -use Drush\Exceptions\CommandFailedException; use Drush\Drush; use Drush\Exceptions\CommandFailedException; use Drush\SiteAlias\SiteAliasManagerAwareInterface; From 0d1c803972b37332455750a0ee4b1d10871b5d13 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 29 Aug 2024 11:09:04 +1000 Subject: [PATCH 7/7] Updates annotations to attributes --- src/LagoonCommands.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 361441d..f558ed5 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -3,6 +3,7 @@ namespace Drush\Commands\drupal_integrations; use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Drush\Attributes as CLI; use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Exceptions\CommandFailedException; @@ -86,11 +87,8 @@ public function __construct() { /** * Get all remote aliases from lagoon API. - * - * @command lagoon:aliases - * - * @aliases la */ + #[CLI\Command(name: 'lagoon:aliases', aliases: ['la'])] public function aliases() { $this->preCommandChecks(); // Project still not defined, throw a warning. @@ -124,11 +122,8 @@ public function aliases() { /** * Generate a JWT token for the lagoon API. - * - * @command lagoon:jwt - * - * @aliases jwt */ + #[CLI\Command(name: 'lagoon:jwt', aliases: ['jwt'])] public function generateJwt() { $this->preCommandChecks(); $this->io()->writeln($this->getJwtToken()); @@ -136,9 +131,8 @@ public function generateJwt() { /** * Run pre-rollout tasks. - * - * @command lagoon:pre-rollout-tasks */ + #[CLI\Command(name: 'lagoon:pre-rollout-tasks')] public function preRolloutTasks() { $this->preCommandChecks(); $this->runRolloutTasks('pre'); @@ -146,9 +140,8 @@ public function preRolloutTasks() { /** * Run post-rollout tasks. - * - * @command lagoon:post-rollout-tasks */ + #[CLI\Command(name: 'lagoon:post-rollout-tasks')] public function postRolloutTasks() { $this->preCommandChecks(); $this->runRolloutTasks('post');