Skip to content

Commit

Permalink
show a short list of mount infos: files_external:list --short
Browse files Browse the repository at this point in the history
  • Loading branch information
mmattel authored and phil-davis committed Aug 7, 2018
1 parent 1ab5846 commit 0bb4c3a
Showing 1 changed file with 93 additions and 37 deletions.
130 changes: 93 additions & 37 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,58 +141,78 @@ 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, '***');
}
}
}
}
}

if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
$keys = \array_map(function ($header) {
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, $userId) {
$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

0 comments on commit 0bb4c3a

Please sign in to comment.