Delete.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Command;
  25. use OC\Core\Command\Base;
  26. use OCA\Files_External\NotFoundException;
  27. use OCA\Files_External\Service\GlobalStoragesService;
  28. use OCA\Files_External\Service\UserStoragesService;
  29. use OCP\IUserManager;
  30. use OCP\IUserSession;
  31. use Symfony\Component\Console\Input\ArrayInput;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. use Symfony\Component\Console\Question\ConfirmationQuestion;
  37. class Delete extends Base {
  38. protected GlobalStoragesService $globalService;
  39. protected UserStoragesService $userService;
  40. protected IUserSession $userSession;
  41. protected IUserManager $userManager;
  42. public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
  43. parent::__construct();
  44. $this->globalService = $globalService;
  45. $this->userService = $userService;
  46. $this->userSession = $userSession;
  47. $this->userManager = $userManager;
  48. }
  49. protected function configure(): void {
  50. $this
  51. ->setName('files_external:delete')
  52. ->setDescription('Delete an external mount')
  53. ->addArgument(
  54. 'mount_id',
  55. InputArgument::REQUIRED,
  56. 'The id of the mount to edit'
  57. )->addOption(
  58. 'yes',
  59. 'y',
  60. InputOption::VALUE_NONE,
  61. 'Skip confirmation'
  62. );
  63. parent::configure();
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $mountId = $input->getArgument('mount_id');
  67. try {
  68. $mount = $this->globalService->getStorage($mountId);
  69. } catch (NotFoundException $e) {
  70. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  71. return 404;
  72. }
  73. $noConfirm = $input->getOption('yes');
  74. if (!$noConfirm) {
  75. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  76. $listInput = new ArrayInput([], $listCommand->getDefinition());
  77. $listInput->setOption('output', $input->getOption('output'));
  78. $listCommand->listMounts(null, [$mount], $listInput, $output);
  79. $questionHelper = $this->getHelper('question');
  80. $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);
  81. if (!$questionHelper->ask($input, $output, $question)) {
  82. return 1;
  83. }
  84. }
  85. $this->globalService->removeStorage($mountId);
  86. return 0;
  87. }
  88. }