Skip to content

Commit

Permalink
Refined help text for backup element options
Browse files Browse the repository at this point in the history
  • Loading branch information
tesladethray committed Jan 27, 2017
1 parent 0e0dd0c commit ee30070
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions src/Commands/Backup/BackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Pantheon\Terminus\Commands\Backup;

use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

abstract class BackupCommand extends TerminusCommand implements SiteAwareInterface
{
use SiteAwareTrait;

/**
* Returns the API-ready element name for the given string
*
* @param string $element
* @return null|string
*/
protected function getElement($element)
{
if ($element === 'db') {
return 'database';
}
if ($element === 'all') {
return null;
}
return $element;
}
}
16 changes: 4 additions & 12 deletions src/Commands/Backup/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

namespace Pantheon\Terminus\Commands\Backup;

use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

/**
* Class CreateCommand
* @package Pantheon\Terminus\Commands\Backup
*/
class CreateCommand extends TerminusCommand implements SiteAwareInterface
class CreateCommand extends BackupCommand
{
use SiteAwareTrait;

/**
* Creates a backup of a specific site and environment.
*
Expand All @@ -22,20 +16,18 @@ class CreateCommand extends TerminusCommand implements SiteAwareInterface
* @command backup:create
*
* @param string $site_env Site & environment in the format `site-name.env`
* @option string $element [code|files|database|db] Element to be backed up. If not defined, all elements are selected.
* @option string $element [code|files|database|db] Element to be backed up
* @option integer $keep-for Retention period, in days, to retain backup
*
* @usage <site>.<env> Creates a backup of <site>'s <env> environment.
* @usage <site>.<env> --element=<element> Creates a backup of <site>'s <env> environment's <element>.
* @usage <site>.<env> --keep-for=<days> Creates a backup of <site>'s <env> environment and retains it for <days> days.
* @usage <site>.<env> --keep-for=<days> Creates a backup of <site>'s <env> environment's <element> and retains it for <days> 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,]);
}
Expand Down
12 changes: 4 additions & 8 deletions src/Commands/Backup/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*
Expand All @@ -37,14 +33,14 @@ class GetCommand extends TerminusCommand implements SiteAwareInterface, RequestA
* @usage <site>.<env> --to=<path> Saves the most recent backup of any type in <site>'s <env> environment to <path>.
* @usage <site>.<env> --to=<path> Saves the most recent <element> backup in <site>'s <env> environment to <path>.
*/
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(
Expand Down
34 changes: 9 additions & 25 deletions src/Commands/Backup/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 <site>.<env> Lists all backups made of <site>'s <env> environment.
* @usage <site>.<env> --element=<element> Lists all <element> backups made of <site>'s <env> 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);
Expand Down
15 changes: 5 additions & 10 deletions src/Commands/Backup/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

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;

/**
* 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.
*
Expand All @@ -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 <site>.<env> Restores the most recent backup of any type to <site>'s <env> environment.
* @usage <site>.<env> --file=<backup> Restores backup with the <backup> file name to <site>'s <env> environment.
* @usage <site>.<env> --element=<element> Restores the most recent <element> backup to <site>'s <env> 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}`',
Expand Down
2 changes: 1 addition & 1 deletion tests/features/backup-list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion tests/unit_tests/Commands/Backup/CreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/Commands/Backup/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down

0 comments on commit ee30070

Please sign in to comment.