From ee3007015a1e25d9e94bd3034fcf74bc0ee8123e Mon Sep 17 00:00:00 2001 From: tesladethray Date: Thu, 26 Jan 2017 15:50:37 -0800 Subject: [PATCH] Refined help text for backup element options --- CHANGELOG.md | 1 + src/Commands/Backup/BackupCommand.php | 29 ++++++++++++++++ src/Commands/Backup/CreateCommand.php | 16 +++------ src/Commands/Backup/GetCommand.php | 12 +++---- src/Commands/Backup/ListCommand.php | 34 +++++-------------- src/Commands/Backup/RestoreCommand.php | 15 +++----- tests/features/backup-list.feature | 2 +- .../Commands/Backup/CreateCommandTest.php | 2 +- .../Commands/Backup/ListCommandTest.php | 4 +-- 9 files changed, 56 insertions(+), 59 deletions(-) create mode 100644 src/Commands/Backup/BackupCommand.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c15e9b60a..3581d58ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. This projec - Clear cache no longer deletes stored machine tokens. Logout now deletes stored machine tokens. (#1542) - Terminus now checks for new versions after every command run. (#1523) - `site:create` now checks to see whether a site name is taken before attempting to create it. (#1536) +- The element param on `backup:list` is now an option. (#1563) ### Removed - Removed framework type check from `drush` and `wp` commands. (#1521) diff --git a/src/Commands/Backup/BackupCommand.php b/src/Commands/Backup/BackupCommand.php new file mode 100644 index 000000000..a78fcd9f8 --- /dev/null +++ b/src/Commands/Backup/BackupCommand.php @@ -0,0 +1,29 @@ +. Creates a backup of 's environment. @@ -30,12 +24,10 @@ class CreateCommand extends TerminusCommand implements SiteAwareInterface * @usage . --keep-for= Creates a backup of 's environment and retains it for days. * @usage . --keep-for= Creates a backup of 's environment's and retains it for days. */ - public function create($site_env, $options = ['element' => null, 'keep-for' => 365,]) + public function create($site_env, $options = ['element' => 'all', 'keep-for' => 365,]) { list(, $env) = $this->getSiteEnv($site_env); - if (isset($options['element']) && ($options['element'] == 'db')) { - $options['element'] = 'database'; - } + $options['element'] = isset($options['element']) ? $this->getElement($options['element']) : null; $env->getBackups()->create($options)->wait(); $this->log()->notice('Created a backup of the {env} environment.', ['env' => $env->id,]); } diff --git a/src/Commands/Backup/GetCommand.php b/src/Commands/Backup/GetCommand.php index 4959e88c7..63676e364 100644 --- a/src/Commands/Backup/GetCommand.php +++ b/src/Commands/Backup/GetCommand.php @@ -2,21 +2,17 @@ namespace Pantheon\Terminus\Commands\Backup; -use Pantheon\Terminus\Commands\TerminusCommand; use Pantheon\Terminus\Request\RequestAwareInterface; use Pantheon\Terminus\Request\RequestAwareTrait; -use Pantheon\Terminus\Site\SiteAwareInterface; -use Pantheon\Terminus\Site\SiteAwareTrait; use Pantheon\Terminus\Exceptions\TerminusNotFoundException; /** * Class GetCommand * @package Pantheon\Terminus\Commands\Backup */ -class GetCommand extends TerminusCommand implements SiteAwareInterface, RequestAwareInterface +class GetCommand extends BackupCommand implements RequestAwareInterface { use RequestAwareTrait; - use SiteAwareTrait; /** * Displays the download URL for a specific backup or latest backup. @@ -27,7 +23,7 @@ class GetCommand extends TerminusCommand implements SiteAwareInterface, RequestA * * @param string $site_env Site & environment in the format `site-name.env` * @option string $file [filename.tgz] Name of backup file - * @option string $element [code|files|database|db] Backup element to retrieve. If not defined, all elements are selected. + * @option string $element [code|files|database|db] Backup element to retrieve * @option string $to Local path to save to * @throws TerminusNotFoundException * @@ -37,14 +33,14 @@ class GetCommand extends TerminusCommand implements SiteAwareInterface, RequestA * @usage . --to= Saves the most recent backup of any type in 's environment to . * @usage . --to= Saves the most recent backup in 's environment to . */ - public function getBackup($site_env, array $options = ['file' => null, 'element' => null, 'to' => null,]) + public function getBackup($site_env, array $options = ['file' => null, 'element' => 'all', 'to' => null,]) { list($site, $env) = $this->getSiteEnv($site_env); if (isset($options['file']) && !is_null($file_name = $options['file'])) { $backup = $env->getBackups()->getBackupByFileName($file_name); } else { - $element = ($options['element'] == 'db') ? 'database' : $options['element']; + $element = isset($options['element']) ? $this->getElement($options['element']) : null; $backups = $env->getBackups()->getFinishedBackups($element); if (empty($backups)) { throw new TerminusNotFoundException( diff --git a/src/Commands/Backup/ListCommand.php b/src/Commands/Backup/ListCommand.php index e01f34f0f..9249c77ff 100644 --- a/src/Commands/Backup/ListCommand.php +++ b/src/Commands/Backup/ListCommand.php @@ -3,18 +3,13 @@ namespace Pantheon\Terminus\Commands\Backup; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; -use Pantheon\Terminus\Commands\TerminusCommand; -use Pantheon\Terminus\Site\SiteAwareInterface; -use Pantheon\Terminus\Site\SiteAwareTrait; /** * Class ListCommand * @package Pantheon\Terminus\Commands\Backup */ -class ListCommand extends TerminusCommand implements SiteAwareInterface +class ListCommand extends BackupCommand { - use SiteAwareTrait; - /** * Lists backups for a specific site and environment. * @@ -31,32 +26,21 @@ class ListCommand extends TerminusCommand implements SiteAwareInterface * @return RowsOfFields * * @param string $site_env Site & environment in the format `site-name.env` - * @param string $element [code|files|database|db] Backup element filter. If not defined, all elements are selected. + * @param string $element [code|files|database|db] Backup element filter * * @usage . Lists all backups made of 's environment. * @usage . --element= Lists all backups made of 's environment. */ - public function listBackups($site_env, $element = 'all') + public function listBackups($site_env, array $options = ['element' => 'all',]) { list(, $env) = $this->getSiteEnv($site_env, 'dev'); - switch ($element) { - case 'all': - $backup_element = null; - break; - case 'db': - $backup_element = 'database'; - break; - default: - $backup_element = $element; - } - - $backups = $env->getBackups()->getFinishedBackups($backup_element); - - $data = []; - foreach ($backups as $backup) { - $data[] = $backup->serialize(); - } + $data = array_map( + function ($backup) { + return $backup->serialize(); + }, + $env->getBackups()->getFinishedBackups($this->getElement($options['element'])) + ); // Return the output data. return new RowsOfFields($data); diff --git a/src/Commands/Backup/RestoreCommand.php b/src/Commands/Backup/RestoreCommand.php index 02b497fe3..ef60bedc0 100644 --- a/src/Commands/Backup/RestoreCommand.php +++ b/src/Commands/Backup/RestoreCommand.php @@ -2,9 +2,6 @@ namespace Pantheon\Terminus\Commands\Backup; -use Pantheon\Terminus\Commands\TerminusCommand; -use Pantheon\Terminus\Site\SiteAwareInterface; -use Pantheon\Terminus\Site\SiteAwareTrait; use Pantheon\Terminus\Exceptions\TerminusException; use Pantheon\Terminus\Exceptions\TerminusNotFoundException; @@ -12,10 +9,8 @@ * Class RestoreCommand * @package Pantheon\Terminus\Commands\Backup */ -class RestoreCommand extends TerminusCommand implements SiteAwareInterface +class RestoreCommand extends BackupCommand { - use SiteAwareTrait; - /** * Restores a specific backup or the latest backup. * @@ -25,22 +20,22 @@ class RestoreCommand extends TerminusCommand implements SiteAwareInterface * * @param string $site_env Site & environment in the format `site-name.env` * @option string $file [filename.tgz] Name of backup file - * @option string $element [code|files|database|db] Backup element. If not defined, all elements are selected. + * @option string $element [code|files|database|db] Backup element * @throws TerminusException * * @usage . Restores the most recent backup of any type to 's environment. * @usage . --file= Restores backup with the file name to 's environment. * @usage . --element= Restores the most recent backup to 's environment. */ - public function restoreBackup($site_env, array $options = ['file' => null, 'element' => null,]) + public function restoreBackup($site_env, array $options = ['file' => null, 'element' => 'all',]) { list($site, $env) = $this->getSiteEnv($site_env); if (isset($options['file']) && !is_null($file_name = $options['file'])) { $backup = $env->getBackups()->getBackupByFileName($file_name); } else { - $element = ($options['element'] == 'db') ? 'database' : $options['element']; - $backups = $env->getBackups()->getFinishedBackups($element); + $element = isset($options['element']) ? $this->getElement($options['element']) : null; + $backups = $env->getBackups()->getFinishedBackups($this->getElement($element)); if (empty($backups)) { throw new TerminusNotFoundException( 'No backups available. Create one with `terminus backup:create {site}.{env}`', diff --git a/tests/features/backup-list.feature b/tests/features/backup-list.feature index 28a0fd608..4ee3ea778 100644 --- a/tests/features/backup-list.feature +++ b/tests/features/backup-list.feature @@ -15,7 +15,7 @@ Feature: List Backups for a Site @vcr backup-list.yml Scenario: Filter backups by element - When I run "terminus backup:list [[test_site_name]].dev db --format=json" + When I run "terminus backup:list [[test_site_name]].dev --element=db --format=json" Then I should have "2" records And I should get: "database.sql.gz" And I should not get: "code.tar.gz" diff --git a/tests/unit_tests/Commands/Backup/CreateCommandTest.php b/tests/unit_tests/Commands/Backup/CreateCommandTest.php index 626cd2005..86caf56ce 100644 --- a/tests/unit_tests/Commands/Backup/CreateCommandTest.php +++ b/tests/unit_tests/Commands/Backup/CreateCommandTest.php @@ -61,7 +61,7 @@ public function testCreateBackupWithKeepFor() $this->backups->expects($this->once()) ->method('create') - ->with($this->equalTo($params)) + ->with($this->equalTo($params + ['element' => null,])) ->willReturn($this->workflow); $this->workflow->expects($this->once()) diff --git a/tests/unit_tests/Commands/Backup/ListCommandTest.php b/tests/unit_tests/Commands/Backup/ListCommandTest.php index a96317dc1..b2c962a62 100644 --- a/tests/unit_tests/Commands/Backup/ListCommandTest.php +++ b/tests/unit_tests/Commands/Backup/ListCommandTest.php @@ -61,7 +61,7 @@ public function testListBackupsWithDatabaseElement() ->with($this->equalTo('database')) ->willReturn([$this->backup,]); - $out = $this->command->listBackups('mysite.dev', 'db'); + $out = $this->command->listBackups('mysite.dev', ['element' => 'db',]); $this->assertInstanceOf(RowsOfFields::class, $out); $this->assertEquals([$this->sample_data,], $out->getArrayCopy()); } @@ -79,7 +79,7 @@ public function testListBackupsWithSomeOtherElement() ->willReturn([$this->backup,]); - $out = $this->command->listBackups('mysite.dev', $element); + $out = $this->command->listBackups('mysite.dev', compact('element')); $this->assertInstanceOf(RowsOfFields::class, $out); $this->assertEquals([$this->sample_data,], $out->getArrayCopy()); }