|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pagely\AtomicClient\Command\Apps; |
| 4 | + |
| 5 | +use Pagely\AtomicClient\API\AuthApi; |
| 6 | +use Pagely\AtomicClient\Command\Command; |
| 7 | +use Pagely\AtomicClient\Command\OauthCommandTrait; |
| 8 | +use Pagely\AtomicClient\API\Apps\AppsClient; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Console\Helper\Table; |
| 14 | + |
| 15 | +class GetAppBackupCommand extends Command |
| 16 | +{ |
| 17 | + use OauthCommandTrait; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var AppsClient |
| 21 | + */ |
| 22 | + protected $api; |
| 23 | + |
| 24 | + public function __construct(AuthApi $authApi, AppsClient $apps, $name = 'apps:backups:get') |
| 25 | + { |
| 26 | + $this->authClient = $authApi; |
| 27 | + $this->api = $apps; |
| 28 | + parent::__construct($name); |
| 29 | + } |
| 30 | + |
| 31 | + public function configure() |
| 32 | + { |
| 33 | + parent::configure(); |
| 34 | + $this |
| 35 | + ->setDescription('Get Backup details and download links') |
| 36 | + ->addArgument('appId', InputArgument::REQUIRED, 'App ID') |
| 37 | + ->addOption('backupId', null, InputOption::VALUE_REQUIRED, 'Backup ID. If omitted, the latest backup is returned.') |
| 38 | + ->addOption('curl', null, InputOption::VALUE_REQUIRED, 'Return curl download command eg. file|sql') |
| 39 | + ->addOption('json', null, InputOption::VALUE_NONE, 'Return data is JSON format'); |
| 40 | + $this->addOauthOptions(); |
| 41 | + } |
| 42 | + |
| 43 | + public function execute(InputInterface $input, OutputInterface $output) |
| 44 | + { |
| 45 | + $appId = $input->getArgument('appId'); |
| 46 | + $backupId = $input->getOption('backupId'); |
| 47 | + $json = $input->getOption('json'); |
| 48 | + $curlOption = $input->getOption('curl'); |
| 49 | + |
| 50 | + $token = $this->token->token; |
| 51 | + |
| 52 | + if ($backupId) { |
| 53 | + $r = $this->api->getAppBackup($token, $appId, $backupId); |
| 54 | + } else { |
| 55 | + $r = $this->api->getLatestAppBackup($token, $appId); |
| 56 | + } |
| 57 | + |
| 58 | + $body = json_decode($r->getBody()->getContents(), true); |
| 59 | + |
| 60 | + if ($json) { |
| 61 | + echo json_encode($body, JSON_PRETTY_PRINT); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + if (empty($curlOption)) { |
| 66 | + $rows = []; |
| 67 | + foreach ($body as $k => $v) { |
| 68 | + if ($k === 'sqlLink' || $k === 'fileLink') { |
| 69 | + continue; |
| 70 | + } |
| 71 | + $rows[] = [$k, $v]; |
| 72 | + } |
| 73 | + |
| 74 | + $table = new Table($output); |
| 75 | + $table |
| 76 | + ->setHeaders(['Field', 'Value']) |
| 77 | + ->setRows($rows); |
| 78 | + $table->render(); |
| 79 | + |
| 80 | + $output->writeln("Files Link"); |
| 81 | + $output->writeln($body['fileLink']); |
| 82 | + $output->writeln("Sql Link"); |
| 83 | + $output->writeln($body['sqlLink']); |
| 84 | + return 0; |
| 85 | + } |
| 86 | + switch ($curlOption) { |
| 87 | + case 'file': |
| 88 | + $output->writeln("curl {$body['fileLink']} --output pagely-file-backup.tar.gz"); |
| 89 | + break; |
| 90 | + case 'sql': |
| 91 | + $output->writeln("curl {$body['sqlLink']} --output pagely-sql-backup.tar.gz"); |
| 92 | + break; |
| 93 | + default: |
| 94 | + $output->writeln("Unknown backup type for --curl"); |
| 95 | + return 1; |
| 96 | + } |
| 97 | + return 0; |
| 98 | + } |
| 99 | +} |
0 commit comments