|
| 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\Docker; |
| 15 | + |
| 16 | +use Symfony\Component\Console\Exception\RuntimeException; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Ymir\Cli\Command\AbstractCommand; |
| 20 | +use Ymir\Cli\Console\OutputInterface; |
| 21 | +use Ymir\Cli\Tool\Docker; |
| 22 | + |
| 23 | +class PruneDockerImagesCommand extends AbstractCommand |
| 24 | +{ |
| 25 | + /** |
| 26 | + * The name of the command. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + public const NAME = 'docker:prune'; |
| 31 | + |
| 32 | + /** |
| 33 | + * The grep pattern used with "--all" option. |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + private const ALL_PATTERN = 'dkr.ecr'; |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + protected function configure() |
| 43 | + { |
| 44 | + $this |
| 45 | + ->setName(self::NAME) |
| 46 | + ->setDescription('Prune deployment docker images') |
| 47 | + ->addOption('all', null, InputOption::VALUE_NONE, 'Delete all deployment docker images'); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + protected function perform(InputInterface $input, OutputInterface $output) |
| 54 | + { |
| 55 | + $pattern = $this->getBooleanOption($input, 'all') ? self::ALL_PATTERN : null; |
| 56 | + |
| 57 | + if (!is_string($pattern)) { |
| 58 | + $pattern = $this->determinePattern($input, $output); |
| 59 | + } |
| 60 | + |
| 61 | + if (!is_string($pattern) || !$output->confirm(self::ALL_PATTERN === $pattern ? 'Are you sure you want to delete all deployment docker images?' : 'Are you sure you want to delete the project\'s deployment docker images?', false)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + Docker::rmigrep($pattern); |
| 66 | + |
| 67 | + $output->info('Deployment docker images pruned successfully'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Determine the grep pattern to use. |
| 72 | + */ |
| 73 | + private function determinePattern(InputInterface $input, OutputInterface $output): ?string |
| 74 | + { |
| 75 | + $project = $this->projectConfiguration->exists() ? $this->apiClient->getProject($this->projectConfiguration->getProjectId()) : null; |
| 76 | + |
| 77 | + if (null !== $project && !empty($project['repository_uri'])) { |
| 78 | + return $project['repository_uri']; |
| 79 | + } elseif (null !== $project && empty($project['repository_uri'])) { |
| 80 | + throw new RuntimeException(sprintf('The "%s" project has\'t been deployed using container images', $project['name'])); |
| 81 | + } elseif (!$input->isInteractive()) { |
| 82 | + throw new RuntimeException('Must run command inside an existing project or with "--all" option in non-interactive mode'); |
| 83 | + } |
| 84 | + |
| 85 | + $output->warning('No project detected in the current directory'); |
| 86 | + |
| 87 | + return $output->confirm('Do you want to delete all deployment docker images?', false) ? self::ALL_PATTERN : null; |
| 88 | + } |
| 89 | +} |
0 commit comments