protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input, true);
$environments = $this->api()->getEnvironments($this->getSelectedProject());
$toDelete = [];
// Gather inactive environments.
if ($input->getOption('inactive')) {
if ($input->getOption('no-delete-branch')) {
$this->stdErr->writeln('The option --no-delete-branch cannot be combined with --inactive.');
return 1;
}
$inactive = array_filter($environments, function ($environment) {
/** @var Environment $environment */
return $environment->status == 'inactive';
});
if (!$inactive) {
$this->stdErr->writeln('No inactive environments found.');
}
$toDelete = array_merge($toDelete, $inactive);
}
// Gather merged environments.
if ($input->getOption('merged')) {
if (!$this->hasSelectedEnvironment()) {
$this->stdErr->writeln('Cannot find merged environments: no base environment specified.');
return 1;
}
$base = $this->getSelectedEnvironment()->id;
$this->stdErr->writeln("Finding environments merged with <info>{$base}</info>.");
$merged = $this->getMergedEnvironments($base);
if (!$merged) {
$this->stdErr->writeln('No merged environments found.');
}
$toDelete = array_merge($toDelete, $merged);
}
// If --merged and --inactive are not specified, look for the selected
// environment(s).
if (!$input->getOption('merged') && !$input->getOption('inactive')) {
if ($this->hasSelectedEnvironment()) {
$toDelete = [$this->getSelectedEnvironment()];
} elseif ($environmentIds = $input->getArgument('environment')) {
$toDelete = array_intersect_key($environments, array_flip($environmentIds));
$notFound = array_diff($environmentIds, array_keys($environments));
foreach ($notFound as $notFoundId) {
$this->stdErr->writeln("Environment not found: <error>{$notFoundId}</error>");
}
}
}
// Exclude environment(s) specified in --exclude.
$toDelete = array_diff_key($toDelete, array_flip($input->getOption('exclude')));
if (empty($toDelete)) {
$this->stdErr->writeln('No environment(s) to delete.');
$this->stdErr->writeln('Use --environment (-e) to specify an environment.');
return 1;
}
$success = $this->deleteMultiple($toDelete, $input, $this->stdErr);
return $success ? 0 : 1;
}