Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve commands #30

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</source>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
</phpunit>
8 changes: 4 additions & 4 deletions src/Console/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Console\Command;
use Itiden\Backup\Facades\Backuper;

use function Laravel\Prompts\{info, spin};

/**
* Backup site
*/
Expand All @@ -18,10 +20,8 @@ class BackupCommand extends Command

public function handle()
{
$this->components->info('Backing up content');

$backup_location = Backuper::backup();
$backup = spin(fn () => Backuper::backup(), 'Backing up...');

$this->components->info('Backup saved to ' . $backup_location->path);
info('Backup saved to ' . $backup->path);
}
}
8 changes: 5 additions & 3 deletions src/Console/Commands/ClearFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

use function Laravel\Prompts\info;

/**
* Clear the backup temp directory
*/
Expand All @@ -18,14 +20,14 @@ class ClearFilesCommand extends Command

public function handle()
{
if (! File::exists(config('backup.temp_path'))) {
$this->components->info('Backup temp directory does not exist, no need to clear it.');
if (!File::exists(config('backup.temp_path'))) {
info('Backup temp directory does not exist, no need to clear it.');

return;
}

File::cleanDirectory(config('backup.temp_path'));

$this->components->info('Backup temp directory cleared successfully');
info('Backup temp directory cleared successfully');
}
}
36 changes: 26 additions & 10 deletions src/Console/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,45 @@

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Itiden\Backup\Contracts\Repositories\BackupRepository;
use Itiden\Backup\DataTransferObjects\BackupDto;
use Itiden\Backup\Facades\Restorer;

use function Laravel\Prompts\{confirm, spin, info, select};

/**
* Restore content from a directory / backup
*/
class RestoreCommand extends Command implements PromptsForMissingInput
{
protected $signature = 'statamic:backup:restore {path} {--force}';
protected $signature = 'statamic:backup:restore {--path=} {--force}';

protected $description = 'Reset or restore content from a directory / backup';

protected function promptForMissingArgumentsUsing()
public function handle(BackupRepository $repo)
{
return [
'path' => 'Which filepath does your backup have?',
];
}
/* @var BackupDto $backup */
$backup = match (true) {
(bool) $this->option('path') => BackupDto::fromAbsolutePath($this->option('path')),
default => BackupDto::fromFile(select(
label: 'Which backup do you want to restore to?',
scroll: 10,
options: $repo->all()->flatMap(
fn (BackupDto $backup) => [$backup->path => $backup->path]
)
)),
};

public function handle()
{
if ($this->option('force') || $this->confirm('Are you sure you want to restore your content?')) {
Restorer::restore(BackupDto::fromAbsolutePath($this->argument('path')));
if (
$this->option('force')
|| confirm(
label: "Are you sure you want to restore your content?",
hint: "This will overwrite your current content with state from {$backup->created_at->format('Y-m-d H:i:s')}"
)
) {
spin(fn () => Restorer::restore($backup), 'Restoring backup');

info('Backup restored!');
}
}
}
6 changes: 3 additions & 3 deletions tests/Feature/RestoreBackupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@

File::cleanDirectory(config('backup.content_path'));

$this->artisan('statamic:backup:restore', ['path' => Storage::path($backup->path)])
$this->artisan('statamic:backup:restore', ['--path' => Storage::path($backup->path)])
->expectsConfirmation('Are you sure you want to restore your content?', 'no');

expect(File::isEmptyDirectory(config('backup.content_path')))->toBeTrue();

$this->artisan('statamic:backup:restore', ['path' => Storage::path($backup->path), '--force' => true])
$this->artisan('statamic:backup:restore', ['--path' => Storage::path($backup->path), '--force' => true])
->assertExitCode(0);
});

it('can restore from path command', function () {
$backup = Backuper::backup();

$this->artisan('statamic:backup:restore', ['path' => Storage::path($backup->path), '--force' => true])
$this->artisan('statamic:backup:restore', ['--path' => Storage::path($backup->path), '--force' => true])
->assertExitCode(0);

expect(File::isEmptyDirectory(config('backup.content_path')))->toBeFalse();
Expand Down
Loading