Skip to content

Commit

Permalink
feat: allow the project:delete command to delete other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Aug 28, 2021
1 parent b627688 commit 3b3350e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ public function getTeamNetworks(int $teamId): Collection
return $this->request('get', "/teams/{$teamId}/networks");
}

/**
* Get the projects that belong to the given team.
*/
public function getTeamProjects(int $teamId): Collection
{
return $this->request('get', "/teams/{$teamId}/projects");
}

/**
* Get the teams the user is a member of.
*/
Expand Down
28 changes: 28 additions & 0 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,34 @@ protected function determineOrCreateNetwork(string $question, InputInterface $in
return $this->determineNetwork($question, $input, $output);
}

/**
* Determine the project to use.
*/
protected function determineProject(string $question, InputInterface $input, ConsoleOutput $output): int
{
$projects = $this->apiClient->getTeamProjects($this->cliConfiguration->getActiveTeamId());

if ($projects->isEmpty()) {
throw new RuntimeException('There are no projects on the currently active team.');
}

$projectIdOrName = $this->getStringArgument($input, 'project');

if (empty($projectIdOrName)) {
$projectIdOrName = $output->choiceWithId($question, $projects);
} elseif (1 < $projects->where('name', $projectIdOrName)->count()) {
throw new RuntimeException(sprintf('Unable to select a project because more than one project has the name "%s"', $projectIdOrName));
}

$project = $projects->firstWhere('name', $projectIdOrName) ?? $projects->firstWhere('id', $projectIdOrName);

if (empty($project['id'])) {
throw new RuntimeException(sprintf('Unable to find a project with "%s" as the ID or name', $projectIdOrName));
}

return (int) $project['id'];
}

/**
* Determine the cloud provider region to use.
*/
Expand Down
24 changes: 18 additions & 6 deletions src/Command/Project/DeleteProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

namespace Ymir\Cli\Command\Project;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Ymir\Cli\Command\AbstractProjectCommand;
use Ymir\Cli\Command\AbstractCommand;
use Ymir\Cli\Console\ConsoleOutput;

class DeleteProjectCommand extends AbstractProjectCommand
class DeleteProjectCommand extends AbstractCommand
{
/**
* The alias of the command.
Expand All @@ -40,7 +41,8 @@ protected function configure()
{
$this
->setName(self::NAME)
->setDescription('Delete the project')
->setDescription('Delete a project')
->addArgument('project', InputArgument::OPTIONAL, 'The ID or name of the project to delete')
->setAliases([self::ALIAS]);
}

Expand All @@ -49,15 +51,25 @@ protected function configure()
*/
protected function perform(InputInterface $input, ConsoleOutput $output)
{
if (!$output->confirm('Are you sure you want to delete this project?', false)) {
$projectId = $this->projectConfiguration->exists() ? $this->projectConfiguration->getProjectId() : null;

if (null === $projectId) {
$projectId = $this->determineProject('Which project would you like to delete', $input, $output);
}

$project = $this->apiClient->getProject($projectId);

if (!$output->confirm(sprintf('Are you sure you want to delete the <comment>%s</comment> project?', $project['name']), false)) {
return;
}

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

$this->apiClient->deleteProject($this->projectConfiguration->getProjectId(), $deleteResources);
$this->apiClient->deleteProject($projectId, $deleteResources);

$this->projectConfiguration->delete();
if ($this->projectConfiguration->exists() && $projectId === $this->projectConfiguration->getProjectId()) {
$this->projectConfiguration->delete();
}

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

0 comments on commit 3b3350e

Please sign in to comment.