From 91b6ff27ac92a9b9e1b6c96949e9e8db50cddc78 Mon Sep 17 00:00:00 2001 From: ochorocho Date: Wed, 24 Feb 2021 17:21:10 +0100 Subject: [PATCH] Add --format option to `composer fund` command (#9678) * Add --format option to `composer fund` command Co-authored-by: Jochen Roth --- src/Composer/Command/FundCommand.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Composer/Command/FundCommand.php b/src/Composer/Command/FundCommand.php index 6ae37ef2d..788d60f21 100644 --- a/src/Composer/Command/FundCommand.php +++ b/src/Composer/Command/FundCommand.php @@ -12,12 +12,14 @@ namespace Composer\Command; -use Composer\Package\CompletePackageInterface; +use Composer\Json\JsonFile; use Composer\Package\AliasPackage; use Composer\Package\BasePackage; -use Composer\Semver\Constraint\MatchAllConstraint; +use Composer\Package\CompletePackageInterface; use Composer\Repository\CompositeRepository; +use Composer\Semver\Constraint\MatchAllConstraint; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -30,6 +32,9 @@ class FundCommand extends BaseCommand { $this->setName('fund') ->setDescription('Discover how to help fund the maintenance of your dependencies.') + ->setDefinition(array( + new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text') + )) ; } @@ -81,7 +86,14 @@ class FundCommand extends BaseCommand $io = $this->getIO(); - if ($fundings) { + $format = $input->getOption('format'); + if (!in_array($format, array('text', 'json'))) { + $io->writeError(sprintf('Unsupported format "%s". See help for supported formats.', $format)); + + return 1; + } + + if ($fundings && $format === 'text') { $prev = null; $io->write('The following packages were found in your dependencies which publish funding information:'); @@ -104,6 +116,8 @@ class FundCommand extends BaseCommand $io->write(""); $io->write("Please consider following these links and sponsoring the work of package authors!"); $io->write("Thank you!"); + } elseif ($format === 'json') { + $io->write(JsonFile::encode($fundings)); } else { $io->write("No funding links were found in your package dependencies. This doesn't mean they don't need your support!"); }