Remove.php 4.4 KB

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