Delete.php 3.3 KB

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