Skip to content

Commit e63c52e

Browse files
committed
feat: better handling of drush commands outside of Lagoon environments
Allows all `lagoon` commands to be run in DDEV
1 parent 5ff908d commit e63c52e

1 file changed

Lines changed: 35 additions & 27 deletions

File tree

src/LagoonCommands.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter
2323
*
2424
* @var string
2525
*/
26-
private $api;
26+
private $api = 'https://api.lagoon.amazeeio.cloud/graphql';
2727

2828
/**
2929
* Lagoon SSH endpoint.
3030
*
3131
* @var string
3232
*/
33-
private $endpoint;
33+
private $endpoint = 'ssh.lagoon.amazeeio.cloud:32222';
3434

3535
/**
3636
* JWT token.
3737
*
3838
* @var string
3939
*/
40-
private $jwttoken;
40+
private $jwtToken;
4141

4242
/**
4343
* Lagoon project name.
4444
*
4545
* @var string
4646
*/
47-
private $projectName;
47+
private $projectName = '';
4848

4949
/**
5050
* Connection timeout.
5151
*
5252
* @var int
5353
*/
54-
private $sshTimeout;
54+
private $sshTimeout = 30;
5555

5656
/**
5757
* Path to a specific SSH key to use for lagoon authentication.
@@ -64,22 +64,20 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter
6464
* {@inheritdoc}
6565
*/
6666
public function __construct() {
67-
if (getenv('LAGOON')) {
68-
// Get default config.
69-
$lagoonyml = $this->getLagoonYml();
70-
$this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql';
71-
$this->endpoint = $lagoonyml['ssh'] ?? 'ssh.lagoon.amazeeio.cloud:32222';
72-
$this->jwt_token = getenv('LAGOON_OVERRIDE_JWT_TOKEN');
73-
$this->projectName = $lagoonyml['project'] ?? '';
74-
$this->ssh_port_timeout = $lagoonyml['ssh_port_timeout'] ?? 30;
75-
76-
// Allow environment variable overrides.
77-
$this->api = getenv('LAGOON_OVERRIDE_API') ?: $this->api;
78-
$this->endpoint = getenv('LAGOON_OVERRIDE_SSH') ?: $this->endpoint;
79-
$this->projectName = getenv('LAGOON_PROJECT') ?: $this->projectName;
80-
$this->sshTimeout = getenv('LAGOON_OVERRIDE_SSH_TIMEOUT') ?: $this->sshTimeout;
81-
$this->sshKey = getenv('LAGOON_SSH_KEY');
82-
}
67+
// Allow `.lagoon.yml` overrides.
68+
$lagoonyml = $this->getLagoonYml();
69+
$this->api = $lagoonyml['api'] ?? $this->api;
70+
$this->endpoint = $lagoonyml['ssh'] ?? $this->endpoint;
71+
$this->projectName = $lagoonyml['project'] ?? $this->projectName;
72+
$this->sshTimeout = $lagoonyml['ssh_port_timeout'] ?? $this->sshTimeout;
73+
74+
// Allow environment variable overrides.
75+
$this->api = getenv('LAGOON_OVERRIDE_API') ?: $this->api;
76+
$this->endpoint = getenv('LAGOON_OVERRIDE_SSH') ?: $this->endpoint;
77+
$this->projectName = getenv('LAGOON_PROJECT') ?: $this->projectName;
78+
$this->sshTimeout = getenv('LAGOON_OVERRIDE_SSH_TIMEOUT') ?: $this->sshTimeout;
79+
$this->sshKey = getenv('LAGOON_SSH_KEY');
80+
$this->jwtToken = getenv('LAGOON_OVERRIDE_JWT_TOKEN');
8381
}
8482

8583
/**
@@ -96,8 +94,8 @@ public function aliases() {
9694
return;
9795
}
9896

99-
if (empty($this->jwt_token)) {
100-
$this->jwt_token = $this->getJwtToken();
97+
if (empty($this->jwtToken)) {
98+
$this->jwtToken = $this->getJwtToken();
10199
}
102100

103101
$response = $this->getLagoonEnvs();
@@ -176,9 +174,19 @@ public function runRolloutTasks($stage = 'post') {
176174
* Retrieves the contents of the sites .lagoon.yml file.
177175
*/
178176
public function getLagoonYml() {
179-
$project_root = Drush::bootstrapManager()->getComposerRoot();
180-
$lagoonyml_path = $project_root . "/.lagoon.yml";
181-
return (file_exists($lagoonyml_path)) ? Yaml::parse(file_get_contents($lagoonyml_path)) : [];
177+
try {
178+
$project_root = Drush::bootstrapManager()->getComposerRoot();
179+
$lagoonyml_path = $project_root . "/.lagoon.yml";
180+
return (file_exists($lagoonyml_path)) ? Yaml::parse(file_get_contents($lagoonyml_path)) : [];
181+
}
182+
catch (\Throwable $e) {
183+
if (empty(getenv("LAGOON"))) {
184+
// We might not be in a Lagoon environment, fail silently.
185+
return [];
186+
}
187+
188+
throw $e;
189+
}
182190
}
183191

184192
/**
@@ -248,7 +256,7 @@ public function getLagoonEnvs() {
248256
'base_uri' => $this->api,
249257
'headers' => [
250258
'Content-Type' => 'application/json',
251-
'Authorization' => 'Bearer ' . $this->jwt_token,
259+
'Authorization' => 'Bearer ' . $this->jwtToken,
252260
],
253261
'body' => json_encode(['query' => $query]),
254262
]);

0 commit comments

Comments
 (0)