Delete.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_External\NotFoundException;
  10. use OCA\Files_External\Service\GlobalStoragesService;
  11. use OCA\Files_External\Service\UserStoragesService;
  12. use OCP\AppFramework\Http;
  13. use OCP\IUserManager;
  14. use OCP\IUserSession;
  15. use Symfony\Component\Console\Helper\QuestionHelper;
  16. use Symfony\Component\Console\Input\ArrayInput;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Question\ConfirmationQuestion;
  22. class Delete extends Base {
  23. public function __construct(
  24. protected GlobalStoragesService $globalService,
  25. protected UserStoragesService $userService,
  26. protected IUserSession $userSession,
  27. protected IUserManager $userManager,
  28. protected QuestionHelper $questionHelper,
  29. ) {
  30. parent::__construct();
  31. }
  32. protected function configure(): void {
  33. $this
  34. ->setName('files_external:delete')
  35. ->setDescription('Delete an external mount')
  36. ->addArgument(
  37. 'mount_id',
  38. InputArgument::REQUIRED,
  39. 'The id of the mount to edit'
  40. )->addOption(
  41. 'yes',
  42. 'y',
  43. InputOption::VALUE_NONE,
  44. 'Skip confirmation'
  45. );
  46. parent::configure();
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. $mountId = $input->getArgument('mount_id');
  50. try {
  51. $mount = $this->globalService->getStorage($mountId);
  52. } catch (NotFoundException $e) {
  53. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  54. return Http::STATUS_NOT_FOUND;
  55. }
  56. $noConfirm = $input->getOption('yes');
  57. if (!$noConfirm) {
  58. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  59. $listInput = new ArrayInput([], $listCommand->getDefinition());
  60. $listInput->setOption('output', $input->getOption('output'));
  61. $listCommand->listMounts(null, [$mount], $listInput, $output);
  62. /** @var QuestionHelper $questionHelper */
  63. $questionHelper = $this->getHelper('question');
  64. $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);
  65. if (!$questionHelper->ask($input, $output, $question)) {
  66. return self::FAILURE;
  67. }
  68. }
  69. $this->globalService->removeStorage($mountId);
  70. return self::SUCCESS;
  71. }
  72. }