ListCommand.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
  4. *
  5. * @author Johannes Leuker <j.leuker@hosting.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Command\SystemTag;
  24. use OC\Core\Command\Base;
  25. use OCP\SystemTag\ISystemTag;
  26. use OCP\SystemTag\ISystemTagManager;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class ListCommand extends Base {
  31. public function __construct(
  32. protected ISystemTagManager $systemTagManager,
  33. ) {
  34. parent::__construct();
  35. }
  36. protected function configure() {
  37. $this
  38. ->setName('tag:list')
  39. ->setDescription('list tags')
  40. ->addOption(
  41. 'visibilityFilter',
  42. null,
  43. InputOption::VALUE_OPTIONAL,
  44. 'filter by visibility (1,0)'
  45. )
  46. ->addOption(
  47. 'nameSearchPattern',
  48. null,
  49. InputOption::VALUE_OPTIONAL,
  50. 'optional search pattern for the tag name (infix)'
  51. );
  52. parent::configure();
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output): int {
  55. $tags = $this->systemTagManager->getAllTags(
  56. $input->getOption('visibilityFilter'),
  57. $input->getOption('nameSearchPattern')
  58. );
  59. $this->writeArrayInOutputFormat($input, $output, $this->formatTags($tags));
  60. return 0;
  61. }
  62. /**
  63. * @param ISystemtag[] $tags
  64. * @return array
  65. */
  66. private function formatTags(array $tags): array {
  67. $result = [];
  68. foreach ($tags as $tag) {
  69. $result[$tag->getId()] = [
  70. 'name' => $tag->getName(),
  71. 'access' => ISystemTag::ACCESS_LEVEL_LOOKUP[$tag->getAccessLevel()],
  72. ];
  73. }
  74. return $result;
  75. }
  76. }