1
0
Fork 0

Add --format json to search command (#9747)

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
pull/9765/head
ochorocho 2021-03-09 23:06:02 +01:00 committed by GitHub
parent 8f315551d4
commit 35210d99a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -359,6 +359,11 @@ You can also search for more than one term by passing multiple arguments.
* **--only-name (-N):** Search only in name. * **--only-name (-N):** Search only in name.
* **--type (-t):** Search for a specific package type. * **--type (-t):** Search for a specific package type.
* **--format (-f):** Lets you pick between text (default) or json output format.
Note that in the json, only the name and description keys are guaranteed to be
present. The rest (`url`, `repository`, `downloads` and `favers`) are available
for Packagist.org search results and other repositories may return more or less
data.
## show ## show

View File

@ -13,6 +13,7 @@
namespace Composer\Command; namespace Composer\Command;
use Composer\Factory; use Composer\Factory;
use Composer\Json\JsonFile;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
@ -42,6 +43,7 @@ class SearchCommand extends BaseCommand
->setDefinition(array( ->setDefinition(array(
new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in name'), new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in name'),
new InputOption('type', 't', InputOption::VALUE_REQUIRED, 'Search for a specific package type'), new InputOption('type', 't', InputOption::VALUE_REQUIRED, 'Search for a specific package type'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'),
new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'), new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
)) ))
->setHelp( ->setHelp(
@ -60,6 +62,14 @@ EOT
// init repos // init repos
$platformRepo = new PlatformRepository; $platformRepo = new PlatformRepository;
$io = $this->getIO(); $io = $this->getIO();
$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 (!($composer = $this->getComposer(false))) { if (!($composer = $this->getComposer(false))) {
$composer = Factory::create($this->getIO(), array(), $input->hasParameterOption('--no-plugins')); $composer = Factory::create($this->getIO(), array(), $input->hasParameterOption('--no-plugins'));
} }
@ -76,8 +86,12 @@ EOT
$flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT; $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
$results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type); $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
foreach ($results as $result) { if ($results && $format === 'text') {
$io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : '')); foreach ($results as $result) {
$io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
}
} elseif ($format === 'json') {
$io->write(JsonFile::encode($results));
} }
return 0; return 0;