Skip to content

Commit 3b3350e

Browse files
committed
feat: allow the project:delete command to delete other projects
1 parent b627688 commit 3b3350e

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

src/ApiClient.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,14 @@ public function getTeamNetworks(int $teamId): Collection
788788
return $this->request('get', "/teams/{$teamId}/networks");
789789
}
790790

791+
/**
792+
* Get the projects that belong to the given team.
793+
*/
794+
public function getTeamProjects(int $teamId): Collection
795+
{
796+
return $this->request('get', "/teams/{$teamId}/projects");
797+
}
798+
791799
/**
792800
* Get the teams the user is a member of.
793801
*/

src/Command/AbstractCommand.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,34 @@ protected function determineOrCreateNetwork(string $question, InputInterface $in
158158
return $this->determineNetwork($question, $input, $output);
159159
}
160160

161+
/**
162+
* Determine the project to use.
163+
*/
164+
protected function determineProject(string $question, InputInterface $input, ConsoleOutput $output): int
165+
{
166+
$projects = $this->apiClient->getTeamProjects($this->cliConfiguration->getActiveTeamId());
167+
168+
if ($projects->isEmpty()) {
169+
throw new RuntimeException('There are no projects on the currently active team.');
170+
}
171+
172+
$projectIdOrName = $this->getStringArgument($input, 'project');
173+
174+
if (empty($projectIdOrName)) {
175+
$projectIdOrName = $output->choiceWithId($question, $projects);
176+
} elseif (1 < $projects->where('name', $projectIdOrName)->count()) {
177+
throw new RuntimeException(sprintf('Unable to select a project because more than one project has the name "%s"', $projectIdOrName));
178+
}
179+
180+
$project = $projects->firstWhere('name', $projectIdOrName) ?? $projects->firstWhere('id', $projectIdOrName);
181+
182+
if (empty($project['id'])) {
183+
throw new RuntimeException(sprintf('Unable to find a project with "%s" as the ID or name', $projectIdOrName));
184+
}
185+
186+
return (int) $project['id'];
187+
}
188+
161189
/**
162190
* Determine the cloud provider region to use.
163191
*/

src/Command/Project/DeleteProjectCommand.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
namespace Ymir\Cli\Command\Project;
1515

16+
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
17-
use Ymir\Cli\Command\AbstractProjectCommand;
18+
use Ymir\Cli\Command\AbstractCommand;
1819
use Ymir\Cli\Console\ConsoleOutput;
1920

20-
class DeleteProjectCommand extends AbstractProjectCommand
21+
class DeleteProjectCommand extends AbstractCommand
2122
{
2223
/**
2324
* The alias of the command.
@@ -40,7 +41,8 @@ protected function configure()
4041
{
4142
$this
4243
->setName(self::NAME)
43-
->setDescription('Delete the project')
44+
->setDescription('Delete a project')
45+
->addArgument('project', InputArgument::OPTIONAL, 'The ID or name of the project to delete')
4446
->setAliases([self::ALIAS]);
4547
}
4648

@@ -49,15 +51,25 @@ protected function configure()
4951
*/
5052
protected function perform(InputInterface $input, ConsoleOutput $output)
5153
{
52-
if (!$output->confirm('Are you sure you want to delete this project?', false)) {
54+
$projectId = $this->projectConfiguration->exists() ? $this->projectConfiguration->getProjectId() : null;
55+
56+
if (null === $projectId) {
57+
$projectId = $this->determineProject('Which project would you like to delete', $input, $output);
58+
}
59+
60+
$project = $this->apiClient->getProject($projectId);
61+
62+
if (!$output->confirm(sprintf('Are you sure you want to delete the <comment>%s</comment> project?', $project['name']), false)) {
5363
return;
5464
}
5565

5666
$deleteResources = (bool) $output->confirm('Do you want to delete all the project resources on the cloud provider?', false);
5767

58-
$this->apiClient->deleteProject($this->projectConfiguration->getProjectId(), $deleteResources);
68+
$this->apiClient->deleteProject($projectId, $deleteResources);
5969

60-
$this->projectConfiguration->delete();
70+
if ($this->projectConfiguration->exists() && $projectId === $this->projectConfiguration->getProjectId()) {
71+
$this->projectConfiguration->delete();
72+
}
6173

6274
$message = 'Project deleted';
6375
$deleteResources ? $output->infoWithDelayWarning($message) : $output->info($message);

0 commit comments

Comments
 (0)