ListApps.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  10. * @author Adam Blakey <adam@blakey.family>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Command\App;
  28. use OC\Core\Command\Base;
  29. use OCP\App\IAppManager;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class ListApps extends Base {
  35. public function __construct(
  36. protected IAppManager $manager,
  37. ) {
  38. parent::__construct();
  39. }
  40. protected function configure(): void {
  41. parent::configure();
  42. $this
  43. ->setName('app:list')
  44. ->setDescription('List all available apps')
  45. ->addOption(
  46. 'shipped',
  47. null,
  48. InputOption::VALUE_REQUIRED,
  49. 'true - limit to shipped apps only, false - limit to non-shipped apps only'
  50. )
  51. ->addOption(
  52. 'enabled',
  53. null,
  54. InputOption::VALUE_NONE,
  55. 'shows only enabled apps'
  56. )
  57. ->addOption(
  58. 'disabled',
  59. null,
  60. InputOption::VALUE_NONE,
  61. 'shows only disabled apps'
  62. )
  63. ;
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. if ($input->getOption('shipped') === 'true' || $input->getOption('shipped') === 'false') {
  67. $shippedFilter = $input->getOption('shipped') === 'true';
  68. } else {
  69. $shippedFilter = null;
  70. }
  71. $showEnabledApps = $input->getOption('enabled') || !$input->getOption('disabled');
  72. $showDisabledApps = $input->getOption('disabled') || !$input->getOption('enabled');
  73. $apps = \OC_App::getAllApps();
  74. $enabledApps = $disabledApps = [];
  75. $versions = \OC_App::getAppVersions();
  76. //sort enabled apps above disabled apps
  77. foreach ($apps as $app) {
  78. if ($shippedFilter !== null && $this->manager->isShipped($app) !== $shippedFilter) {
  79. continue;
  80. }
  81. if ($this->manager->isInstalled($app)) {
  82. $enabledApps[] = $app;
  83. } else {
  84. $disabledApps[] = $app;
  85. }
  86. }
  87. $apps = [];
  88. if ($showEnabledApps) {
  89. $apps['enabled'] = [];
  90. sort($enabledApps);
  91. foreach ($enabledApps as $app) {
  92. $apps['enabled'][$app] = $versions[$app] ?? true;
  93. }
  94. }
  95. if ($showDisabledApps) {
  96. $apps['disabled'] = [];
  97. sort($disabledApps);
  98. foreach ($disabledApps as $app) {
  99. $apps['disabled'][$app] = $this->manager->getAppVersion($app) . (isset($versions[$app]) ? ' (installed ' . $versions[$app] . ')' : '');
  100. }
  101. }
  102. $this->writeAppList($input, $output, $apps);
  103. return 0;
  104. }
  105. /**
  106. * @param InputInterface $input
  107. * @param OutputInterface $output
  108. * @param array $items
  109. */
  110. protected function writeAppList(InputInterface $input, OutputInterface $output, $items): void {
  111. switch ($input->getOption('output')) {
  112. case self::OUTPUT_FORMAT_PLAIN:
  113. if (isset($items['enabled'])) {
  114. $output->writeln('Enabled:');
  115. parent::writeArrayInOutputFormat($input, $output, $items['enabled']);
  116. }
  117. if (isset($items['disabled'])) {
  118. $output->writeln('Disabled:');
  119. parent::writeArrayInOutputFormat($input, $output, $items['disabled']);
  120. }
  121. break;
  122. default:
  123. parent::writeArrayInOutputFormat($input, $output, $items);
  124. break;
  125. }
  126. }
  127. /**
  128. * @param string $optionName
  129. * @param CompletionContext $context
  130. * @return array
  131. */
  132. public function completeOptionValues($optionName, CompletionContext $context): array {
  133. if ($optionName === 'shipped') {
  134. return ['true', 'false'];
  135. }
  136. return [];
  137. }
  138. /**
  139. * @param string $argumentName
  140. * @param CompletionContext $context
  141. * @return string[]
  142. */
  143. public function completeArgumentValues($argumentName, CompletionContext $context): array {
  144. return [];
  145. }
  146. }