From: Joas Schilling Date: Thu, 9 Apr 2015 09:45:07 +0000 (+0200) Subject: Add an option to get the output in plain, json or print_r syntax X-Git-Tag: v8.1.0alpha1~50^2~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dd5063bd491f26c514e3206a167c2888d6bc3310;p=nextcloud-server.git Add an option to get the output in plain, json or print_r syntax --- diff --git a/core/command/app/listapps.php b/core/command/app/listapps.php index dbb04c41eed..8a790b56284 100644 --- a/core/command/app/listapps.php +++ b/core/command/app/listapps.php @@ -23,15 +23,18 @@ namespace OC\Core\Command\App; -use Symfony\Component\Console\Command\Command; +use OC\Core\Command\Base; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class ListApps extends Command { +class ListApps extends Base { protected function configure() { + parent::configure(); + $this ->setName('app:list') - ->setDescription('List all available apps'); + ->setDescription('List all available apps') + ; } protected function execute(InputInterface $input, OutputInterface $output) { @@ -51,13 +54,38 @@ class ListApps extends Command { sort($enabledApps); sort($disabledApps); - $output->writeln('Enabled:'); + $apps = ['enabled' => [], 'disabled' => []]; foreach ($enabledApps as $app) { - $output->writeln(' - ' . $app . (isset($versions[$app]) ? ' (' . $versions[$app] . ')' : '')); + if (isset($versions[$app])) { + $apps['enabled'][$app] = $versions[$app]; + } else { + $apps['enabled'][$app] = true; + } } - $output->writeln('Disabled:'); + foreach ($disabledApps as $app) { - $output->writeln(' - ' . $app . (isset($versions[$app]) ? ' (' . $versions[$app] . ')' : '')); + if (isset($versions[$app])) { + $apps['disabled'][$app] = $versions[$app]; + } else { + $apps['disabled'][$app] = false; + } + } + $this->writeArrayInOutputFormat($input, $output, $apps); + } + + protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items) { + $outputFormat = $input->getOption('output'); + switch ($outputFormat) { + case 'json': + case 'print': + parent::writeArrayInOutputFormat($input, $output, $items); + break; + default: + $output->writeln('Enabled:'); + parent::writeArrayInOutputFormat($input, $output, $items['enabled']); + $output->writeln('Disabled:'); + parent::writeArrayInOutputFormat($input, $output, $items['disabled']); + break; } } } diff --git a/core/command/base.php b/core/command/base.php new file mode 100644 index 00000000000..7e139636b63 --- /dev/null +++ b/core/command/base.php @@ -0,0 +1,60 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Core\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Base extends Command { + protected function configure() { + $this + ->addOption( + 'output', + null, + InputOption::VALUE_OPTIONAL, + 'Output format (plain, print or json, default is plain)', + 'plain' + ) + ; + } + + protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items) { + $outputFormat = $input->getOption('output'); + switch ($outputFormat) { + case 'json': + case 'print': + if ($outputFormat === 'json') { + $output->writeln(json_encode($items)); + } else { + print_r($items); + } + break; + default: + foreach ($items as $key => $item) { + $output->writeln(' - ' . (!is_int($key) ? $key . ': ' : '') . $item); + } + break; + } + } +} diff --git a/core/command/check.php b/core/command/check.php index 9d0db3a726c..ddfe9b73bba 100644 --- a/core/command/check.php +++ b/core/command/check.php @@ -3,11 +3,10 @@ namespace OC\Core\Command; use OCP\IConfig; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class Check extends Command { +class Check extends Base { /** * @var IConfig */ @@ -19,6 +18,8 @@ class Check extends Command { } protected function configure() { + parent::configure(); + $this ->setName('check') ->setDescription('check dependencies of the server environment') @@ -28,10 +29,11 @@ class Check extends Command { protected function execute(InputInterface $input, OutputInterface $output) { $errors = \OC_Util::checkServer($this->config); if (!empty($errors)) { - $errors = array_map( function($items) { - return (string)$items['error']; + $errors = array_map(function($item) { + return (string) $item['error']; }, $errors); - echo json_encode($errors); + + $this->writeArrayInOutputFormat($input, $output, $errors); return 1; } return 0; diff --git a/core/command/status.php b/core/command/status.php index 8c6653a8910..3859f69febc 100644 --- a/core/command/status.php +++ b/core/command/status.php @@ -22,12 +22,13 @@ namespace OC\Core\Command; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class Status extends Command { +class Status extends Base { protected function configure() { + parent::configure(); + $this ->setName('status') ->setDescription('show some status information') @@ -41,6 +42,7 @@ class Status extends Command { 'versionstring' => \OC_Util::getVersionString(), 'edition' => \OC_Util::getEditionString(), ); - print_r($values); + + $this->writeArrayInOutputFormat($input, $output, $values); } }