Remove.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public function __construct(
  41. protected IAppManager $manager,
  42. private Installer $installer,
  43. private LoggerInterface $logger,
  44. ) {
  45. parent::__construct();
  46. }
  47. protected function configure() {
  48. $this
  49. ->setName('app:remove')
  50. ->setDescription('remove an app')
  51. ->addArgument(
  52. 'app-id',
  53. InputArgument::REQUIRED,
  54. 'remove the specified app'
  55. )
  56. ->addOption(
  57. 'keep-data',
  58. null,
  59. InputOption::VALUE_NONE,
  60. 'keep app data and do not remove them'
  61. );
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int {
  64. $appId = $input->getArgument('app-id');
  65. // Check if the app is installed
  66. if (!\OC_App::getAppPath($appId)) {
  67. $output->writeln($appId . ' is not installed');
  68. return 1;
  69. }
  70. // Removing shipped apps is not possible, therefore we pre-check that
  71. // before trying to remove it
  72. if ($this->manager->isShipped($appId)) {
  73. $output->writeln($appId . ' could not be removed as it is a shipped app');
  74. return 1;
  75. }
  76. // If we want to keep the data of the app, we simply don't disable it here.
  77. // App uninstall tasks are being executed when disabled. More info: PR #11627.
  78. if (!$input->getOption('keep-data')) {
  79. try {
  80. $this->manager->disableApp($appId);
  81. $output->writeln($appId . ' disabled');
  82. } catch (Throwable $e) {
  83. $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
  84. $this->logger->error($e->getMessage(), [
  85. 'app' => 'CLI',
  86. 'exception' => $e,
  87. ]);
  88. return 1;
  89. }
  90. }
  91. // Let's try to remove the app...
  92. try {
  93. $result = $this->installer->removeApp($appId);
  94. } catch (Throwable $e) {
  95. $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
  96. $this->logger->error($e->getMessage(), [
  97. 'app' => 'CLI',
  98. 'exception' => $e,
  99. ]);
  100. return 1;
  101. }
  102. if ($result === false) {
  103. $output->writeln($appId . ' could not be removed');
  104. return 1;
  105. }
  106. $appVersion = $this->manager->getAppVersion($appId);
  107. $output->writeln($appId . ' ' . $appVersion . ' removed');
  108. return 0;
  109. }
  110. /**
  111. * @param string $optionName
  112. * @param CompletionContext $context
  113. * @return string[]
  114. */
  115. public function completeOptionValues($optionName, CompletionContext $context) {
  116. return [];
  117. }
  118. /**
  119. * @param string $argumentName
  120. * @param CompletionContext $context
  121. * @return string[]
  122. */
  123. public function completeArgumentValues($argumentName, CompletionContext $context) {
  124. if ($argumentName === 'app-id') {
  125. return \OC_App::getAllApps();
  126. }
  127. return [];
  128. }
  129. }