diff --git a/apps/files_external/lib/Command/ListCommand.php b/apps/files_external/lib/Command/ListCommand.php index e682fd929577..b9a005fcb1a6 100644 --- a/apps/files_external/lib/Command/ListCommand.php +++ b/apps/files_external/lib/Command/ListCommand.php @@ -91,6 +91,11 @@ protected function configure() { 'a', InputOption::VALUE_NONE, 'show both system wide mounts and all personal mounts' + )->addOption( + 'short', + 's', + InputOption::VALUE_NONE, + 'show only a reduced mount info' ); parent::configure(); } @@ -119,6 +124,8 @@ protected function execute(InputInterface $input, OutputInterface $output) { */ public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output) { $outputType = $input->getOption('output'); + $shortView = $input->getOption('short'); + if (\count($mounts) === 0) { if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { $output->writeln('[]'); @@ -134,23 +141,28 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output return; } - $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; + if ($shortView) { + $headers = ['Mount ID', 'Mount Point', 'Type']; + } else { + $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; - if (!$userId || $userId === self::ALL) { - $headers[] = 'Applicable Users'; - $headers[] = 'Applicable Groups'; - } - if ($userId === self::ALL) { - $headers[] = 'Type'; - } + if (!$userId || $userId === self::ALL) { + $headers[] = 'Applicable Users'; + $headers[] = 'Applicable Groups'; + } + + if ($userId === self::ALL) { + $headers[] = 'Type'; + } - if (!$input->getOption('show-password')) { - $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; - foreach ($mounts as $mount) { - $config = $mount->getBackendOptions(); - foreach ($config as $key => $value) { - if (\in_array($key, $hideKeys)) { - $mount->setBackendOption($key, '***'); + if (!$input->getOption('show-password')) { + $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; + foreach ($mounts as $mount) { + $config = $mount->getBackendOptions(); + foreach ($config as $key => $value) { + if (\in_array($key, $hideKeys)) { + $mount->setBackendOption($key, '***'); + } } } } @@ -161,31 +173,46 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output return \strtolower(\str_replace(' ', '_', $header)); }, $headers); - $pairs = \array_map(function (IStorageConfig $config) use ($keys, $userId) { - $values = [ - $config->getId(), - $config->getMountPoint(), - $config->getBackend()->getStorageClass(), - $config->getAuthMechanism()->getIdentifier(), - $config->getBackendOptions(), - $config->getMountOptions() - ]; - if (!$userId || $userId === self::ALL) { - $values[] = $config->getApplicableUsers(); - $values[] = $config->getApplicableGroups(); - } - if ($userId === self::ALL) { - $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal'; - } + if ($shortView) { + $pairs = \array_map(function (IStorageConfig $config) use ($keys) { + $values = [ + $config->getId(), + $config->getMountPoint(), + $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal' + ]; + + return \array_combine($keys, $values); + }, $mounts); + } else { + $pairs = \array_map(function (IStorageConfig $config) use ($keys, $userId) { + $values = [ + $config->getId(), + $config->getMountPoint(), + $config->getBackend()->getStorageClass(), + $config->getAuthMechanism()->getIdentifier(), + $config->getBackendOptions(), + $config->getMountOptions() + ]; + if (!$userId || $userId === self::ALL) { + $values[] = $config->getApplicableUsers(); + $values[] = $config->getApplicableGroups(); + } + if ($userId === self::ALL) { + $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal'; + } + + return \array_combine($keys, $values); + }, $mounts); + } - return \array_combine($keys, $values); - }, $mounts); if ($outputType === self::OUTPUT_FORMAT_JSON) { $output->writeln(\json_encode(\array_values($pairs))); } else { $output->writeln(\json_encode(\array_values($pairs), JSON_PRETTY_PRINT)); } } else { + + // default output style $full = $input->getOption('full'); $defaultMountOptions = [ 'encrypt' => true, @@ -195,7 +222,9 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output 'encoding_compatibility' => false ]; $countInvalid = 0; - $rows = \array_map(function (IStorageConfig $config) use ($userId, $defaultMountOptions, $full, &$countInvalid) { + // In case adding array elements, add them only after the first two (Mount ID / Mount Point) + // and before the last one entry (Type). Necessary for option -s + $rows = \array_map(function (IStorageConfig $config) use ($shortView, $userId, $defaultMountOptions, $full, &$countInvalid) { if ($config->getBackend() instanceof InvalidBackend || $config->getAuthMechanism() instanceof InvalidAuth) { $countInvalid++; } @@ -251,7 +280,8 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output $values[] = $applicableUsers; $values[] = $applicableGroups; } - if ($userId === self::ALL) { + // This MUST stay the last entry + if ($shortView || $userId === self::ALL) { $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; } @@ -260,7 +290,7 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output $table = new Table($output); $table->setHeaders($headers); - $table->setRows($rows); + $table->setRows($this->getColumns($shortView, $rows)); $table->render(); if ($countInvalid > 0) { @@ -273,6 +303,32 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output } } + // Removes all unused columns for option -s. + // Only the first two (Mount ID / Mount Point) and the last column (Mount Type) is kept. + protected function getColumns($shortView, $rows) { + if ($shortView) { + $newRows = []; + // $subArr is a copy of $rows anyways... + foreach ($rows as $subArr) { + $c = \count($subArr) - 1; + $u = false; + foreach ($subArr as $key => $val) { + if ((int)$key > 1 && (int)$key < $c) { + unset($subArr[$key]); + $u = true; + } + } + if ($u) { + $subArr = \array_values($subArr); + } + $newRows[] = $subArr; + } + return $newRows; + } else { + return $rows; + } + } + protected function getStorageService($userId) { if (!empty($userId)) { $user = $this->userManager->get($userId); diff --git a/apps/files_external/tests/Command/ListCommandTest.php b/apps/files_external/tests/Command/ListCommandTest.php index 8cc329f41d27..c4eaa8415bb5 100644 --- a/apps/files_external/tests/Command/ListCommandTest.php +++ b/apps/files_external/tests/Command/ListCommandTest.php @@ -90,7 +90,82 @@ public function testDisplayWarningForIncomplete() { $instance->listMounts('', [$mount1, $mount2], $input, $output); $output = $output->fetch(); - $lines = \explode($output, "\n"); $this->assertRegexp('/Number of invalid storages found/', $output); } + + public function providesShortView() { + return [ + [ + ['short' => true, 'output' => 'json'], + [ + ['mount_id' => 1, 'mount_point' => '/ownCloud', 'type' => 'admin'], + ['mount_id' => 2, 'mount_point' => '/SFTP', 'type' => 'personal'], + ], + [ + ['mount_id' => 1, 'mount_point' => '/ownCloud', 'type' => StorageConfig::MOUNT_TYPE_ADMIN], + ['mount_id' => 2, 'mount_point' => '/SFTP', 'type' => StorageConfig::MOUNT_TYPE_PERSONAl], + ] + ], + [ + ['short' => true], + << 1, 'mount_point' => '/ownCloud', 'type' => StorageConfig::MOUNT_TYPE_ADMIN], + ['mount_id' => 2, 'mount_point' => '/SFTP', 'type' => StorageConfig::MOUNT_TYPE_PERSONAl], + ] + ], + ]; + } + + /** + * @dataProvider providesShortView + * @param $options + * @param $expectedResult + * @param $mountOptions + */ + public function testShortView($options, $expectedResult, $mountOptions) { + $l10n = $this->createMock('\OCP\IL10N', null, [], '', false); + $session = $this->createMock('\OCP\ISession'); + $crypto = $this->createMock('\OCP\Security\ICrypto'); + $instance = $this->getInstance(); + // FIXME: use mock of IStorageConfig + $mount1 = new StorageConfig(); + $mount1->setId($mountOptions[0]['mount_id']); + $mount1->setMountPoint($mountOptions[0]['mount_point']); + $mount1->setType($mountOptions[0]['type']); + $mount1->setAuthMechanism(new Password()); + $mount1->setBackend(new Local($l10n, new NullMechanism())); + $mount2 = new StorageConfig(); + $mount2->setId($mountOptions[1]['mount_id']); + $mount2->setMountPoint($mountOptions[1]['mount_point']); + $mount2->setType($mountOptions[1]['type']); + $mount2->setAuthMechanism(new SessionCredentials($session, $crypto)); + $mount2->setBackend(new Local($l10n, new NullMechanism())); + $input = $this->getInput($instance, ['user_id' => 'user1'], $options); + $output = new BufferedOutput(); + + $instance->listMounts('user1', [$mount1, $mount2], $input, $output); + $output = $output->fetch(); + if (isset($options['output']) && ($options['output'] === 'json')) { + $results = \json_decode($output, true); + $countResults = \count($results); + + for ($i = 0; $i < $countResults; $i++) { + $this->assertEquals($expectedResult[$i]['mount_id'], $results[$i]['mount_id']); + $this->assertEquals($expectedResult[$i]['mount_point'], $results[$i]['mount_point']); + $this->assertEquals($expectedResult[$i]['type'], $results[$i]['type']); + } + } else { + $this->assertEquals($expectedResult, $output); + } + } }