]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add an option to get the output in plain, json or print_r syntax
authorJoas Schilling <nickvergessen@owncloud.com>
Thu, 9 Apr 2015 09:45:07 +0000 (11:45 +0200)
committerJoas Schilling <nickvergessen@owncloud.com>
Thu, 9 Apr 2015 09:46:52 +0000 (11:46 +0200)
core/command/app/listapps.php
core/command/base.php [new file with mode: 0644]
core/command/check.php
core/command/status.php

index dbb04c41eed11da480ca8af9d4243fb51b56696b..8a790b56284b3b123955e9898956971349f4d285 100644 (file)
 
 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 (file)
index 0000000..7e13963
--- /dev/null
@@ -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;
+               }
+       }
+}
index 9d0db3a726c387e8ffab799a64653c34be581b8a..ddfe9b73bbaab201892facbce946b15d0b2d8d93 100644 (file)
@@ -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;
index 8c6653a8910c2c5583a70bb14bf141814602d2c1..3859f69febccafb56cf18c39c85fcd0c5ef4face 100644 (file)
 
 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);
        }
 }