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

[stable10] Improve code occ files_external:list --short #34549

Merged
merged 1 commit into from
Feb 19, 2019
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
249 changes: 109 additions & 140 deletions apps/files_external/lib/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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('[]');
Expand All @@ -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'];

Expand All @@ -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(),
Expand All @@ -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(
"<error>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.</error>"
);
$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(
"<error>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.</error>"
);
}
}

protected function getStorageService($userId) {
if (!empty($userId)) {
$user = $this->userManager->get($userId);
Expand Down
25 changes: 13 additions & 12 deletions apps/files_external/tests/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
<<<EOS
+----------+-------------+----------+
| Mount ID | Mount Point | Type |
+----------+-------------+----------+
| 1 | /ownCloud | Admin |
| 2 | /SFTP | Personal |
+----------+-------------+----------+
+----------+-------------+------+----------+
| Mount ID | Mount Point | Auth | Type |
+----------+-------------+------+----------+
| 1 | /ownCloud | User | Admin |
| 2 | /SFTP | User | Personal |
+----------+-------------+------+----------+

EOS
,
[
['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],
]
],
];
Expand Down Expand Up @@ -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 {
Expand Down