Add --format json to search command (#9747)
Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>pull/9765/head
parent
8f315551d4
commit
35210d99a9
|
@ -359,6 +359,11 @@ You can also search for more than one term by passing multiple arguments.
|
|||
|
||||
* **--only-name (-N):** Search only in name.
|
||||
* **--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
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
namespace Composer\Command;
|
||||
|
||||
use Composer\Factory;
|
||||
use Composer\Json\JsonFile;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
@ -42,6 +43,7 @@ class SearchCommand extends BaseCommand
|
|||
->setDefinition(array(
|
||||
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('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'),
|
||||
))
|
||||
->setHelp(
|
||||
|
@ -60,6 +62,14 @@ EOT
|
|||
// init repos
|
||||
$platformRepo = new PlatformRepository;
|
||||
$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))) {
|
||||
$composer = Factory::create($this->getIO(), array(), $input->hasParameterOption('--no-plugins'));
|
||||
}
|
||||
|
@ -76,9 +86,13 @@ EOT
|
|||
$flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
|
||||
$results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
|
||||
|
||||
if ($results && $format === 'text') {
|
||||
foreach ($results as $result) {
|
||||
$io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
|
||||
}
|
||||
} elseif ($format === 'json') {
|
||||
$io->write(JsonFile::encode($results));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue