Skip to content

Commit

Permalink
add fetch app backups methods and command
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaffney-godaddy authored and gforsythe-godaddy committed Sep 3, 2021
1 parent a6ffb57 commit 35d0bbf
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/API/Apps/AppsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ public function remove($accessToken, $appId)
//
// Git integration methods
//
public function createGitIntegration(string $accessToken,int $appId, string $remoteProvider, string $branch): \Psr\Http\Message\ResponseInterface
public function createGitIntegration(string $accessToken, int $appId, string $remoteProvider, string $branch): \Psr\Http\Message\ResponseInterface
{
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))
->post("/apps/{$appId}/git-integration",
->post(
"/apps/{$appId}/git-integration",
[
'json' =>[
'remote' => $remoteProvider,
Expand All @@ -212,9 +213,28 @@ public function deleteGitIntegration(string $accessToken, int $appId): \Psr\Http
->delete("/apps/{$appId}/git-integration");
}

public function getGitIntegration(string $accessToken,int $appId): \Psr\Http\Message\ResponseInterface
public function getGitIntegration(string $accessToken, int $appId): \Psr\Http\Message\ResponseInterface
{
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))
->get("/apps/{$appId}/git-integration");
}

public function getAppBackups($accessToken, $appId)
{
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))
->get("apps/{$appId}/backups");
}

public function getAppBackup($accessToken, $appId, $backupId)
{
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))
->get("apps/{$appId}/backups/{$backupId}");
}

public function getLatestAppBackup($accessToken, $appId)
{
$body = json_decode($this->getAppBackups($accessToken, $appId)->getBody()->getContents(), true);
$latest = end($body['data']);
return $this->getAppBackup($accessToken, $appId, $latest['id']);
}
}
99 changes: 99 additions & 0 deletions src/Command/Apps/GetAppBackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Pagely\AtomicClient\Command\Apps;

use Pagely\AtomicClient\API\AuthApi;
use Pagely\AtomicClient\Command\Command;
use Pagely\AtomicClient\Command\OauthCommandTrait;
use Pagely\AtomicClient\API\Apps\AppsClient;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

class GetAppBackupCommand extends Command
{
use OauthCommandTrait;

/**
* @var AppsClient
*/
protected $api;

public function __construct(AuthApi $authApi, AppsClient $apps, $name = 'apps:backups:get')
{
$this->authClient = $authApi;
$this->api = $apps;
parent::__construct($name);
}

public function configure()
{
parent::configure();
$this
->setDescription('Get Backup details and download links')
->addArgument('appId', InputArgument::REQUIRED, 'App ID')
->addOption('backupId', null, InputOption::VALUE_REQUIRED, 'Backup ID. If omitted, the latest backup is returned.')
->addOption('curl', null, InputOption::VALUE_REQUIRED, 'Return curl download command eg. file|sql')
->addOption('json', null, InputOption::VALUE_NONE, 'Return data is JSON format');
$this->addOauthOptions();
}

public function execute(InputInterface $input, OutputInterface $output)
{
$appId = $input->getArgument('appId');
$backupId = $input->getOption('backupId');
$json = $input->getOption('json');
$curlOption = $input->getOption('curl');

$token = $this->token->token;

if ($backupId) {
$r = $this->api->getAppBackup($token, $appId, $backupId);
} else {
$r = $this->api->getLatestAppBackup($token, $appId);
}

$body = json_decode($r->getBody()->getContents(), true);

if ($json) {
echo json_encode($body, JSON_PRETTY_PRINT);
return 1;
}

if (empty($curlOption)) {
$rows = [];
foreach ($body as $k => $v) {
if ($k === 'sqlLink' || $k === 'fileLink') {
continue;
}
$rows[] = [$k, $v];
}

$table = new Table($output);
$table
->setHeaders(['Field', 'Value'])
->setRows($rows);
$table->render();

$output->writeln("Files Link");
$output->writeln($body['fileLink']);
$output->writeln("Sql Link");
$output->writeln($body['sqlLink']);
return 0;
}
switch ($curlOption) {
case 'file':
$output->writeln("curl {$body['fileLink']} --output pagely-file-backup.tar.gz");
break;
case 'sql':
$output->writeln("curl {$body['sqlLink']} --output pagely-sql-backup.tar.gz");
break;
default:
$output->writeln("Unknown backup type for --curl");
return 1;
}
return 0;
}
}

0 comments on commit 35d0bbf

Please sign in to comment.