]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add files_external:export command
authorRobin Appelman <icewind@owncloud.com>
Mon, 18 Jan 2016 10:35:34 +0000 (11:35 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Mon, 18 Jan 2016 13:17:27 +0000 (14:17 +0100)
apps/files_external/appinfo/register_command.php
apps/files_external/command/export.php [new file with mode: 0644]
apps/files_external/command/listcommand.php

index 12bf6a45df386d1a9efac171870ca8d64a51be87..be32cd410f8b8715ba4dc1e5fc4f307c3b6fbf6c 100644 (file)
@@ -23,7 +23,8 @@
 use OCA\Files_External\Command\ListCommand;
 use OCA\Files_External\Command\Config;
 use OCA\Files_External\Command\Option;
-use \OCA\Files_External\Command\Import;
+use OCA\Files_External\Command\Import;
+use OCA\Files_External\Command\Export;
 
 $userManager = OC::$server->getUserManager();
 $userSession = OC::$server->getUserSession();
@@ -40,3 +41,4 @@ $application->add(new ListCommand($globalStorageService, $userStorageService, $u
 $application->add(new Config($globalStorageService));
 $application->add(new Option($globalStorageService));
 $application->add(new Import($globalStorageService, $userStorageService, $userSession, $userManager, $importLegacyStorageService, $backendService));
+$application->add(new Export($globalStorageService, $userStorageService, $userSession, $userManager));
diff --git a/apps/files_external/command/export.php b/apps/files_external/command/export.php
new file mode 100644 (file)
index 0000000..371061b
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@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 OCA\Files_External\Command;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\Table;
+use Symfony\Component\Console\Helper\TableHelper;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\Input;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Export extends ListCommand {
+
+       protected function configure() {
+               $this
+                       ->setName('files_external:export')
+                       ->setDescription('Export mount configurations')
+                       ->addArgument(
+                               'user_id',
+                               InputArgument::OPTIONAL,
+                               'user id to export the personal mounts for, if no user is provided admin mounts will be exported'
+                       );
+       }
+
+       protected function execute(InputInterface $input, OutputInterface $output) {
+               $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
+               $listInput = new ArrayInput([], $listCommand->getDefinition());
+               $listInput->setArgument('user_id', $input->getArgument('user_id'));
+               $listInput->setOption('output', 'json_pretty');
+               $listInput->setOption('show-password', true);
+               $listInput->setOption('full', true);
+               $listCommand->execute($listInput, $output);
+       }
+}
index f55fb464c83783cca4e5152c4269dee8341ad22b..c978ae5cfcb42a932123794d5f055c62b811538b 100644 (file)
@@ -22,6 +22,7 @@
 namespace OCA\Files_External\Command;
 
 use OC\Core\Command\Base;
+use OC\User\NoUserException;
 use OCA\Files_external\Lib\StorageConfig;
 use OCA\Files_external\Service\GlobalStoragesService;
 use OCA\Files_external\Service\UserStoragesService;
@@ -38,22 +39,22 @@ class ListCommand extends Base {
        /**
         * @var GlobalStoragesService
         */
-       private $globalService;
+       protected $globalService;
 
        /**
         * @var UserStoragesService
         */
-       private $userService;
+       protected $userService;
 
        /**
         * @var IUserSession
         */
-       private $userSession;
+       protected $userSession;
 
        /**
         * @var IUserManager
         */
-       private $userManager;
+       protected $userManager;
 
        function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
                parent::__construct();
@@ -87,17 +88,7 @@ class ListCommand extends Base {
 
        protected function execute(InputInterface $input, OutputInterface $output) {
                $userId = $input->getArgument('user_id');
-               if (!empty($userId)) {
-                       $user = $this->userManager->get($userId);
-                       if (is_null($user)) {
-                               $output->writeln("<error>user $userId not found</error>");
-                               return;
-                       }
-                       $this->userSession->setUser($user);
-                       $storageService = $this->userService;
-               } else {
-                       $storageService = $this->globalService;
-               }
+               $storageService = $this->getStorageService($userId);
 
                /** @var  $mounts StorageConfig[] */
                $mounts = $storageService->getAllStorages();
@@ -112,11 +103,16 @@ class ListCommand extends Base {
         * @param OutputInterface $output
         */
        public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output){
+               $outputType = $input->getOption('output');
                if (count($mounts) === 0) {
-                       if ($userId) {
-                               $output->writeln("<info>No mounts configured by $userId</info>");
+                       if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
+                               $output->writeln('[]');
                        } else {
-                               $output->writeln("<info>No admin mounts configured</info>");
+                               if ($userId) {
+                                       $output->writeln("<info>No mounts configured by $userId</info>");
+                               } else {
+                                       $output->writeln("<info>No admin mounts configured</info>");
+                               }
                        }
                        return;
                }
@@ -140,7 +136,6 @@ class ListCommand extends Base {
                        }
                }
 
-               $outputType = $input->getOption('output');
                if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
                        $keys = array_map(function ($header) {
                                return strtolower(str_replace(' ', '_', $header));
@@ -237,4 +232,17 @@ class ListCommand extends Base {
                        $table->render();
                }
        }
+
+       protected function getStorageService($userId) {
+               if (!empty($userId)) {
+                       $user = $this->userManager->get($userId);
+                       if (is_null($user)) {
+                               throw new NoUserException("user $userId not found");
+                       }
+                       $this->userSession->setUser($user);
+                       return $this->userService;
+               } else {
+                       return $this->globalService;
+               }
+       }
 }