diff --git a/src/Commands/AbstractCommand.php b/src/Commands/AbstractCommand.php new file mode 100644 index 0000000..80ed29e --- /dev/null +++ b/src/Commands/AbstractCommand.php @@ -0,0 +1,16 @@ +format('Y-m-d H:i:s'); + + $output->writeln("[{$time}] {$message}"); + } +} diff --git a/src/Commands/BackupCommand.php b/src/Commands/BackupCommand.php index 1b07ac5..41a68ea 100644 --- a/src/Commands/BackupCommand.php +++ b/src/Commands/BackupCommand.php @@ -6,11 +6,10 @@ use App\Services\MySQLExecutor; use App\Services\Pipeline; use App\Services\S3Executor; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class BackupCommand extends Command +class BackupCommand extends AbstractCommand { use FileFormat; @@ -35,12 +34,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int S3Executor::make()->upload($file), ]); - $output->writeln("Making backup {$file}..."); + $this->log($output, "Making backup {$file}"); $output->write(exec($command, $out, $result)); - $output->writeln('Done.'); - return $result; } } diff --git a/src/Commands/ClearCommand.php b/src/Commands/ClearCommand.php index e1c4b40..c4d55e4 100644 --- a/src/Commands/ClearCommand.php +++ b/src/Commands/ClearCommand.php @@ -8,7 +8,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -class ClearCommand extends Command +class ClearCommand extends AbstractCommand { use FileFormat; @@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $days = (int) $input->getOption('days'); if ($days === 0) { - $output->writeln('Nothing to clear as days is set to zero.'); + $this->log($output, 'Nothing to clear as days is set to zero.'); return Command::SUCCESS; } @@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int continue; } - $output->writeln("Removing {$backup['file']}"); + $this->log($output, "Removing old backup {$backup['file']}"); exec($executor->remove($backup['file'])); } diff --git a/src/Commands/ListCommand.php b/src/Commands/ListCommand.php index 631c215..ff51d28 100644 --- a/src/Commands/ListCommand.php +++ b/src/Commands/ListCommand.php @@ -7,7 +7,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class ListCommand extends Command +class ListCommand extends AbstractCommand { use FileFormat; diff --git a/src/Commands/RestoreCommand.php b/src/Commands/RestoreCommand.php index 07e99d1..715eb7e 100644 --- a/src/Commands/RestoreCommand.php +++ b/src/Commands/RestoreCommand.php @@ -11,7 +11,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class RestoreCommand extends Command +class RestoreCommand extends AbstractCommand { use FileFormat; @@ -31,16 +31,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int $file = $this->renderFormat($id); if (! $this->isExists($id)) { - $output->writeln("Backup file {$file} does not exists on S3 bucket."); + $this->log($output, "Backup file {$file} does not exists on S3 bucket."); return Command::SUCCESS; } - - $output->writeln('Preparing database..'); + $this->log($output, 'Preparing database..'); $output->write(exec(MySQLExecutor::make()->init(), $out, $result)); - $output->writeln('Done.'); + $this->log($output, 'Done.'); $command = Pipeline::make([ S3Executor::make()->download($this->renderFormat($id)), @@ -48,11 +47,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int MySQLExecutor::make()->restore(), ]); - $output->writeln("Restoring backup {$file}..."); + $this->log($output, "Restoring backup {$file}..."); $output->write(exec($command, $out, $result)); - $output->writeln('Done.'); + $this->log($output, 'Done.'); return $result; }