Remove.php 4.2 KB

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