Skip to content

Commit

Permalink
AL-1684 - Updated list and info commands to use renderer filters, mod…
Browse files Browse the repository at this point in the history
…el serialize to not format, for DRYness and increased usefulness of model data

* Updated Terminus to use Robo\Config\Config because of deprecation and added formatDatetime function to DRY up TerminusModel::serialize() functions

* Added Terminus' own ConfigAwareTrait to avoid issues returning TerminusConfig

* Updated CHANGELOG.md to mention workflow:list's datetime data formatting

* Removed redundant functions in TerminusConfig

* Added datetime renderer adding to RowsOfFieldsTrait

* Added formatter for boolean so serialize functions return bools instead of strings

* Changed RowsOfFieldsTrait to StructuredListTrait so the filters can be applied to any AbstractStructuredList descendant

* Added Backup-filtering functions to Backups

* Eliminated PropertyListInterface and RowsOfFieldsInterface because they were largely pointless

* Moved StructuredListTrait to Pantheon\Terminus\Commands because it was misplaced with model/collection-type traits

* Removed date-formatting cruft from Backup::serialize()
  • Loading branch information
TeslaDethray authored Jan 22, 2019
1 parent b12166a commit b953f0d
Show file tree
Hide file tree
Showing 94 changed files with 779 additions and 687 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ All notable changes to this project will be documented in this file. This projec
- A progess bar has been added to the process portion of `remote:drush`. (#1910)
- A progess bar has been added to the process portion of `remote:wp`. (#1910)
- A progress bar has been added to the workflow processing portion of `site:delete`. (#1922)
- Added the TerminusConfig::formatDatetime() function in order to use the configuration to format datetimes. (#1923)

### Changed
- `org:site:list` now displays a `Plan`/`plan_name` field to replace `Service Level`/`service_level`. (#1901)
Expand All @@ -81,6 +82,20 @@ All notable changes to this project will be documented in this file. This projec
- `Site::delete()` now returns a Workflow object. (#1922)
- Removed final, redundant 'Applied upstream updates to "dev"' notice from `upstream:updates:apply`. (#1851)
- `upstream:updates:list` now orders the pending updates in chronological order. (#1852)
- TerminusConfig::setSource() changed from public to now protected. (#1923)
- The `started_at` data returned by `workflow:list` is now formatted using TERMINUS_DATE_FORMAT. (#1923)
- The `finished_at` data returned by `workflow:list` is now formatted using TERMINUS_DATE_FORMAT. (#1923)
- Site::serialize() 'frozen' attribute has changed from string to boolean. (#1923)
- Site::serialize() 'created' attribute has changed to a Unix timestamp. (#1923)
- Site::serialize() 'last_frozen_at' attribute has changed to a Unix timestamp. (#1923)
- Environment::serialize() 'environment_created' attribute has changed to a Unix timestamp. (#1923)
- Environment::serialize() 'initialized' attribute has changed from string to boolean. (#1923)
- Environment::serialize() 'locked' attribute has changed from string to boolean. (#1923)
- Environment::serialize() 'onseverdev' attribute has changed from string to boolean. (#1923)
- Domain::serialize() 'deletable' attribute has changed from string to boolean. (#1923)
- Lock::serialize() 'locked' attribute has changed from string to boolean. (#1923)
- `Pantheon\Terminus\Friends\RowsOfFieldsTrait` has become `Pantheon\Terminus\Commands\StructuredDataTrait`. (#1923)
- `Backup::getUrl()` has been changed to `Backup::getArchiveURL()`. (#1923)

### Deprecated
- `service-level:set` is now deprecated. Please use `plan:set`. (#1901)
Expand All @@ -94,6 +109,11 @@ All notable changes to this project will be documented in this file. This projec
- Fixed `site:upstream:set` to appropriately reject attempted changes by unauthorized users. (#1913)
- Fixed `site:team:remove` when removing oneself from the team an error is no longer thrown upon success. (#1914)

### Removed
- Removed `TerminusConfig::fromArray()`. Use the inherited `TerminusConfig::combine()`. (#1923)
- Removed `TerminusConfig::toArray()`. Use the inherited `TerminusConfig::export()`. (#1923)
- Removed `Pantheon\Terminus\Friends\RowsOfFieldsInterface` (#1923)

## 1.9.0 - 2018-09-11
### Added
- Added a `hide_git_mode_warning` option to disable the warning presented when users run Drush or WP-CLI commands on Pantheon sites that are in git mode. (#1882)
Expand Down
25 changes: 25 additions & 0 deletions src/Collections/Backups.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ public function fetch()
return $this;
}

/**
* Filters out backups which are not of the given type
*
* @param string [code|files|database] The element desired of the backup collection
* @return Backups
*/
public function filterForElement($element)
{
return $this->filter(function ($backup) use ($element) {
return $backup->get('type') === $element;
});
}

/**
* Filters out unfinished backups
*
* @return Backups
*/
public function filterForFinished()
{
return $this->filter(function ($backup) {
return $backup->backupIsFinished();
});
}

/**
* Fetches backup for a specified filename
*
Expand Down
17 changes: 14 additions & 3 deletions src/Collections/Environments.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public function create($to_env_id, Environment $from_env)
return $workflow;
}

/**
* Filters out non-multidev environments
*
* @return Environments $this
*/
public function filterForMultidev()
{
$this->filter(function ($env) {
return $env->isMultidev();
});
return $this;
}

/**
* List Environment IDs, with Dev/Test/Live first
*
Expand All @@ -72,9 +85,7 @@ public function ids()
*/
public function multidev()
{
$multidev_envs = $this->filter(function ($environment) {
return $environment->isMultidev();
})->all();
$multidev_envs = $this->filterForMultidev()->all();
$this->reset();
return $multidev_envs;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Collections/SavedTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Pantheon\Terminus\Collections;

use Pantheon\Terminus\Config\ConfigAwareTrait;
use Pantheon\Terminus\DataStore\DataStoreAwareInterface;
use Pantheon\Terminus\DataStore\DataStoreAwareTrait;
use Pantheon\Terminus\Models\SavedToken;
use Robo\Common\ConfigAwareTrait;
use Robo\Contract\ConfigAwareInterface;

/**
* Class SavedTokens
* @package Pantheon\Terminus\Collections
* TODO: Convert to FetchlessCollection
*/
class SavedTokens extends TerminusCollection implements ConfigAwareInterface, DataStoreAwareInterface
{
Expand Down
10 changes: 10 additions & 0 deletions src/Collections/TerminusCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ public function get($id)
);
}

/**
* Returns the name of the model class this collection collects
*
* @return string
*/
public function getCollectedClass()
{
return $this->collected_class;
}

/**
* @return array Returns data array
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Commands/Auth/WhoamiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Commands\StructuredListTrait;

/**
* Class WhoamiCommand
* @package Pantheon\Terminus\Commands\Auth
*/
class WhoamiCommand extends TerminusCommand
{
use StructuredListTrait;

/**
* Displays information about the currently logged-in user.
*
Expand All @@ -32,7 +35,7 @@ public function whoAmI()
{
if ($this->session()->isActive()) {
$user = $this->session()->getUser();
return new PropertyList($user->fetch()->serialize());
return $this->getPropertyList($user->fetch());
} else {
$this->log()->notice('You are not logged in.');
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Backup/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pantheon\Terminus\Commands\Backup;

use Pantheon\Terminus\Exceptions\TerminusNotFoundException;
use Pantheon\Terminus\Request\RequestAwareInterface;
use Pantheon\Terminus\Request\RequestAwareTrait;

Expand Down Expand Up @@ -34,7 +35,7 @@ class GetCommand extends SingleBackupCommand implements RequestAwareInterface
*/
public function get($site_env, array $options = ['file' => null, 'element' => 'files', 'to' => null,])
{
$backup_url = $this->getBackup($site_env, $options)->getUrl();
$backup_url = $this->getBackup($site_env, $options)->getArchiveURL();
if (!isset($options['to']) || is_null($save_path = $options['to'])) {
return $backup_url;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Commands/Backup/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Pantheon\Terminus\Commands\Backup;

use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Pantheon\Terminus\Commands\StructuredListTrait;

/**
* Class InfoCommand
* @package Pantheon\Terminus\Commands\Backup
*/
class InfoCommand extends SingleBackupCommand
{
use StructuredListTrait;

/**
* Displays information about a specific backup or the latest backup.
*
Expand Down Expand Up @@ -37,6 +40,8 @@ class InfoCommand extends SingleBackupCommand
public function info($site_env, array $options = ['file' => null, 'element' => 'all',])
{
$backup = $this->getBackup($site_env, $options);
return new PropertyList(array_merge($backup->serialize(), ['url' => $backup->getUrl(),]));
// By retrieving the archive URL it will appear in the model's serialize data
$backup->getArchiveURL();
return $this->getPropertyList($backup);
}
}
53 changes: 9 additions & 44 deletions src/Commands/Backup/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace Pantheon\Terminus\Commands\Backup;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Pantheon\Terminus\Friends\RowsOfFieldsInterface;
use Pantheon\Terminus\Friends\RowsOfFieldsTrait;
use Pantheon\Terminus\Commands\StructuredListTrait;

/**
* Class ListCommand
* @package Pantheon\Terminus\Commands\Backup
*/
class ListCommand extends BackupCommand implements RowsOfFieldsInterface
class ListCommand extends BackupCommand
{
use RowsOfFieldsTrait;
use StructuredListTrait;

/**
* Lists backups for a specific site and environment.
Expand All @@ -30,54 +29,20 @@ class ListCommand extends BackupCommand implements RowsOfFieldsInterface
* initiator: Initiator
* @return RowsOfFields
*
*
* @param string $site_env Site & environment in the format `site-name.env`
* @param string $element [all|code|files|database|db] DEPRECATED Backup element filter
* @option string $element [all|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.
*
* @deprecated 1.0.0 The element parameter is inconsistent with the other backup commands and will be removed. Please use the option instead.
*/
public function listBackups($site_env, $element = 'all', array $options = ['element' => 'all',])
public function listBackups($site_env, array $options = ['element' => 'all',])
{
list(, $env) = $this->getSiteEnv($site_env, 'dev');
// If the element option is set to default, it looks to the element parameter.

$filter = function ($collection) use ($element, $options) {
return array_map(
function ($backup) {
return $backup->serialize();
},
$collection->getFinishedBackups($this->getElement($element, $options))
);
};

// Return the output data.
return $this->getRowsOfFields($env->getBackups(), compact('filter'));
}

/**
* Decides between the depricated element parameter and options parameter, formats its choice for use in the API.
*
* @param string $param Element parameter from the command
* @param $options Options from the command
* string $element [all|code|files|database|db] Backup element filter
* @return null|string
*/
protected function getElement($param = 'all', array $options = ['element' => 'all',])
{
$element = $default = 'all';
if (isset($options['element']) && ($options['element'] !== $default)) {
$element = $options['element'];
} else if (is_array($param)) { // Sometimes the options come through as the second parameter
if (isset($param['element'])) {
$element = $param['element'];
}
} else {
$element = $param;
$backups = $env->getBackups()->filterForFinished();
$element = $this->getElement($options['element']);
if ($element !== null) {
$backups->filterForElement($element);
}
return parent::getElement($element);
return $this->getRowsOfFields($backups);
}
}
7 changes: 3 additions & 4 deletions src/Commands/Branch/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Friends\RowsOfFieldsInterface;
use Pantheon\Terminus\Friends\RowsOfFieldsTrait;
use Pantheon\Terminus\Commands\StructuredListTrait;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

/**
* Class ListCommand
* @package Pantheon\Terminus\Commands\Branch
*/
class ListCommand extends TerminusCommand implements RowsOfFieldsInterface, SiteAwareInterface
class ListCommand extends TerminusCommand implements SiteAwareInterface
{
use RowsOfFieldsTrait;
use StructuredListTrait;
use SiteAwareTrait;

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Commands/Domain/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Friends\RowsOfFieldsInterface;
use Pantheon\Terminus\Friends\RowsOfFieldsTrait;
use Pantheon\Terminus\Commands\StructuredListTrait;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

/**
* Class ListCommand
* @package Pantheon\Terminus\Commands\Domain
*/
class ListCommand extends TerminusCommand implements RowsOfFieldsInterface, SiteAwareInterface
class ListCommand extends TerminusCommand implements SiteAwareInterface
{
use RowsOfFieldsTrait;
use SiteAwareTrait;
use StructuredListTrait;

/**
* Displays domains associated with the environment.
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/Env/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Commands\StructuredListTrait;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

Expand All @@ -14,6 +15,7 @@
class InfoCommand extends TerminusCommand implements SiteAwareInterface
{
use SiteAwareTrait;
use StructuredListTrait;

/**
* Displays environment status and configuration.
Expand Down Expand Up @@ -41,6 +43,6 @@ class InfoCommand extends TerminusCommand implements SiteAwareInterface
public function info($site_env)
{
list(, $env) = $this->getUnfrozenSiteEnv($site_env);
return new PropertyList($env->serialize());
return $this->getPropertyList($env);
}
}
7 changes: 3 additions & 4 deletions src/Commands/Env/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Pantheon\Terminus\Commands\TerminusCommand;
use Pantheon\Terminus\Friends\RowsOfFieldsInterface;
use Pantheon\Terminus\Friends\RowsOfFieldsTrait;
use Pantheon\Terminus\Commands\StructuredListTrait;
use Pantheon\Terminus\Site\SiteAwareInterface;
use Pantheon\Terminus\Site\SiteAwareTrait;

/**
* Class ListCommand
* @package Pantheon\Terminus\Commands\Env
*/
class ListCommand extends TerminusCommand implements RowsOfFieldsInterface, SiteAwareInterface
class ListCommand extends TerminusCommand implements SiteAwareInterface
{
use RowsOfFieldsTrait;
use SiteAwareTrait;
use StructuredListTrait;

/**
* Displays a list of the site's environments.
Expand Down
Loading

0 comments on commit b953f0d

Please sign in to comment.