|
5 | 5 | namespace Itiden\Backup\Console\Commands; |
6 | 6 |
|
7 | 7 | use Illuminate\Console\Command; |
8 | | -use Illuminate\Contracts\Console\PromptsForMissingInput; |
| 8 | +use Itiden\Backup\Contracts\Repositories\BackupRepository; |
9 | 9 | use Itiden\Backup\DataTransferObjects\BackupDto; |
10 | 10 | use Itiden\Backup\Facades\Restorer; |
11 | 11 |
|
| 12 | +use function Laravel\Prompts\{confirm, spin, info, select}; |
| 13 | + |
12 | 14 | /** |
13 | 15 | * Restore content from a directory / backup |
14 | 16 | */ |
15 | | -class RestoreCommand extends Command implements PromptsForMissingInput |
| 17 | +final class RestoreCommand extends Command |
16 | 18 | { |
17 | | - protected $signature = 'statamic:backup:restore {path} {--force}'; |
| 19 | + protected $signature = 'statamic:backup:restore {--path=} {--force}'; |
18 | 20 |
|
19 | 21 | protected $description = 'Reset or restore content from a directory / backup'; |
20 | 22 |
|
21 | | - protected function promptForMissingArgumentsUsing() |
| 23 | + public function handle(BackupRepository $repo) |
22 | 24 | { |
23 | | - return [ |
24 | | - 'path' => 'Which filepath does your backup have?', |
25 | | - ]; |
26 | | - } |
| 25 | + /* @var BackupDto $backup */ |
| 26 | + $backup = match (true) { |
| 27 | + (bool) $this->option('path') => BackupDto::fromAbsolutePath($this->option('path')), |
| 28 | + default => BackupDto::fromFile(select( |
| 29 | + label: 'Which backup do you want to restore to?', |
| 30 | + scroll: 10, |
| 31 | + options: $repo->all()->flatMap( |
| 32 | + fn (BackupDto $backup) => [$backup->path => $backup->path] |
| 33 | + ), |
| 34 | + required: true |
| 35 | + )), |
| 36 | + }; |
27 | 37 |
|
28 | | - public function handle() |
29 | | - { |
30 | | - if ($this->option('force') || $this->confirm('Are you sure you want to restore your content?')) { |
31 | | - Restorer::restore(BackupDto::fromAbsolutePath($this->argument('path'))); |
| 38 | + if ( |
| 39 | + $this->option('force') |
| 40 | + || confirm( |
| 41 | + label: "Are you sure you want to restore your content?", |
| 42 | + hint: "This will overwrite your current content with state from {$backup->created_at->format('Y-m-d H:i:s')}", |
| 43 | + required: true |
| 44 | + ) |
| 45 | + ) { |
| 46 | + spin(fn () => Restorer::restore($backup), 'Restoring backup'); |
| 47 | + |
| 48 | + info('Backup restored!'); |
32 | 49 | } |
33 | 50 | } |
34 | 51 | } |
0 commit comments