Merge pull request #15501 from owncloud/better-output-format-console

Add an option to get the output in plain, json or print_r syntax
remotes/origin/poc-doctrine-migrations
Morris Jobke 10 years ago
commit f5a145b410
  1. 46
      core/command/app/listapps.php
  2. 62
      core/command/base.php
  3. 12
      core/command/check.php
  4. 8
      core/command/status.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;
}
}
}

@ -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;
}
}
}

@ -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;

@ -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);
}
}

Loading…
Cancel
Save