diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 3d3595b..f558ed5 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -3,8 +3,10 @@ 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; use Drush\SiteAlias\SiteAliasManagerAwareInterface; use GuzzleHttp\Client; use Symfony\Component\HttpKernel\Kernel; @@ -64,8 +66,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'; @@ -84,12 +87,10 @@ 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. if ($this->projectName === FALSE) { $this->logger()->warning('ERROR: Could not discover project name, you should define it inside your .lagoon.yml file'); @@ -121,30 +122,28 @@ 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()); } /** * Run pre-rollout tasks. - * - * @command lagoon:pre-rollout-tasks */ + #[CLI\Command(name: 'lagoon:pre-rollout-tasks')] public function preRolloutTasks() { + $this->preCommandChecks(); $this->runRolloutTasks('pre'); } /** * Run post-rollout tasks. - * - * @command lagoon:post-rollout-tasks */ + #[CLI\Command(name: 'lagoon:post-rollout-tasks')] public function postRolloutTasks() { + $this->preCommandChecks(); $this->runRolloutTasks('post'); } @@ -264,4 +263,25 @@ public function getLagoonEnvs() { return json_decode($response); } + /** + * 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")); + } + + /** + * This should be run before any Lagoon commands. + * + * Checks whether we have a valid environment before running. + */ + private function preCommandChecks() { + if ($this->isLagoonEnvironment() == FALSE) { + throw new CommandFailedException(dt("Attempting to run a Lagoon command in a non-Lagoon environment.")); + } + } + }