Skip to content

Commit a82b08a

Browse files
committed
Add --format=json option to status command
1 parent 03424d3 commit a82b08a

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/Command/Process/Action/ReviewAppliedAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function ($patch) {
7373
$appliedOptionalPatches
7474
);
7575
$totalNumber = count(array_unique(array_merge($ids, $patchFilter)));
76-
if ($totalNumber >= self::UPGRADE_THRESHOLD) {
76+
if ($totalNumber >= self::UPGRADE_THRESHOLD && $input->getOption('format') !== 'json') {
7777
$message = 'Warning for those on a previous minor line! Magento recommends installing a limited'.
7878
' number of quality patches to ensure a smooth upgrade to the latest line. Please begin planning'.
7979
' an upgrade to the latest release line.';

src/Command/Process/Renderer.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ public function printTable(OutputInterface $output, array $patchList)
105105
$table->render();
106106
}
107107

108+
/**
109+
* Renders patches list as JSON.
110+
*
111+
* @param OutputInterface $output
112+
* @param AggregatedPatchInterface[] $patchList
113+
* @return void
114+
*/
115+
public function printJson(OutputInterface $output, array $patchList)
116+
{
117+
$rows = [];
118+
foreach ($patchList as $patch) {
119+
$rows[] = $this->createJsonRow($patch);
120+
}
121+
122+
usort($rows, function ($a, $b) {
123+
if ($a[self::STATUS] === $b[self::STATUS]) {
124+
return strcmp($a[self::ORIGIN], $b[self::ORIGIN]);
125+
}
126+
return strcmp($a[self::STATUS], $b[self::STATUS]);
127+
});
128+
129+
$output->writeln(json_encode($rows, JSON_PRETTY_PRINT));
130+
}
131+
108132
/**
109133
* Print patch info.
110134
*
@@ -209,6 +233,17 @@ function ($item) {
209233
];
210234
}
211235

236+
/**
237+
* Creates JSON row.
238+
*
239+
* @param AggregatedPatchInterface $patch
240+
* @return array
241+
*/
242+
private function createJsonRow(AggregatedPatchInterface $patch): array
243+
{
244+
return array_map('strip_tags', $this->createRow($patch));
245+
}
246+
212247
/**
213248
* Adds table separator.
214249
*

src/Command/Process/ShowStatus.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CloudPatches\Command\Process;
99

10+
use Laminas\View\Renderer\JsonRenderer;
1011
use Magento\CloudPatches\Command\Process\Action\ReviewAppliedAction;
1112
use Magento\CloudPatches\Console\QuestionFactory;
1213
use Magento\CloudPatches\Patch\Data\AggregatedPatch;
@@ -18,6 +19,7 @@
1819
use Symfony\Component\Console\Helper\QuestionHelper;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\HttpFoundation\JsonResponse;
2123

2224
/**
2325
* Show information about available patches and their statuses.
@@ -103,15 +105,18 @@ public function __construct(
103105
*/
104106
public function run(InputInterface $input, OutputInterface $output)
105107
{
106-
$this->printDetailsInfo($output);
108+
if ($input->getOption('format') !== 'json') {
109+
$this->printDetailsInfo($output);
110+
}
107111

108112
$this->reviewAppliedAction->execute($input, $output, []);
109113

110114
$patches = $this->aggregator->aggregate(
111115
array_merge($this->optionalPool->getList(), $this->localPool->getList())
112116
);
117+
113118
foreach ($patches as $patch) {
114-
if ($patch->isDeprecated() && $this->isPatchVisible($patch)) {
119+
if ($patch->isDeprecated() && $this->isPatchVisible($patch) && $input->getOption('format') !== 'json') {
115120
$this->printDeprecatedWarning($output, $patch);
116121
}
117122
}
@@ -124,7 +129,11 @@ public function run(InputInterface $input, OutputInterface $output)
124129
$patches = $this->filterByPatchCategory($input, $output, $patches);
125130
}
126131

127-
$this->renderer->printTable($output, array_values($patches));
132+
if ($input->getOption('format') === 'json') {
133+
$this->renderer->printJson($output, array_values($patches));
134+
} else {
135+
$this->renderer->printTable($output, array_values($patches));
136+
}
128137
}
129138

130139
/**

src/Command/Status.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Magento\CloudPatches\Command\Process\ShowStatus;
1212
use Magento\CloudPatches\Composer\MagentoVersion;
1313
use Psr\Log\LoggerInterface;
14+
use Symfony\Component\Console\Input\InputArgument;
1415
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
1517
use Symfony\Component\Console\Output\OutputInterface;
1618

1719
/**
@@ -59,7 +61,8 @@ public function __construct(
5961
protected function configure()
6062
{
6163
$this->setName(self::NAME)
62-
->setDescription('Shows the list of available patches and their statuses');
64+
->setDescription('Shows the list of available patches and their statuses')
65+
->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format', 'table');
6366

6467
parent::configure();
6568
}
@@ -71,7 +74,9 @@ public function execute(InputInterface $input, OutputInterface $output)
7174
{
7275
try {
7376
$this->showStatus->run($input, $output);
74-
$output->writeln('<info>' . $this->magentoVersion->get() . '</info>');
77+
if ($input->getOption('format') !== 'json') {
78+
$output->writeln('<info>' . $this->magentoVersion->get() . '</info>');
79+
}
7580
} catch (RuntimeException $e) {
7681
$output->writeln('<error>' . $e->getMessage() . '</error>');
7782
$this->logger->error($e->getMessage());

0 commit comments

Comments
 (0)