diff --git a/src/API/Apps/AppsClient.php b/src/API/Apps/AppsClient.php index b263b28..bb938c4 100644 --- a/src/API/Apps/AppsClient.php +++ b/src/API/Apps/AppsClient.php @@ -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, @@ -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']); + } } diff --git a/src/Command/Apps/GetAppBackupCommand.php b/src/Command/Apps/GetAppBackupCommand.php new file mode 100644 index 0000000..18125c6 --- /dev/null +++ b/src/Command/Apps/GetAppBackupCommand.php @@ -0,0 +1,99 @@ +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; + } +}