88
99namespace OCA \Files \Command \Mount ;
1010
11+ use OC \Core \Command \Base ;
1112use OCP \Files \Config \ICachedMountInfo ;
1213use OCP \Files \Config \IMountProviderCollection ;
1314use OCP \Files \Config \IUserMountCache ;
1415use OCP \Files \Mount \IMountPoint ;
1516use OCP \IUserManager ;
16- use Symfony \Component \Console \Command \Command ;
1717use Symfony \Component \Console \Input \InputArgument ;
1818use Symfony \Component \Console \Input \InputInterface ;
19+ use Symfony \Component \Console \Input \InputOption ;
1920use Symfony \Component \Console \Output \OutputInterface ;
2021
21- class ListMounts extends Command {
22+ class ListMounts extends Base {
2223 public function __construct (
2324 private readonly IUserManager $ userManager ,
2425 private readonly IUserMountCache $ userMountCache ,
@@ -28,52 +29,81 @@ public function __construct(
2829 }
2930
3031 protected function configure (): void {
32+ parent ::configure ();
3133 $ this
3234 ->setName ('files:mount:list ' )
3335 ->setDescription ('List of mounts for a user ' )
34- ->addArgument ('user ' , InputArgument::REQUIRED , 'User to list mounts for ' );
36+ ->addArgument ('user ' , InputArgument::REQUIRED , 'User to list mounts for ' )
37+ ->addOption ('cached-only ' , null , InputOption::VALUE_NONE , 'Only return cached mounts, prevents filesystem setup ' );
3538 }
3639
3740 public function execute (InputInterface $ input , OutputInterface $ output ): int {
3841 $ userId = $ input ->getArgument ('user ' );
42+ $ cachedOnly = $ input ->getOption ('cached-only ' );
3943 $ user = $ this ->userManager ->get ($ userId );
4044 if (!$ user ) {
4145 $ output ->writeln ("<error>User $ userId not found</error> " );
4246 return 1 ;
4347 }
4448
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+ if ($ cachedOnly ) {
50+ $ mounts = [];
51+ } else {
52+ $ mounts = $ this ->mountProviderCollection ->getMountsForUser ($ user );
53+ $ mounts [] = $ this ->mountProviderCollection ->getHomeMountForUser ($ user );
54+ }
55+ /** @var array<string, IMountPoint> $cachedByMountPoint */
56+ $ mountsByMountPoint = array_combine (array_map (fn (IMountPoint $ mount ) => $ mount ->getMountPoint (), $ mounts ), $ mounts );
4957 usort ($ mounts , fn (IMountPoint $ a , IMountPoint $ b ) => $ a ->getMountPoint () <=> $ b ->getMountPoint ());
5058
5159 $ cachedMounts = $ this ->userMountCache ->getMountsForUser ($ user );
5260 usort ($ cachedMounts , fn (ICachedMountInfo $ a , ICachedMountInfo $ b ) => $ a ->getMountPoint () <=> $ b ->getMountPoint ());
5361 /** @var array<string, ICachedMountInfo> $cachedByMountpoint */
54- $ cachedByMountpoint = array_combine (array_map (fn (ICachedMountInfo $ mount ) => $ mount ->getMountPoint (), $ cachedMounts ), $ cachedMounts );
62+ $ cachedByMountPoint = array_combine (array_map (fn (ICachedMountInfo $ mount ) => $ mount ->getMountPoint (), $ cachedMounts ), $ cachedMounts );
63+
64+ $ format = $ input ->getOption ('output ' );
5565
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> " );
66+ if ($ format === self ::OUTPUT_FORMAT_PLAIN ) {
67+ foreach ($ mounts as $ mount ) {
68+ $ output ->writeln ('<info> ' . $ mount ->getMountPoint () . '</info>: ' . $ mount ->getStorageId ());
69+ if (isset ($ cachedByMountPoint [$ mount ->getMountPoint ()])) {
70+ $ cached = $ cachedByMountPoint [$ mount ->getMountPoint ()];
71+ $ output ->writeln ("\t- provider: " . $ cached ->getMountProvider ());
72+ $ output ->writeln ("\t- storage id: " . $ cached ->getStorageId ());
73+ $ output ->writeln ("\t- root id: " . $ cached ->getRootId ());
74+ } else {
75+ $ output ->writeln ("\t<error>not registered</error> " );
76+ }
6577 }
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 ());
78+ foreach ($ cachedMounts as $ cachedMount ) {
79+ if ($ cachedOnly || !isset ($ mountsByMountPoint [$ cachedMount ->getMountPoint ()])) {
80+ $ output ->writeln ('<info> ' . $ cachedMount ->getMountPoint () . '</info>: ' );
81+ if (!$ cachedOnly ) {
82+ $ output ->writeln ("\t<error>registered but no longer provided</error> " );
83+ }
84+ $ output ->writeln ("\t- provider: " . $ cachedMount ->getMountProvider ());
85+ $ output ->writeln ("\t- storage id: " . $ cachedMount ->getStorageId ());
86+ $ output ->writeln ("\t- root id: " . $ cachedMount ->getRootId ());
87+ }
7488 }
89+ } else {
90+ $ cached = array_map (fn (ICachedMountInfo $ cachedMountInfo ) => [
91+ 'mountpoint ' => $ cachedMountInfo ->getMountPoint (),
92+ 'provider ' => $ cachedMountInfo ->getMountProvider (),
93+ 'storage_id ' => $ cachedMountInfo ->getStorageId (),
94+ 'root_id ' => $ cachedMountInfo ->getRootId (),
95+ ], $ cachedMounts );
96+ $ provided = array_map (fn (IMountPoint $ cachedMountInfo ) => [
97+ 'mountpoint ' => $ cachedMountInfo ->getMountPoint (),
98+ 'provider ' => $ cachedMountInfo ->getMountProvider (),
99+ 'storage_id ' => $ cachedMountInfo ->getStorageId (),
100+ 'root_id ' => $ cachedMountInfo ->getStorageRootId (),
101+ ], $ mounts );
102+ $ this ->writeArrayInOutputFormat ($ input , $ output , array_filter ([
103+ 'cached ' => $ cached ,
104+ 'provided ' => $ cachedOnly ? null : $ provided ,
105+ ]));
75106 }
76-
77107 return 0 ;
78108 }
79109
0 commit comments