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/4] 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/4] 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/4] 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/4] 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) {