Skip to content

Commit 5002de1

Browse files
committed
feat: add environment commands
1 parent 5e9a1c1 commit 5002de1

File tree

5 files changed

+220
-6
lines changed

5 files changed

+220
-6
lines changed

src/ApiClient.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ public function createEmailIdentity(int $providerId, string $name, string $regio
162162
]);
163163
}
164164

165+
/**
166+
* Create a new environment with the given name for the given project.
167+
*/
168+
public function createEnvironment(int $projectId, string $name): Collection
169+
{
170+
return $this->request('post', "/projects/{$projectId}/environments", [
171+
'name' => $name,
172+
]);
173+
}
174+
165175
/**
166176
* Create a new function invocation for the given project on the given environment.
167177
*/
@@ -289,6 +299,16 @@ public function deleteEmailIdentity(int $identityId)
289299
$this->request('delete', "/email-identities/{$identityId}");
290300
}
291301

302+
/**
303+
* Delete the given environment on the given project.
304+
*/
305+
public function deleteEnvironment(int $projectId, string $environment, bool $deleteResources = false)
306+
{
307+
$this->request('delete', "/projects/{$projectId}/environments/{$environment}", [
308+
'delete_resources' => $deleteResources,
309+
]);
310+
}
311+
292312
/**
293313
* Delete the given network.
294314
*/
@@ -508,6 +528,14 @@ public function getEnvironment(int $projectId, string $environment): Collection
508528
return $this->request('get', "/projects/{$projectId}/environments/$environment");
509529
}
510530

531+
/**
532+
* Get the details on the project's environments.
533+
*/
534+
public function getEnvironments(int $projectId): Collection
535+
{
536+
return $this->request('get', "/projects/{$projectId}/environments");
537+
}
538+
511539
/**
512540
* Get the project environment's vanity domain name.
513541
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Command\Environment;
15+
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Ymir\Cli\Command\AbstractProjectCommand;
19+
use Ymir\Cli\Console\ConsoleOutput;
20+
21+
class CreateEnvironmentCommand extends AbstractProjectCommand
22+
{
23+
/**
24+
* The name of the command.
25+
*
26+
* @var string
27+
*/
28+
public const NAME = 'environment:create';
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function configure()
34+
{
35+
$this
36+
->setName(self::NAME)
37+
->setDescription('Create a new environment')
38+
->addArgument('name', InputArgument::OPTIONAL, 'The name of the environment');
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function perform(InputInterface $input, ConsoleOutput $output)
45+
{
46+
$name = $this->getStringArgument($input, 'name') ?: $output->ask('What is the name of the environment');
47+
48+
$this->apiClient->createEnvironment($this->projectConfiguration->getProjectId(), $name);
49+
50+
$this->projectConfiguration->addEnvironment($name);
51+
52+
$output->info('Environment created');
53+
}
54+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Command\Environment;
15+
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Ymir\Cli\Command\AbstractProjectCommand;
19+
use Ymir\Cli\Console\ConsoleOutput;
20+
21+
class DeleteEnvironmentCommand extends AbstractProjectCommand
22+
{
23+
/**
24+
* The name of the command.
25+
*
26+
* @var string
27+
*/
28+
public const NAME = 'environment:delete';
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function configure()
34+
{
35+
$this
36+
->setName(self::NAME)
37+
->setDescription('Delete an environment')
38+
->addArgument('name', InputArgument::OPTIONAL, 'The name of the environment');
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function perform(InputInterface $input, ConsoleOutput $output)
45+
{
46+
$name = $this->getStringArgument($input, 'name');
47+
48+
if (!empty($name) && !$output->confirm('Are you sure you want to delete this environment?', false)) {
49+
return;
50+
} elseif (empty($name)) {
51+
$name = $output->choice('Please choose an environment to delete', $this->apiClient->getEnvironments($this->projectConfiguration->getProjectId())->pluck(['name'])->all());
52+
}
53+
54+
$deleteResources = (bool) $output->confirm('Do you want to delete all the environment resources on the cloud provider?', false);
55+
56+
$this->apiClient->deleteEnvironment($this->projectConfiguration->getProjectId(), $name, $deleteResources);
57+
58+
$this->projectConfiguration->deleteEnvironment($name);
59+
60+
$message = 'Environment deleted';
61+
$deleteResources ? $output->infoWithDelayWarning($message) : $output->info($message);
62+
}
63+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Command\Environment;
15+
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Ymir\Cli\Command\AbstractProjectCommand;
18+
use Ymir\Cli\Console\ConsoleOutput;
19+
20+
class ListEnvironmentsCommand extends AbstractProjectCommand
21+
{
22+
/**
23+
* The name of the command.
24+
*
25+
* @var string
26+
*/
27+
public const NAME = 'environment:list';
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function configure()
33+
{
34+
$this
35+
->setName(self::NAME)
36+
->setDescription('List the project\'s environments');
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function perform(InputInterface $input, ConsoleOutput $output)
43+
{
44+
$output->table(
45+
['Id', 'Name', 'URL'],
46+
$this->apiClient->getEnvironments($this->projectConfiguration->getProjectId())->map(function (array $environment) {
47+
return [$environment['id'], $environment['name'], 'https://'.$environment['vanity_domain_name']];
48+
})->all()
49+
);
50+
}
51+
}

src/ProjectConfiguration.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public function __destruct()
6464
$this->save();
6565
}
6666

67+
/**
68+
* Add a new environment node to the project configuration.
69+
*/
70+
public function addEnvironment(string $name, ?array $options = null)
71+
{
72+
if ('bedrock' === $this->configuration['type']) {
73+
$options = array_merge(['build' => ['composer install --classmap-authoritative']], (array) $options);
74+
}
75+
76+
$this->configuration['environments'][$name] = $options;
77+
}
78+
6779
/**
6880
* Creates a new configuration from the given project.
6981
*
@@ -74,12 +86,8 @@ public function createNew(Collection $project, string $databaseName = '', string
7486
$this->configuration = $project->only(['id', 'name'])->all();
7587
$this->configuration['type'] = $type ?: 'wordpress';
7688

77-
$baseEnvironment = 'bedrock' === $type ? ['build' => ['composer install --classmap-authoritative']] : null;
78-
79-
$this->configuration['environments'] = [
80-
'production' => $baseEnvironment,
81-
'staging' => array_merge((array) $baseEnvironment, ['cdn' => ['caching' => 'assets'], 'cron' => false, 'warmup' => false]),
82-
];
89+
$this->addEnvironment('production');
90+
$this->addEnvironment('staging', ['cdn' => ['caching' => 'assets'], 'cron' => false, 'warmup' => false]);
8391

8492
if (!empty($databaseServer) && !empty($databaseName)) {
8593
$this->configuration['environments']['staging']['database'] = [
@@ -103,6 +111,16 @@ public function delete()
103111
$this->filesystem->remove($this->configurationFilePath);
104112
}
105113

114+
/**
115+
* Delete the given project environment.
116+
*/
117+
public function deleteEnvironment(string $environment)
118+
{
119+
if ($this->hasEnvironment($environment)) {
120+
unset($this->configuration['environments'][$environment]);
121+
}
122+
}
123+
106124
/**
107125
* Checks if the project configuration file exists.
108126
*/

0 commit comments

Comments
 (0)