Remove.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\App;
  8. use OC\Installer;
  9. use OCP\App\IAppManager;
  10. use Psr\Log\LoggerInterface;
  11. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  12. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Throwable;
  19. class Remove extends Command implements CompletionAwareInterface {
  20. public function __construct(
  21. protected IAppManager $manager,
  22. private Installer $installer,
  23. private LoggerInterface $logger,
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure(): void {
  28. $this
  29. ->setName('app:remove')
  30. ->setDescription('remove an app')
  31. ->addArgument(
  32. 'app-id',
  33. InputArgument::REQUIRED,
  34. 'remove the specified app'
  35. )
  36. ->addOption(
  37. 'keep-data',
  38. null,
  39. InputOption::VALUE_NONE,
  40. 'keep app data and do not remove them'
  41. );
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output): int {
  44. $appId = $input->getArgument('app-id');
  45. // Check if the app is installed
  46. if (!$this->manager->isInstalled($appId)) {
  47. $output->writeln($appId . ' is not installed');
  48. return 1;
  49. }
  50. // Removing shipped apps is not possible, therefore we pre-check that
  51. // before trying to remove it
  52. if ($this->manager->isShipped($appId)) {
  53. $output->writeln($appId . ' could not be removed as it is a shipped app');
  54. return 1;
  55. }
  56. // If we want to keep the data of the app, we simply don't disable it here.
  57. // App uninstall tasks are being executed when disabled. More info: PR #11627.
  58. if (!$input->getOption('keep-data')) {
  59. try {
  60. $this->manager->disableApp($appId);
  61. $output->writeln($appId . ' disabled');
  62. } catch (Throwable $e) {
  63. $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
  64. $this->logger->error($e->getMessage(), [
  65. 'app' => 'CLI',
  66. 'exception' => $e,
  67. ]);
  68. return 1;
  69. }
  70. }
  71. // Let's try to remove the app...
  72. try {
  73. $result = $this->installer->removeApp($appId);
  74. } catch (Throwable $e) {
  75. $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
  76. $this->logger->error($e->getMessage(), [
  77. 'app' => 'CLI',
  78. 'exception' => $e,
  79. ]);
  80. return 1;
  81. }
  82. if ($result === false) {
  83. $output->writeln($appId . ' could not be removed');
  84. return 1;
  85. }
  86. $appVersion = $this->manager->getAppVersion($appId);
  87. $output->writeln($appId . ' ' . $appVersion . ' removed');
  88. return 0;
  89. }
  90. /**
  91. * @param string $optionName
  92. * @param CompletionContext $context
  93. * @return string[]
  94. */
  95. public function completeOptionValues($optionName, CompletionContext $context): array {
  96. return [];
  97. }
  98. /**
  99. * @param string $argumentName
  100. * @param CompletionContext $context
  101. * @return string[]
  102. */
  103. public function completeArgumentValues($argumentName, CompletionContext $context): array {
  104. if ($argumentName === 'app-id') {
  105. return $this->manager->getInstalledApps();
  106. }
  107. return [];
  108. }
  109. }