From d4aafea0c3ed1ee4270644dc931664f60e41dd05 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 19 Feb 2019 09:27:51 +0100 Subject: [PATCH] [stable10] Merge pull request #34033 from owncloud/improve_short Improve code occ files_external:list --short --- .../lib/Command/ListCommand.php | 249 ++++++++---------- .../tests/Command/ListCommandTest.php | 25 +- 2 files changed, 122 insertions(+), 152 deletions(-) diff --git a/apps/files_external/lib/Command/ListCommand.php b/apps/files_external/lib/Command/ListCommand.php index b9a005fcb1a6..5aaed05b42de 100644 --- a/apps/files_external/lib/Command/ListCommand.php +++ b/apps/files_external/lib/Command/ListCommand.php @@ -75,27 +75,27 @@ protected function configure() { ->addArgument( 'user_id', InputArgument::OPTIONAL, - 'user id to list the personal mounts for, if no user is provided admin mounts will be listed' + 'User id to list the personal mounts for, if no user is provided admin mounts will be listed' )->addOption( 'show-password', null, InputOption::VALUE_NONE, - 'show passwords and secrets' + 'Show passwords and secrets' )->addOption( 'full', null, InputOption::VALUE_NONE, - 'don\'t truncate long values in table output' + 'Don\'t truncate long values in table output' )->addOption( 'all', 'a', InputOption::VALUE_NONE, - 'show both system wide mounts and all personal mounts' + 'Show mounts for all users. All has precedence over user_id' )->addOption( 'short', 's', InputOption::VALUE_NONE, - 'show only a reduced mount info' + 'Show only a reduced mount info' ); parent::configure(); } @@ -126,6 +126,7 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output $outputType = $input->getOption('output'); $shortView = $input->getOption('short'); + // check if there are any mounts present if (\count($mounts) === 0) { if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { $output->writeln('[]'); @@ -141,8 +142,14 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output return; } + // set minimum columns used based on options if ($shortView) { - $headers = ['Mount ID', 'Mount Point', 'Type']; + $headers = ['Mount ID', 'Mount Point', 'Auth', 'Type']; + // if there is no userId or option --all is set, insert additional columns + if (!$userId || $userId === self::ALL) { + \array_splice($headers, 2, 0, 'Applicable Users'); + \array_splice($headers, 3, 0, 'Applicable Groups'); + } } else { $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; @@ -168,100 +175,63 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output } } - if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { - $keys = \array_map(function ($header) { - return \strtolower(\str_replace(' ', '_', $header)); - }, $headers); - - 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); + // default output style + $full = $input->getOption('full'); + $defaultMountOptions = [ + 'encrypt' => true, + 'previews' => true, + 'filesystem_check_changes' => 1, + 'enable_sharing' => false, + 'encoding_compatibility' => false + ]; + $countInvalid = 0; + // 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++; } - - if ($outputType === self::OUTPUT_FORMAT_JSON) { - $output->writeln(\json_encode(\array_values($pairs))); - } else { - $output->writeln(\json_encode(\array_values($pairs), JSON_PRETTY_PRINT)); + $storageConfig = $config->getBackendOptions(); + $keys = \array_keys($storageConfig); + $values = \array_values($storageConfig); + + if (!$full) { + $values = \array_map(function ($value) { + if (\is_string($value) && \strlen($value) > 32) { + return \substr($value, 0, 6) . '...' . \substr($value, -6, 6); + } else { + return $value; + } + }, $values); } - } else { - // default output style - $full = $input->getOption('full'); - $defaultMountOptions = [ - 'encrypt' => true, - 'previews' => true, - 'filesystem_check_changes' => 1, - 'enable_sharing' => false, - 'encoding_compatibility' => false - ]; - $countInvalid = 0; - // 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++; - } - $storageConfig = $config->getBackendOptions(); - $keys = \array_keys($storageConfig); - $values = \array_values($storageConfig); + $configStrings = \array_map(function ($key, $value) { + return $key . ': ' . \json_encode($value); + }, $keys, $values); + $configString = \implode(', ', $configStrings); - if (!$full) { - $values = \array_map(function ($value) { - if (\is_string($value) && \strlen($value) > 32) { - return \substr($value, 0, 6) . '...' . \substr($value, -6, 6); - } else { - return $value; - } - }, $values); + $mountOptions = $config->getMountOptions(); + // hide defaults + foreach ($mountOptions as $key => $value) { + if ($value === $defaultMountOptions[$key]) { + unset($mountOptions[$key]); } + } + $keys = \array_keys($mountOptions); + $values = \array_values($mountOptions); - $configStrings = \array_map(function ($key, $value) { - return $key . ': ' . \json_encode($value); - }, $keys, $values); - $configString = \implode(', ', $configStrings); - - $mountOptions = $config->getMountOptions(); - // hide defaults - foreach ($mountOptions as $key => $value) { - if ($value === $defaultMountOptions[$key]) { - unset($mountOptions[$key]); - } - } - $keys = \array_keys($mountOptions); - $values = \array_values($mountOptions); - - $optionsStrings = \array_map(function ($key, $value) { - return $key . ': ' . \json_encode($value); - }, $keys, $values); - $optionsString = \implode(', ', $optionsStrings); + $optionsStrings = \array_map(function ($key, $value) { + return $key . ': ' . \json_encode($value); + }, $keys, $values); + $optionsString = \implode(', ', $optionsStrings); + // output dependent on option shortview + if ($shortView) { + $values = [ + $config->getId(), + $config->getMountPoint() + ]; + } else { $values = [ $config->getId(), $config->getMountPoint(), @@ -270,65 +240,64 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output $configString, $optionsString ]; + } - if (!$userId || $userId === self::ALL) { - $applicableUsers = \implode(', ', $config->getApplicableUsers()); - $applicableGroups = \implode(', ', $config->getApplicableGroups()); - if ($applicableUsers === '' && $applicableGroups === '') { - $applicableUsers = 'All'; - } - $values[] = $applicableUsers; - $values[] = $applicableGroups; + // output independent on option shortview + if (!$userId || $userId === self::ALL) { + $applicableUsers = \implode(', ', $config->getApplicableUsers()); + $applicableGroups = \implode(', ', $config->getApplicableGroups()); + if ($applicableUsers === '' && $applicableGroups === '') { + $applicableUsers = 'All'; } - // This MUST stay the last entry - if ($shortView || $userId === self::ALL) { - $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; + $values[] = $applicableUsers; + $values[] = $applicableGroups; + } + // This MUST stay the last entry + if ($shortView || $userId === self::ALL) { + // query the auth type + if (\stristr($config->getBackend()->getText(), 'session') === true) { + $values[] = 'Session'; + } else { + $values[] = 'User'; } + // get the mount type + $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; + } - return $values; - }, $mounts); + return $values; + }, $mounts); - $table = new Table($output); - $table->setHeaders($headers); - $table->setRows($this->getColumns($shortView, $rows)); - $table->render(); + if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { + $keys = \array_map(function ($header) { + return \strtolower(\str_replace(' ', '_', $header)); + }, $headers); - if ($countInvalid > 0) { - $output->writeln( - "Number of invalid storages found: $countInvalid.\n" . - "The listed configuration details are likely incomplete.\n" . - "Please make sure that all related apps that provide these storages are enabled or delete these." - ); + $pairs = []; + foreach ($rows as $array_1) { + $pairs[] = \array_combine($keys, $array_1); } - } - } - // 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; + if ($outputType === self::OUTPUT_FORMAT_JSON) { + $output->writeln(\json_encode(\array_values($pairs))); + } else { + $output->writeln(\json_encode(\array_values($pairs), JSON_PRETTY_PRINT)); } - return $newRows; } else { - return $rows; + $table = new Table($output); + $table->setHeaders($headers); + $table->setRows($rows); + $table->render(); + } + + if ($countInvalid > 0) { + $output->writeln( + "Number of invalid storages found: $countInvalid.\n" . + "The listed configuration details are likely incomplete.\n" . + "Please make sure that all related apps that provide these storages are enabled or delete these." + ); } } - + 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 c4eaa8415bb5..64dae4ed6d7e 100644 --- a/apps/files_external/tests/Command/ListCommandTest.php +++ b/apps/files_external/tests/Command/ListCommandTest.php @@ -98,29 +98,29 @@ public function providesShortView() { [ ['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', 'auth' => 'User', 'type' => 'Admin'], + ['mount_id' => 2, 'mount_point' => '/SFTP', 'auth' => 'User', 'type' => 'Personal'], ], [ - ['mount_id' => 1, 'mount_point' => '/ownCloud', 'type' => StorageConfig::MOUNT_TYPE_ADMIN], - ['mount_id' => 2, 'mount_point' => '/SFTP', 'type' => StorageConfig::MOUNT_TYPE_PERSONAl], + ['mount_id' => 1, 'mount_point' => '/ownCloud', 'auth' => 'User', 'type' => StorageConfig::MOUNT_TYPE_ADMIN], + ['mount_id' => 2, 'mount_point' => '/SFTP', 'auth' => 'User', '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], + ['mount_id' => 1, 'mount_point' => '/ownCloud', 'auth' => 'User', 'type' => StorageConfig::MOUNT_TYPE_ADMIN], + ['mount_id' => 2, 'mount_point' => '/SFTP', 'auth' => 'User', 'type' => StorageConfig::MOUNT_TYPE_PERSONAl], ] ], ]; @@ -162,6 +162,7 @@ public function testShortView($options, $expectedResult, $mountOptions) { 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->assertContains($results[$i]['auth'], 'UserSession', true); $this->assertEquals($expectedResult[$i]['type'], $results[$i]['type']); } } else {