Skip to content

Commit

Permalink
#92 - AnonymizeCommand - Make output more compact in none-verbose mode (
Browse files Browse the repository at this point in the history
#93)

* #92 - AnonymizeCommand - Make output more compact in none-verbose mode

* #92 - AnonymizeCommand - Make output more compact in none-verbose mode

* #92 - AnonymizeCommand - Make output more compact in none-verbose mode

* #92 - AnonymizeCommand - Make output more compact in none-verbose mode
  • Loading branch information
SimonMellerin authored Feb 16, 2024
1 parent cbf2092 commit e844a9f
Showing 1 changed file with 83 additions and 33 deletions.
116 changes: 83 additions & 33 deletions src/Command/Anonymization/AnonymizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function configure(): void
5/ Restore your database to its original state from the backup produced at step 1.
If called with the --local-database option, step 2 is skipped.
If called with the --no-restore option, step 1 and 4 are skipped.
If called with the --no-restore option, step 1 and 5 are skipped.
TXT
)
->addUsage('/path/to/backup/to/anonymize')
Expand Down Expand Up @@ -174,17 +174,17 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
}

if ($this->doBackupAndRestoreInitial) {
$this->doBackupAndRestoreInitial = $this->io->confirm("Do you want to backup local database and restore it at the end of this process?", true);
$this->doBackupAndRestoreInitial = $this->io->confirm('Do you want to backup local database and restore it at the end of this process?', true);
}

if ('prod' === $input->getOption('env') && !$this->io->confirm("You are currently on a production environment. Are you sure you want to continue?", false)) {
if ('prod' === $input->getOption('env') && !$this->io->confirm('You are currently on a production environment. Are you sure you want to continue?', false)) {
$this->doCancel = true;

return;
}

if (!$this->doBackupAndRestoreInitial) {
$this->doCancel = !$this->io->confirm("You are about to erase your local database. Are you sure you want to continue?", true);
$this->doCancel = !$this->io->confirm('You are about to erase your local database. Are you sure you want to continue?', true);
}
}

Expand All @@ -195,15 +195,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
if ((false === $this->doBackupAndRestoreInitial) && ('prod' === $input->getOption('env'))) {
$this->io->caution([
"You are currently on a production environment.",
"Anonymizing a local database in production is not allowed.",
'You are currently on a production environment.',
'Anonymizing a local database in production is not allowed.',
]);

return self::SUCCESS;
}

if ($this->doCancel) {
$this->io->info("Action cancelled.");
$this->io->info('Action cancelled.');

return self::SUCCESS;
}
Expand All @@ -218,14 +218,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->doAnonymizeDatabase();

if (!$this->doAnonymizeCurrentDatabase && !$this->doBackupAndRestoreInitial) {
$this->doBackupAnonymizedDatabase();
}
$this->doBackupAnonymizedDatabase();

if ($this->doBackupAndRestoreInitial) {
$this->doRestoreInitialDatabase();
}

if ($this->doAnonymizeCurrentDatabase) {
$this->io->success('Anonymized backup from current database has been generated: ' . $this->backupFilename);
} else {
$this->io->success($this->backupFilename . ' has been anonymized!');
}
} catch (NotImplementedException $e) {
$this->io->error($e->getMessage());

Expand All @@ -237,7 +240,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function doBackupInitialDatabase(): void
{
$this->io->section('Start backuping local database');
if ($this->io->isVerbose()) {
$this->io->section('Start backing up local database');
} else {
$this->io->write('Backing up local database ...');
}

$backupper = $this->backupperFactory->create($this->connectionName);

Expand All @@ -256,15 +263,25 @@ private function doBackupInitialDatabase(): void
}

$backupper->checkSuccessful();
$this->io->text($backupper->getOutput());
if ($this->io->isVerbose()) {
$this->io->text($backupper->getOutput());
}

$this->io->newLine();
$this->io->info("Backup of local database done: " . $this->backupFilename);
if ($this->io->isVerbose()) {
$this->io->newLine();
$this->io->info('Local database backed up: ' . $this->backupFilename);
} else {
$this->io->writeln(' ok (' . $this->initialDatabaseBackupFilename . ')');
}
}

private function doRestoreGivenBackup(): void
{
$this->io->section('Start restoring given backup');
if ($this->io->isVerbose()) {
$this->io->section('Start restoring given backup');
} else {
$this->io->write('Restoring given backup ...');
}

$restorer = $this->restorerFactory->create($this->connectionName);

Expand All @@ -281,16 +298,25 @@ private function doRestoreGivenBackup(): void
}

$restorer->checkSuccessful();
if ($this->io->isVerbose()) {
$this->io->text($restorer->getOutput());
}

$this->io->text($restorer->getOutput());

$this->io->newLine();
$this->io->info("Restoration of given backup file done");
if ($this->io->isVerbose()) {
$this->io->newLine();
$this->io->info('Restoration of given backup file done');
} else {
$this->io->writeln(' ok');
}
}

private function doAnonymizeDatabase(): void
{
$this->io->section('Start anonymizing database');
if ($this->io->isVerbose()) {
$this->io->section('Start anonymizing database');
} else {
$this->io->write('Anonymizing database ...');
}

$anonymizator = $this->anonymizatorFactory->getOrCreate($this->connectionName);

Expand All @@ -312,21 +338,29 @@ private function doAnonymizeDatabase(): void
$this->io->writeln("");
}

$this->io->newLine();
$this->io->info("Database anonymized!");
if ($this->io->isVerbose()) {
$this->io->newLine();
$this->io->info('Database anonymized!');
} else {
$this->io->writeln(' ok');
}
}

private function doBackupAnonymizedDatabase(): void
{
$this->io->section('Start backuping anonymized database');
if ($this->io->isVerbose()) {
$this->io->section('Start backing up anonymized database');
} else {
$this->io->write('Backing up anonymized database ...');
}

$backupper = $this->backupperFactory->create($this->connectionName);
// If we are not anomymizing a database from a given backup file, we put
// anonymized database backup in classic storage dir but we specify
// it's anonymized.
$destination = $this->backupFilename ?? $this->storage->generateFilename($this->connectionName, $backupper->getExtension(), true);
$this->backupFilename = $this->backupFilename ?? $this->storage->generateFilename($this->connectionName, $backupper->getExtension(), true);
$backupper
->setDestination($destination)
->setDestination($this->backupFilename)
->setVerbose($this->io->isVerbose())
->startBackup()
;
Expand All @@ -338,15 +372,25 @@ private function doBackupAnonymizedDatabase(): void
}

$backupper->checkSuccessful();
$this->io->text($backupper->getOutput());
if ($this->io->isVerbose()) {
$this->io->text($backupper->getOutput());
}

$this->io->newLine();
$this->io->success("Anonymized backup done : " . $this->backupFilename);
if ($this->io->isVerbose()) {
$this->io->newLine();
$this->io->info('Anonymized backup done : ' . $this->backupFilename);
} else {
$this->io->writeln(' ok (' . $this->backupFilename . ')');
}
}

private function doRestoreInitialDatabase(): void
{
$this->io->section('Start restoring initial database');
if ($this->io->isVerbose()) {
$this->io->section('Start restoring initial database');
} else {
$this->io->write('Restoring initial database ...');
}

$restorer = $this->restorerFactory->create($this->connectionName);

Expand All @@ -364,9 +408,15 @@ private function doRestoreInitialDatabase(): void

$restorer->checkSuccessful();

$this->io->text($restorer->getOutput());
if ($this->io->isVerbose()) {
$this->io->text($restorer->getOutput());
}

$this->io->newLine();
$this->io->info("Restoration done");
if ($this->io->isVerbose()) {
$this->io->newLine();
$this->io->info('Restoration done');
} else {
$this->io->writeln(' ok');
}
}
}

0 comments on commit e844a9f

Please sign in to comment.