Delete.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @author Roeland Jago Douma <roeland@famdouma.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. /**
  39. * @var GlobalStoragesService
  40. */
  41. protected $globalService;
  42. /**
  43. * @var UserStoragesService
  44. */
  45. protected $userService;
  46. /**
  47. * @var IUserSession
  48. */
  49. protected $userSession;
  50. /**
  51. * @var IUserManager
  52. */
  53. protected $userManager;
  54. function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
  55. parent::__construct();
  56. $this->globalService = $globalService;
  57. $this->userService = $userService;
  58. $this->userSession = $userSession;
  59. $this->userManager = $userManager;
  60. }
  61. protected function configure() {
  62. $this
  63. ->setName('files_external:delete')
  64. ->setDescription('Delete an external mount')
  65. ->addArgument(
  66. 'mount_id',
  67. InputArgument::REQUIRED,
  68. 'The id of the mount to edit'
  69. )->addOption(
  70. 'yes',
  71. 'y',
  72. InputOption::VALUE_NONE,
  73. 'Skip confirmation'
  74. );
  75. parent::configure();
  76. }
  77. protected function execute(InputInterface $input, OutputInterface $output) {
  78. $mountId = $input->getArgument('mount_id');
  79. try {
  80. $mount = $this->globalService->getStorage($mountId);
  81. } catch (NotFoundException $e) {
  82. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  83. return 404;
  84. }
  85. $noConfirm = $input->getOption('yes');
  86. if (!$noConfirm) {
  87. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  88. $listInput = new ArrayInput([], $listCommand->getDefinition());
  89. $listInput->setOption('output', $input->getOption('output'));
  90. $listCommand->listMounts(null, [$mount], $listInput, $output);
  91. $questionHelper = $this->getHelper('question');
  92. $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);
  93. if (!$questionHelper->ask($input, $output, $question)) {
  94. return null;
  95. }
  96. }
  97. $this->globalService->removeStorage($mountId);
  98. }
  99. }