|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | + |
| 6 | +/** |
| 7 | + * Nextcloud - Backup |
| 8 | + * |
| 9 | + * This file is licensed under the Affero General Public License version 3 or |
| 10 | + * later. See the COPYING file. |
| 11 | + * |
| 12 | + * @author Maxence Lange <maxence@artificial-owl.com> |
| 13 | + * @copyright 2021, Maxence Lange <maxence@artificial-owl.com> |
| 14 | + * @license GNU AGPL version 3 or any later version |
| 15 | + * |
| 16 | + * This program is free software: you can redistribute it and/or modify |
| 17 | + * it under the terms of the GNU Affero General Public License as |
| 18 | + * published by the Free Software Foundation, either version 3 of the |
| 19 | + * License, or (at your option) any later version. |
| 20 | + * |
| 21 | + * This program is distributed in the hope that it will be useful, |
| 22 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | + * GNU Affero General Public License for more details. |
| 25 | + * |
| 26 | + * You should have received a copy of the GNU Affero General Public License |
| 27 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +namespace OCA\Backup\Command; |
| 33 | + |
| 34 | + |
| 35 | +use OC\Core\Command\Base; |
| 36 | +use OCA\Backup\Db\CoreRequestBuilder; |
| 37 | +use Symfony\Component\Console\Input\InputInterface; |
| 38 | +use Symfony\Component\Console\Input\InputOption; |
| 39 | +use Symfony\Component\Console\Output\OutputInterface; |
| 40 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 41 | +use Symfony\Component\Console\Question\Question; |
| 42 | + |
| 43 | + |
| 44 | +/** |
| 45 | + * Class Reset |
| 46 | + * |
| 47 | + * @package OCA\Backup\Command |
| 48 | + */ |
| 49 | +class Reset extends Base { |
| 50 | + |
| 51 | + |
| 52 | + /** @var CoreRequestBuilder */ |
| 53 | + private $coreRequestBuilder; |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * Reset constructor. |
| 58 | + * |
| 59 | + * @param CoreRequestBuilder $coreRequestBuilder |
| 60 | + */ |
| 61 | + public function __construct(CoreRequestBuilder $coreRequestBuilder) { |
| 62 | + $this->coreRequestBuilder = $coreRequestBuilder; |
| 63 | + |
| 64 | + parent::__construct(); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * |
| 70 | + */ |
| 71 | + protected function configure() { |
| 72 | + $this->setName('backup:reset') |
| 73 | + ->setDescription('Remove all data related to the Backup App') |
| 74 | + ->addOption( |
| 75 | + 'uninstall', '', InputOption::VALUE_NONE, 'Also uninstall the app from the instance' |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * @param InputInterface $input |
| 82 | + * @param OutputInterface $output |
| 83 | + * |
| 84 | + * @return int |
| 85 | + */ |
| 86 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 87 | + $action = ($input->getOption('uninstall')) ? 'uninstall' : 'reset'; |
| 88 | + |
| 89 | + $output->writeln(''); |
| 90 | + $output->writeln(''); |
| 91 | + $output->writeln( |
| 92 | + '<error>WARNING! You are about to delete all data (and Restoring Point) related to the Backup App!</error>' |
| 93 | + ); |
| 94 | + $question = new ConfirmationQuestion( |
| 95 | + '<comment>Do you really want to ' . $action . ' the Backup App?</comment> (y/N) ', false, |
| 96 | + '/^(y|Y)/i' |
| 97 | + ); |
| 98 | + |
| 99 | + $helper = $this->getHelper('question'); |
| 100 | + if (!$helper->ask($input, $output, $question)) { |
| 101 | + $output->writeln('aborted.'); |
| 102 | + |
| 103 | + return 0; |
| 104 | + } |
| 105 | + |
| 106 | + $output->writeln(''); |
| 107 | + $output->writeln('<error>WARNING! This operation is not reversible.</error>'); |
| 108 | + |
| 109 | + $question = new Question( |
| 110 | + '<comment>Please confirm this destructive operation by typing \'' . $action |
| 111 | + . '\'</comment>: ', '' |
| 112 | + ); |
| 113 | + |
| 114 | + $helper = $this->getHelper('question'); |
| 115 | + $confirmation = $helper->ask($input, $output, $question); |
| 116 | + if (strtolower($confirmation) !== $action) { |
| 117 | + $output->writeln('aborted.'); |
| 118 | + |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + $this->coreRequestBuilder->cleanDatabase(); |
| 123 | + if ($action === 'uninstall') { |
| 124 | + $this->coreRequestBuilder->uninstall(); |
| 125 | + } |
| 126 | + |
| 127 | + $output->writeln('<info>' . $action . ' done</info>'); |
| 128 | + |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments