diff options
author | Morris Jobke <hey@morrisjobke.de> | 2015-04-09 15:46:36 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-04-09 15:46:36 +0200 |
commit | f5a145b410ce8e8002b634264f146c21de346050 (patch) | |
tree | 7fc2993392ec6dfbf193282808e44916eec691b6 /core | |
parent | b9eaa386c4f72fcf6e347a92886897b537ade6e6 (diff) | |
parent | 98bdfa1518da32782f8a906e6643c0cde6c4dfc9 (diff) | |
download | nextcloud-server-f5a145b410ce8e8002b634264f146c21de346050.tar.gz nextcloud-server-f5a145b410ce8e8002b634264f146c21de346050.zip |
Merge pull request #15501 from owncloud/better-output-format-console
Add an option to get the output in plain, json or print_r syntax
Diffstat (limited to 'core')
-rw-r--r-- | core/command/app/listapps.php | 46 | ||||
-rw-r--r-- | core/command/base.php | 62 | ||||
-rw-r--r-- | core/command/check.php | 12 | ||||
-rw-r--r-- | core/command/status.php | 8 |
4 files changed, 110 insertions, 18 deletions
diff --git a/core/command/app/listapps.php b/core/command/app/listapps.php index dbb04c41eed..37a1d645ed4 100644 --- a/core/command/app/listapps.php +++ b/core/command/app/listapps.php @@ -23,21 +23,23 @@ 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) { $apps = \OC_App::getAllApps(); - $enabledApps = array(); - $disabledApps = array(); + $enabledApps = $disabledApps = []; $versions = \OC_App::getAppVersions(); //sort enabled apps above disabled apps @@ -49,15 +51,39 @@ class ListApps extends Command { } } + $apps = ['enabled' => [], 'disabled' => []]; + sort($enabledApps); - sort($disabledApps); - $output->writeln('Enabled:'); foreach ($enabledApps as $app) { - $output->writeln(' - ' . $app . (isset($versions[$app]) ? ' (' . $versions[$app] . ')' : '')); + $apps['enabled'][$app] = (isset($versions[$app])) ? $versions[$app] : ''; } - $output->writeln('Disabled:'); + + sort($disabledApps); foreach ($disabledApps as $app) { - $output->writeln(' - ' . $app . (isset($versions[$app]) ? ' (' . $versions[$app] . ')' : '')); + $apps['disabled'][$app] = (isset($versions[$app])) ? $versions[$app] : ''; + } + + $this->writeAppList($input, $output, $apps); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @param array $items + */ + protected function writeAppList(InputInterface $input, OutputInterface $output, $items) { + switch ($input->getOption('output')) { + case 'plain': + $output->writeln('Enabled:'); + parent::writeArrayInOutputFormat($input, $output, $items['enabled']); + + $output->writeln('Disabled:'); + parent::writeArrayInOutputFormat($input, $output, $items['disabled']); + break; + + default: + parent::writeArrayInOutputFormat($input, $output, $items); + break; } } } diff --git a/core/command/base.php b/core/command/base.php new file mode 100644 index 00000000000..c2d5cf97f02 --- /dev/null +++ b/core/command/base.php @@ -0,0 +1,62 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @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 <http://www.gnu.org/licenses/> + * + */ + +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, json or json_pretty, default is plain)', + 'plain' + ) + ; + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @param array $items + */ + protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items) { + switch ($input->getOption('output')) { + case 'json': + $output->writeln(json_encode($items)); + break; + case 'json_pretty': + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); + 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); } } |