Remove.php 4.0 KB

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