From f28213b817688ec6f438b02296bb4d68b0b184e1 Mon Sep 17 00:00:00 2001 From: Pierre Tomasina Date: Mon, 14 May 2018 14:49:28 +0200 Subject: [PATCH] feat(export) export pipeline as yaml file (#14) --- composer.json | 4 +- src/ApplicationFactory.php | 2 + .../Pipeline/PipelineExportCommand.php | 78 +++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/Command/Pipeline/PipelineExportCommand.php diff --git a/composer.json b/composer.json index 8cc0075..b00b354 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "continuousphp/sdk": "dev-feat/entities", + "continuousphp/sdk": "^0.7", "symfony/console": "^3.3", "hoa/console": "~3.0" }, @@ -29,7 +29,7 @@ "repositories": [ { "type": "vcs", - "url": "https://github.com/Pierozi/guzzle-services.git" + "url": "https://github.com/continuousphp/guzzle-services.git" } ] } diff --git a/src/ApplicationFactory.php b/src/ApplicationFactory.php index d0485e4..19c9afa 100644 --- a/src/ApplicationFactory.php +++ b/src/ApplicationFactory.php @@ -7,6 +7,7 @@ use Continuous\Cli\Command\Build\BuildStopCommand; use Continuous\Cli\Command\Company\CompanyListCommand; use Continuous\Cli\Command\ConfigureCommand; +use Continuous\Cli\Command\Pipeline\PipelineExportCommand; use Continuous\Cli\Command\Project\ProjectListCommand; use Continuous\Cli\Command\Repository\RepositoryListCommand; use Symfony\Component\Console\Application; @@ -34,6 +35,7 @@ public function create() $application->add(new BuildListCommand()); $application->add(new BuildStartCommand()); $application->add(new BuildStopCommand()); + $application->add(new PipelineExportCommand()); return $application; } diff --git a/src/Command/Pipeline/PipelineExportCommand.php b/src/Command/Pipeline/PipelineExportCommand.php new file mode 100644 index 0000000..74a5ac0 --- /dev/null +++ b/src/Command/Pipeline/PipelineExportCommand.php @@ -0,0 +1,78 @@ +setName('pipeline:export') + ->setDescription('Export pipeline as configuration file.') + ->setHelp('This command export a specific pipeline as a configuration file in YAML.') + ->addArgument('provider', InputArgument::REQUIRED, 'The repository provider') + ->addArgument('repository', InputArgument::REQUIRED, 'The repository name') + ->addArgument('ref', InputArgument::REQUIRED, 'The git reference') + ; + + $this + ->addOption( + 'output', + 'o', + InputOption::VALUE_OPTIONAL, + 'File where yaml output will be persisted' + ); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return void + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $outFd = 'php://stdout'; + + if (null !== ($outOpt = $input->getOption('output'))) { + $outFd = getcwd() . DIRECTORY_SEPARATOR . $outOpt; + } + + $ref = $input->getArgument('ref'); + $params = [ + 'provider' => static::mapProviderToSdk($input->getArgument('provider')), + 'repository' => $input->getArgument('repository'), + 'ref' => $input->getArgument('ref'), + ]; + + $this->showLoader($output, 'Loading pipelines...'); + + /** @var Collection $collection */ + $collection = $this->continuousClient->getPipelines($params); + $entity = null; + + foreach ($collection->getIterator() as $entity) { + /** @var Pipeline $entity */ + if ($ref === $entity->get('settingId')) { + break; + } + + $entity = null; + } + + $exportDecorator = new PipelineExportDecorator($entity); + + $this->hideLoader($output); + file_put_contents($outFd, $exportDecorator->toYaml()); + } +}