Add an option to get the output in plain, json or print_r syntax

This commit is contained in:
Joas Schilling 2015-04-09 11:45:07 +02:00
parent 56f1ffe820
commit dd5063bd49
4 changed files with 107 additions and 15 deletions

View File

@ -23,15 +23,18 @@
namespace OC\Core\Command\App; 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\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class ListApps extends Command { class ListApps extends Base {
protected function configure() { protected function configure() {
parent::configure();
$this $this
->setName('app:list') ->setName('app:list')
->setDescription('List all available apps'); ->setDescription('List all available apps')
;
} }
protected function execute(InputInterface $input, OutputInterface $output) { protected function execute(InputInterface $input, OutputInterface $output) {
@ -51,13 +54,38 @@ class ListApps extends Command {
sort($enabledApps); sort($enabledApps);
sort($disabledApps); sort($disabledApps);
$output->writeln('Enabled:'); $apps = ['enabled' => [], 'disabled' => []];
foreach ($enabledApps as $app) { 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) { 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;
} }
} }
} }

60
core/command/base.php Normal file
View File

@ -0,0 +1,60 @@
<?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, 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;
}
}
}

View File

@ -3,11 +3,10 @@
namespace OC\Core\Command; namespace OC\Core\Command;
use OCP\IConfig; use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class Check extends Command { class Check extends Base {
/** /**
* @var IConfig * @var IConfig
*/ */
@ -19,6 +18,8 @@ class Check extends Command {
} }
protected function configure() { protected function configure() {
parent::configure();
$this $this
->setName('check') ->setName('check')
->setDescription('check dependencies of the server environment') ->setDescription('check dependencies of the server environment')
@ -28,10 +29,11 @@ class Check extends Command {
protected function execute(InputInterface $input, OutputInterface $output) { protected function execute(InputInterface $input, OutputInterface $output) {
$errors = \OC_Util::checkServer($this->config); $errors = \OC_Util::checkServer($this->config);
if (!empty($errors)) { if (!empty($errors)) {
$errors = array_map( function($items) { $errors = array_map(function($item) {
return (string)$items['error']; return (string) $item['error'];
}, $errors); }, $errors);
echo json_encode($errors);
$this->writeArrayInOutputFormat($input, $output, $errors);
return 1; return 1;
} }
return 0; return 0;

View File

@ -22,12 +22,13 @@
namespace OC\Core\Command; namespace OC\Core\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class Status extends Command { class Status extends Base {
protected function configure() { protected function configure() {
parent::configure();
$this $this
->setName('status') ->setName('status')
->setDescription('show some status information') ->setDescription('show some status information')
@ -41,6 +42,7 @@ class Status extends Command {
'versionstring' => \OC_Util::getVersionString(), 'versionstring' => \OC_Util::getVersionString(),
'edition' => \OC_Util::getEditionString(), 'edition' => \OC_Util::getEditionString(),
); );
print_r($values);
$this->writeArrayInOutputFormat($input, $output, $values);
} }
} }