|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files\Command\Mount; |
| 10 | + |
| 11 | +use OCP\Files\Config\ICachedMountInfo; |
| 12 | +use OCP\Files\Config\IMountProviderCollection; |
| 13 | +use OCP\Files\Config\IUserMountCache; |
| 14 | +use OCP\Files\Mount\IMountPoint; |
| 15 | +use OCP\IUserManager; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class ListMounts extends Command { |
| 22 | + public function __construct( |
| 23 | + private readonly IUserManager $userManager, |
| 24 | + private readonly IUserMountCache $userMountCache, |
| 25 | + private readonly IMountProviderCollection $mountProviderCollection, |
| 26 | + ) { |
| 27 | + parent::__construct(); |
| 28 | + } |
| 29 | + |
| 30 | + protected function configure(): void { |
| 31 | + $this |
| 32 | + ->setName('files:mount:list') |
| 33 | + ->setDescription('List of mounts for a user') |
| 34 | + ->addArgument('user', InputArgument::REQUIRED, 'User to list mounts for'); |
| 35 | + } |
| 36 | + |
| 37 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 38 | + $userId = $input->getArgument('user'); |
| 39 | + $user = $this->userManager->get($userId); |
| 40 | + if (!$user) { |
| 41 | + $output->writeln("<error>User $userId not found</error>"); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + $mounts = $this->mountProviderCollection->getMountsForUser($user); |
| 46 | + $mounts[] = $this->mountProviderCollection->getHomeMountForUser($user); |
| 47 | + /** @var array<string, IMountPoint> $cachedByMountpoint */ |
| 48 | + $mountsByMountpoint = array_combine(array_map(fn (IMountPoint $mount) => $mount->getMountPoint(), $mounts), $mounts); |
| 49 | + usort($mounts, fn (IMountPoint $a, IMountPoint $b) => $a->getMountPoint() <=> $b->getMountPoint()); |
| 50 | + |
| 51 | + $cachedMounts = $this->userMountCache->getMountsForUser($user); |
| 52 | + usort($cachedMounts, fn (ICachedMountInfo $a, ICachedMountInfo $b) => $a->getMountPoint() <=> $b->getMountPoint()); |
| 53 | + /** @var array<string, ICachedMountInfo> $cachedByMountpoint */ |
| 54 | + $cachedByMountpoint = array_combine(array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts), $cachedMounts); |
| 55 | + |
| 56 | + foreach ($mounts as $mount) { |
| 57 | + $output->writeln('<info>' . $mount->getMountPoint() . '</info>: ' . $mount->getStorageId()); |
| 58 | + if (isset($cachedByMountpoint[$mount->getMountPoint()])) { |
| 59 | + $cached = $cachedByMountpoint[$mount->getMountPoint()]; |
| 60 | + $output->writeln("\t- provider: " . $cached->getMountProvider()); |
| 61 | + $output->writeln("\t- storage id: " . $cached->getStorageId()); |
| 62 | + $output->writeln("\t- root id: " . $cached->getRootId()); |
| 63 | + } else { |
| 64 | + $output->writeln("\t<error>not registered</error>"); |
| 65 | + } |
| 66 | + } |
| 67 | + foreach ($cachedMounts as $cachedMount) { |
| 68 | + if (!isset($mountsByMountpoint[$cachedMount->getMountPoint()])) { |
| 69 | + $output->writeln('<info>' . $cachedMount->getMountPoint() . '</info>:'); |
| 70 | + $output->writeln("\t<error>registered but no longer provided</error>"); |
| 71 | + $output->writeln("\t- provider: " . $cachedMount->getMountProvider()); |
| 72 | + $output->writeln("\t- storage id: " . $cachedMount->getStorageId()); |
| 73 | + $output->writeln("\t- root id: " . $cachedMount->getRootId()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments