Add json output for show command

main
Alexander Schwenn 7 years ago
parent 818687849d
commit c70aaa421d

@ -14,6 +14,7 @@ namespace Composer\Command;
use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\DefaultPolicy; use Composer\DependencyResolver\DefaultPolicy;
use Composer\Json\JsonFile;
use Composer\Package\CompletePackageInterface; use Composer\Package\CompletePackageInterface;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Composer\Package\BasePackage; use Composer\Package\BasePackage;
@ -74,6 +75,7 @@ class ShowCommand extends BaseCommand
new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'), new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'),
new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'), new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'),
new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'), new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'),
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
The show command displays detailed information about a package, or The show command displays detailed information about a package, or
@ -114,6 +116,13 @@ EOT
return 1; return 1;
} }
$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;
}
// init repos // init repos
$platformOverrides = array(); $platformOverrides = array();
if ($composer) { if ($composer) {
@ -378,6 +387,9 @@ EOT
} }
} }
if ('json' === $format) {
$io->write(JsonFile::encode($viewData));
} else {
foreach ($viewData as $type => $packages) { foreach ($viewData as $type => $packages) {
$nameLength = $viewMetaData[$type]['nameLength']; $nameLength = $viewMetaData[$type]['nameLength'];
$versionLength = $viewMetaData[$type]['versionLength']; $versionLength = $viewMetaData[$type]['versionLength'];
@ -410,7 +422,7 @@ EOT
if (!$io->isDecorated()) { if (!$io->isDecorated()) {
$latestVersion = str_replace(array('info', 'highlight', 'comment'), array('=', '!', '~'), $style) . ' ' . $latestVersion; $latestVersion = str_replace(array('info', 'highlight', 'comment'), array('=', '!', '~'), $style) . ' ' . $latestVersion;
} }
$io->write(' <'.$style.'>' . str_pad($latestVersion, $latestLength, ' ') . '</'.$style.'>', false); $io->write(' <' . $style . '>' . str_pad($latestVersion, $latestLength, ' ') . '</' . $style . '>', false);
} }
if (isset($package['description']) && $writeDescription) { if (isset($package['description']) && $writeDescription) {
$description = strtok($package['description'], "\r\n"); $description = strtok($package['description'], "\r\n");
@ -437,6 +449,7 @@ EOT
$io->write(''); $io->write('');
} }
} }
}
return $exitCode; return $exitCode;
} }

Loading…
Cancel
Save