Remove.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Patrik Kernstock <info@pkern.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Patrik Kernstock <info@pkern.at>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OC\Core\Command\App;
  29. use OC\Installer;
  30. use OCP\App\IAppManager;
  31. use Psr\Log\LoggerInterface;
  32. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  33. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  34. use Symfony\Component\Console\Command\Command;
  35. use Symfony\Component\Console\Input\InputArgument;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Input\InputOption;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. use Throwable;
  40. class Remove extends Command implements CompletionAwareInterface {
  41. public function __construct(
  42. protected IAppManager $manager,
  43. private Installer $installer,
  44. private LoggerInterface $logger,
  45. ) {
  46. parent::__construct();
  47. }
  48. protected function configure(): void {
  49. $this
  50. ->setName('app:remove')
  51. ->setDescription('remove an app')
  52. ->addArgument(
  53. 'app-id',
  54. InputArgument::REQUIRED,
  55. 'remove the specified app'
  56. )
  57. ->addOption(
  58. 'keep-data',
  59. null,
  60. InputOption::VALUE_NONE,
  61. 'keep app data and do not remove them'
  62. );
  63. }
  64. protected function execute(InputInterface $input, OutputInterface $output): int {
  65. $appId = $input->getArgument('app-id');
  66. // Check if the app is installed
  67. if (!$this->manager->isInstalled($appId)) {
  68. $output->writeln($appId . ' is not installed');
  69. return 1;
  70. }
  71. // Removing shipped apps is not possible, therefore we pre-check that
  72. // before trying to remove it
  73. if ($this->manager->isShipped($appId)) {
  74. $output->writeln($appId . ' could not be removed as it is a shipped app');
  75. return 1;
  76. }
  77. // If we want to keep the data of the app, we simply don't disable it here.
  78. // App uninstall tasks are being executed when disabled. More info: PR #11627.
  79. if (!$input->getOption('keep-data')) {
  80. try {
  81. $this->manager->disableApp($appId);
  82. $output->writeln($appId . ' disabled');
  83. } catch (Throwable $e) {
  84. $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
  85. $this->logger->error($e->getMessage(), [
  86. 'app' => 'CLI',
  87. 'exception' => $e,
  88. ]);
  89. return 1;
  90. }
  91. }
  92. // Let's try to remove the app...
  93. try {
  94. $result = $this->installer->removeApp($appId);
  95. } catch (Throwable $e) {
  96. $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
  97. $this->logger->error($e->getMessage(), [
  98. 'app' => 'CLI',
  99. 'exception' => $e,
  100. ]);
  101. return 1;
  102. }
  103. if ($result === false) {
  104. $output->writeln($appId . ' could not be removed');
  105. return 1;
  106. }
  107. $appVersion = $this->manager->getAppVersion($appId);
  108. $output->writeln($appId . ' ' . $appVersion . ' removed');
  109. return 0;
  110. }
  111. /**
  112. * @param string $optionName
  113. * @param CompletionContext $context
  114. * @return string[]
  115. */
  116. public function completeOptionValues($optionName, CompletionContext $context): array {
  117. return [];
  118. }
  119. /**
  120. * @param string $argumentName
  121. * @param CompletionContext $context
  122. * @return string[]
  123. */
  124. public function completeArgumentValues($argumentName, CompletionContext $context): array {
  125. if ($argumentName === 'app-id') {
  126. return $this->manager->getInstalledApps();
  127. }
  128. return [];
  129. }
  130. }