Skip to content

Commit

Permalink
Fixes #40 and moves adapters into class constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 21, 2020
1 parent d604fcf commit a6bd3e9
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 170 deletions.
12 changes: 10 additions & 2 deletions src/Commands/AccountCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
class AccountCommand extends AcquiaCommand
{

protected $accountAdapter;

public function __construct()
{
parent::__construct();

$this->accountAdapter = new Account($this->cloudapi);
}

/**
* Gets information about the user's account.
*
Expand All @@ -22,8 +31,7 @@ public function account()
$format = $this->extraConfig['format'];
$timezone = new \DateTimeZone($tz);

$accountAdapter = new Account($this->cloudapi);
$account = $accountAdapter->get();
$account = $this->accountAdapter->get();

$lastLogin = new \DateTime($account->last_login_at);
$lastLogin->setTimezone($timezone);
Expand Down
39 changes: 24 additions & 15 deletions src/Commands/AcquiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
use AcquiaCloudApi\Connector\Connector;
use AcquiaCloudApi\Endpoints\Applications;
use AcquiaCloudApi\Endpoints\Environments;
use AcquiaCloudApi\Endpoints\Code;
use AcquiaCloudApi\Endpoints\Organizations;
use AcquiaCloudApi\Endpoints\Notifications;
use AcquiaCloudApi\Endpoints\Databases;
use AcquiaCloudApi\Endpoints\DatabaseBackups;
use AcquiaCloudApi\Response\DatabaseResponse;
use AcquiaCloudApi\Endpoints\Domains;
use AcquiaCloudApi\Response\EnvironmentResponse;
use AcquiaCloudApi\Response\OperationResponse;
use AcquiaCloudApi\Response\OrganizationResponse;
Expand Down Expand Up @@ -75,14 +73,18 @@ public function __construct()
'key' => $acquia['key'],
'secret' => $acquia['secret'],
]);
/** @var \AcquiaCloudApi\Connector\Client $cloudapi */
$cloudapi = Client::factory($connector);

/** @var \AcquiaCloudApi\CloudApi\Client $cloudapi */
$this->cloudapi = $cloudapi;

$this->setCloudApi($cloudapi);
$this->setTableStyles();
}

public function setCloudApi($cloudapi)
{
$this->cloudapi = $cloudapi;
}

/**
* Override the confirm method from consolidation/Robo to allow automatic
* confirmation.
Expand Down Expand Up @@ -163,8 +165,8 @@ public function validateUuidHook(CommandData $commandData)
*/
protected function getEnvironmentFromEnvironmentName($uuid, $environment)
{
$environmentAdapter = new Environments($this->cloudapi);
$environments = $environmentAdapter->getAll($uuid);
$environmentsAdapter = new Environments($this->cloudapi);
$environments = $environmentsAdapter->getAll($uuid);

foreach ($environments as $e) {
if ($environment === $e->name) {
Expand Down Expand Up @@ -238,12 +240,7 @@ protected function waitForNotification($response)
$start = new \DateTime(date('c'));
$start->setTimezone($timezone);

// Kindly stolen from https://jonczyk.me/2017/09/20/make-cool-progressbar-symfony-command/
$output = $this->output();
$progress = new ProgressBar($output);
$progress->setBarCharacter('<fg=green>⚬</>');
$progress->setEmptyBarCharacter('<fg=red>⚬</>');
$progress->setProgressCharacter('<fg=green>➤</>');
$progress = $this->getProgressBar();
$progress->setFormat("<fg=white;bg=cyan> %message:-45s%</>\n%elapsed:6s% [%bar%] %percent:3s%%");
$progress->setMessage('Looking up notification');
$progress->start();
Expand Down Expand Up @@ -353,9 +350,9 @@ protected function backupDb($uuid, EnvironmentResponse $environment, DatabaseRes
*/
protected function copyFiles($uuid, $environmentFrom, $environmentTo)
{
$environmentAdapter = new Environments($this->cloudapi);
$environmentsAdapter = new Environments($this->cloudapi);
$this->say(sprintf('Copying files from %s to %s', $environmentFrom->label, $environmentTo->label));
$response = $environmentAdapter->copyFiles($environmentFrom->uuid, $environmentTo->uuid);
$response = $environmentsAdapter->copyFiles($environmentFrom->uuid, $environmentTo->uuid);
$this->waitForNotification($response);
}

Expand All @@ -365,4 +362,16 @@ protected function setTableStyles()
$tableStyle->setPadType(STR_PAD_BOTH);
Table::setStyleDefinition('center-align', $tableStyle);
}

protected function getProgressBar()
{
// Kindly stolen from https://jonczyk.me/2017/09/20/make-cool-progressbar-symfony-command/
$output = $this->output();
$progressBar = new ProgressBar($output);
$progressBar->setBarCharacter('<fg=green>⚬</>');
$progressBar->setEmptyBarCharacter('<fg=red>⚬</>');
$progressBar->setProgressCharacter('<fg=green>➤</>');

return $progressBar;
}
}
31 changes: 19 additions & 12 deletions src/Commands/ApplicationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
class ApplicationsCommand extends AcquiaCommand
{

protected $applicationsAdapter;
protected $environmentsAdapter;
protected $databasesAdapter;

public function __construct()
{
parent::__construct();

$this->applicationsAdapter = new Applications($this->cloudapi);
$this->environmentsAdapter = new Environments($this->cloudapi);
$this->databasesAdapter = new Databases($this->cloudapi);
}

/**
* Shows all sites a user has access to.
*
Expand All @@ -23,8 +36,7 @@ class ApplicationsCommand extends AcquiaCommand
*/
public function applications()
{
$applicationsAdapter = new Applications($this->cloudapi);
$applications = $applicationsAdapter->getAll();
$applications = $this->applicationsAdapter->getAll();

$output = $this->output();
$table = new Table($output);
Expand Down Expand Up @@ -52,15 +64,13 @@ public function applications()
*/
public function applicationInfo($uuid)
{
$environmentsAdapter = new Environments($this->cloudapi);
$environments = $environmentsAdapter->getAll($uuid);
$environments = $this->environmentsAdapter->getAll($uuid);

$output = $this->output();
$table = new Table($output);
$table->setHeaders(['Environment', 'ID', 'Branch/Tag', 'Domain(s)', 'Database(s)']);

$databasesAdapter = new Databases($this->cloudapi);
$databases = $databasesAdapter->getAll($uuid);
$databases = $this->databasesAdapter->getAll($uuid);

$dbNames = array_map(function ($database) {
return $database->name;
Expand Down Expand Up @@ -108,8 +118,7 @@ public function applicationInfo($uuid)
*/
public function applicationsTags($uuid)
{
$applicationsAdapter = new Applications($this->cloudapi);
$tags = $applicationsAdapter->getAllTags($uuid);
$tags = $this->applicationsAdapter->getAllTags($uuid);

$output = $this->output();
$table = new Table($output);
Expand Down Expand Up @@ -139,8 +148,7 @@ public function applicationsTags($uuid)
public function applicationTagCreate($uuid, $name, $color)
{
$this->say(sprintf('Creating application tag %s:%s', $name, $color));
$applicationsAdapter = new Applications($this->cloudapi);
$response = $applicationsAdapter->createTag($uuid, $name, $color);
$response = $this->applicationsAdapter->createTag($uuid, $name, $color);
$this->waitForNotification($response);
}

Expand All @@ -156,8 +164,7 @@ public function applicationTagCreate($uuid, $name, $color)
public function applicationTagDelete($uuid, $name)
{
$this->say(sprintf('Deleting application tag %s', $name));
$applicationsAdapter = new Applications($this->cloudapi);
$response = $applicationsAdapter->deleteTag($uuid, $name);
$response = $this->applicationsAdapter->deleteTag($uuid, $name);
$this->waitForNotification($response);
}
}
18 changes: 12 additions & 6 deletions src/Commands/CodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
class CodeCommand extends AcquiaCommand
{

protected $codeAdapter;

public function __construct()
{
parent::__construct();

$this->codeAdapter = new Code($this->cloudapi);
}

/**
* Gets all code branches and tags associated with an application.
*
Expand All @@ -28,8 +37,7 @@ public function code($uuid, $match = null)
if (null !== $match) {
$this->cloudapi->addQuery('filter', "name=@*${match}*");
}
$codeAdapter = new Code($this->cloudapi);
$branches = $codeAdapter->getAll($uuid);
$branches = $this->codeAdapter->getAll($uuid);
$this->cloudapi->clearQuery();
$output = $this->output();
Expand Down Expand Up @@ -86,8 +94,7 @@ public function codeDeploy(
)
);
$code = new Code($this->cloudapi);
$response = $code->deploy($environmentFrom->uuid, $environmentTo->uuid);
$response = $this->codeAdapter->deploy($environmentFrom->uuid, $environmentTo->uuid);
$this->waitForNotification($response);
}
Expand Down Expand Up @@ -116,8 +123,7 @@ public function codeSwitch($uuid, EnvironmentResponse $environment, $branch)
$this->backupAllEnvironmentDbs($uuid, $environment);
$this->say(sprintf('Switching %s enviroment to %s branch', $environment->label, $branch));
$code = new Code($this->cloudapi);
$response = $code->switch($environment->uuid, $branch);
$response = $this->codeAdapter->switch($environment->uuid, $branch);
$this->waitForNotification($response);
}
}
27 changes: 15 additions & 12 deletions src/Commands/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
class CronCommand extends AcquiaCommand
{

protected $cronAdapter;

public function __construct()
{
parent::__construct();

$this->cronAdapter = new Crons($this->cloudapi);
}

/**
* Shows all cron tasks associated with an environment.
*
Expand All @@ -25,8 +34,7 @@ class CronCommand extends AcquiaCommand
public function crons($uuid, $environment)
{

$cronAdapter = new Crons($this->cloudapi);
$crons = $cronAdapter->getAll($environment->uuid);
$crons = $this->cronAdapter->getAll($environment->uuid);

$output = $this->output();
$table = new Table($output);
Expand Down Expand Up @@ -63,8 +71,7 @@ public function crons($uuid, $environment)
*/
public function cronAdd($uuid, $environment, $commandString, $frequency, $label)
{
$cronAdapter = new Crons($this->cloudapi);
$response = $cronAdapter->create($environment->uuid, $commandString, $frequency, $label);
$response = $this->cronAdapter->create($environment->uuid, $commandString, $frequency, $label);
$this->waitForNotification($response);
}

Expand All @@ -82,8 +89,7 @@ public function cronDelete($uuid, $environment, $cronId)
{
if ($this->confirm("Are you sure you want to delete the cron task?")) {
$this->say(sprintf('Deleting cron task %s from %s', $cronId, $environment->label));
$cronAdapter = new Crons($this->cloudapi);
$response = $cronAdapter->delete($environment->uuid, $cronId);
$response = $this->cronAdapter->delete($environment->uuid, $cronId);
$this->waitForNotification($response);
}
}
Expand All @@ -99,8 +105,7 @@ public function cronDelete($uuid, $environment, $cronId)
*/
public function cronEnable($uuid, $environment, $cronId)
{
$cronAdapter = new Crons($this->cloudapi);
$response = $cronAdapter->enable($environment->uuid, $cronId);
$response = $this->cronAdapter->enable($environment->uuid, $cronId);
$this->waitForNotification($response);
}

Expand All @@ -116,8 +121,7 @@ public function cronEnable($uuid, $environment, $cronId)
public function cronDisable($uuid, $environment, $cronId)
{
if ($this->confirm("Are you sure you want to disable the cron task?")) {
$cronAdapter = new Crons($this->cloudapi);
$response = $cronAdapter->disable($environment->uuid, $cronId);
$response = $this->cronAdapter->disable($environment->uuid, $cronId);
$this->waitForNotification($response);
}
}
Expand All @@ -133,8 +137,7 @@ public function cronDisable($uuid, $environment, $cronId)
*/
public function cronInfo($uuid, $environment, $cronId)
{
$cronAdapter = new Crons($this->cloudapi);
$cron = $cronAdapter->get($environment->uuid, $cronId);
$cron = $this->cronAdapter->get($environment->uuid, $cronId);

$enabled = $cron->flags->enabled ? '' : ' ';
$system = $cron->flags->system ? '' : ' ';
Expand Down
43 changes: 35 additions & 8 deletions src/Commands/DbBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\ProgressBar;

/**
* Class DomainCommand
Expand All @@ -19,6 +20,10 @@ class DbBackupCommand extends AcquiaCommand

public $databaseBackupsAdapter;

private $downloadProgress;

private $lastStep;

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -135,7 +140,7 @@ public function dbBackupLink($uuid, $environment, $dbName, $backupId)
* @command database:backup:download
* @aliases db:backup:download
* @option $backup Select which backup to download by backup ID. If omitted, the latest will be downloaded.
* @option $path Select a path to download the backup to.
* @option $path Select a path to download the backup to. If omitted, the system temp directory will be used.
*/
public function dbBackupDownload($uuid, $environment, $dbName, $opts = ['backup' => null, 'path' => null])
{
Expand All @@ -152,19 +157,41 @@ public function dbBackupDownload($uuid, $environment, $dbName, $opts = ['backup'
$backupId = $opts['backup'];
}

$backup = $this->databaseBackupsAdapter->download($environment->uuid, $dbName, $backupId);
$backupName = sprintf('%s-%s-%s', $environment->name, $dbName, $backupId);

if (null === $opts['path']) {
$location = tempnam(sys_get_temp_dir(), $backupName) . '.sql.gz';
} else {
$location = sprintf("%s/%s.sql.gz", $opts['path'], $backupName);
}

$this->say(sprintf('Downloading database backup to %s', $location));
if (file_put_contents($location, $backup, LOCK_EX)) {
$this->say(sprintf('Database backup downloaded to %s', $location));
} else {
$this->say('Unable to download database backup.');
}

$this->downloadProgress = $this->getProgressBar();
$this->downloadProgress->start();
$this->downloadProgress->setMessage(sprintf('Downloading database backup to %s', $location));

$this->cloudapi->addOption('sink', $location);
$this->cloudapi->addOption('curl.options', ['CURLOPT_RETURNTRANSFER' => true, 'CURLOPT_FILE' => $location]);
$this->cloudapi->addOption('progress', function (
$downloadTotal,
$downloadedBytes
) {
if ($downloadTotal) {
$currentStep = $downloadedBytes - $this->lastStep;
$this->downloadProgress->setMaxSteps($downloadTotal);
$this->downloadProgress->setFormat("<fg=white;bg=cyan> %message:-45s%</>\n%current:6s%/%max:6s% bytes [%bar%] %percent:3s%%");
$this->downloadProgress->advance($currentStep);
}
$this->lastStep = $downloadedBytes;
});

$this->databaseBackupsAdapter->download($environment->uuid, $dbName, $backupId);
$this->downloadProgress->setMessage(sprintf('Database backup downloaded to %s', $location));
$this->downloadProgress->finish();

$this->writeln(PHP_EOL);
$this->say(sprintf('Database backup downloaded to %s', $location));

}
}
Loading

0 comments on commit a6bd3e9

Please sign in to comment.