|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Continuous\Cli\Command\Pipeline; |
| 4 | + |
| 5 | +use Continuous\Cli\Command\CommandAbstract; |
| 6 | +use Continuous\Sdk\Collection; |
| 7 | +use Continuous\Sdk\Decorator\Pipeline\PipelineExportDecorator; |
| 8 | +use Continuous\Sdk\Entity\Pipeline; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +class PipelineExportCommand extends CommandAbstract |
| 15 | +{ |
| 16 | + protected function configure() |
| 17 | + { |
| 18 | + $this |
| 19 | + ->setName('pipeline:export') |
| 20 | + ->setDescription('Export pipeline as configuration file.') |
| 21 | + ->setHelp('This command export a specific pipeline as a configuration file in YAML.') |
| 22 | + ->addArgument('provider', InputArgument::REQUIRED, 'The repository provider') |
| 23 | + ->addArgument('repository', InputArgument::REQUIRED, 'The repository name') |
| 24 | + ->addArgument('ref', InputArgument::REQUIRED, 'The git reference') |
| 25 | + ; |
| 26 | + |
| 27 | + $this |
| 28 | + ->addOption( |
| 29 | + 'output', |
| 30 | + 'o', |
| 31 | + InputOption::VALUE_OPTIONAL, |
| 32 | + 'File where yaml output will be persisted' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param InputInterface $input |
| 38 | + * @param OutputInterface $output |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 42 | + { |
| 43 | + parent::execute($input, $output); |
| 44 | + |
| 45 | + $outFd = 'php://stdout'; |
| 46 | + |
| 47 | + if (null !== ($outOpt = $input->getOption('output'))) { |
| 48 | + $outFd = getcwd() . DIRECTORY_SEPARATOR . $outOpt; |
| 49 | + } |
| 50 | + |
| 51 | + $ref = $input->getArgument('ref'); |
| 52 | + $params = [ |
| 53 | + 'provider' => static::mapProviderToSdk($input->getArgument('provider')), |
| 54 | + 'repository' => $input->getArgument('repository'), |
| 55 | + 'ref' => $input->getArgument('ref'), |
| 56 | + ]; |
| 57 | + |
| 58 | + $this->showLoader($output, 'Loading pipelines...'); |
| 59 | + |
| 60 | + /** @var Collection $collection */ |
| 61 | + $collection = $this->continuousClient->getPipelines($params); |
| 62 | + $entity = null; |
| 63 | + |
| 64 | + foreach ($collection->getIterator() as $entity) { |
| 65 | + /** @var Pipeline $entity */ |
| 66 | + if ($ref === $entity->get('settingId')) { |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + $entity = null; |
| 71 | + } |
| 72 | + |
| 73 | + $exportDecorator = new PipelineExportDecorator($entity); |
| 74 | + |
| 75 | + $this->hideLoader($output); |
| 76 | + file_put_contents($outFd, $exportDecorator->toYaml()); |
| 77 | + } |
| 78 | +} |
0 commit comments