Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show a short list of mount infos: files_external:list --short #32178

Merged
merged 2 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 92 additions & 36 deletions apps/files_external/lib/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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('[]');
Expand All @@ -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, '***');
}
}
}
}
Expand All @@ -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,
Expand All @@ -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++;
}
Expand Down Expand Up @@ -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';
}

Expand All @@ -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) {
Expand All @@ -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);
Expand Down
77 changes: 76 additions & 1 deletion apps/files_external/tests/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
<<<EOS
+----------+-------------+----------+
| Mount ID | Mount Point | Type |
+----------+-------------+----------+
| 1 | /ownCloud | Admin |
| 2 | /SFTP | Personal |
+----------+-------------+----------+

EOS
,
[
['mount_id' => 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);
}
}
}