Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Command/AbstractDbCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace DbDb\Client\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;

abstract class AbstractDbCommand extends Command
{
protected $apiUrl;

protected function execute(InputInterface $input, OutputInterface $output)
{
$url = getenv('DBDB_URL');
$username = getenv('DBDB_USERNAME');
$password = getenv('DBDB_PASSWORD');

if (!$url || !$username || !$password) {
throw new RuntimeException('Configuration incomplete');
}

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url.$this->apiUrl, [
'auth' => [$username, $password],
]);

return $res;
}
}
17 changes: 3 additions & 14 deletions src/Command/DbPropertyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace DbDb\Client\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DbPropertyCommand extends Command
class DbPropertyCommand extends AbstractDbCommand
{
protected function configure()
{
Expand Down Expand Up @@ -37,18 +36,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$key = $input->getArgument('key');
$value = $input->getArgument('value');

$url = getenv('DBDB_URL');
$username = getenv('DBDB_USERNAME');
$password = getenv('DBDB_PASSWORD');

if (!$url || !$username || !$password) {
throw new RuntimeException('Configuration incomplete');
}

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url.'/api/v1/dbs/'.$dbname.'/property/'.$key.'/'.$value, [
'auth' => [$username, $password],
]);
$this->apiUrl = '/api/v1/dbs/'.$dbname.'/property/'.$key.'/'.$value;
$res = parent::execute($input, $output);

echo $res->getBody();
}
Expand Down
41 changes: 26 additions & 15 deletions src/Command/DbSearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace DbDb\Client\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

class DbSearchCommand extends Command
class DbSearchCommand extends AbstractDbCommand
{
protected function configure()
{
Expand All @@ -25,19 +25,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$keyword = $input->getArgument('keyword');

$url = getenv('DBDB_URL');
$username = getenv('DBDB_USERNAME');
$password = getenv('DBDB_PASSWORD');

if (!$url || !$username || !$password) {
throw new RuntimeException('Configuration incomplete');
$this->apiUrl = '/api/v1/dbs/search/'.$keyword;
$res = parent::execute($input, $output);

$result = json_decode($res->getBody());

if ($result) {
$table = new Table($output);
$header = array();
foreach ($result[0] as $key => $value) {
$header[] = $key;
}
$table->setHeaders($header);

foreach ($result as $row) {
$rowArray = array();
foreach ($row as $key => $val) {
$rowArray[] = $val;
}
$table->addRow($rowArray);
}

$table->render();
} else {
echo $res->getBody();
}

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url.'/api/v1/dbs/search/'.$keyword, [
'auth' => [$username, $password],
]);

echo $res->getBody();
}
}
19 changes: 3 additions & 16 deletions src/Command/DbShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace DbDb\Client\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DbShowCommand extends Command
class DbShowCommand extends AbstractDbCommand
{
protected function configure()
{
Expand All @@ -24,20 +23,8 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$dbname = $input->getArgument('dbname');

$url = getenv('DBDB_URL');
$username = getenv('DBDB_USERNAME');
$password = getenv('DBDB_PASSWORD');

if (!$url || !$username || !$password) {
throw new RuntimeException('Configuration incomplete');
}

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url.'/api/v1/dbs/'.$dbname, [
'auth' => [$username, $password],
]);

$this->apiUrl = '/api/v1/dbs/'.$dbname;
$res = parent::execute($input, $output);
echo $res->getBody();
}
}
17 changes: 3 additions & 14 deletions src/Command/SnapshotFullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace DbDb\Client\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SnapshotFullCommand extends Command
class SnapshotFullCommand extends AbstractDbCommand
{
protected function configure()
{
Expand All @@ -25,18 +24,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$dbname = $input->getArgument('dbname');

$url = getenv('DBDB_URL');
$username = getenv('DBDB_USERNAME');
$password = getenv('DBDB_PASSWORD');

if (!$url || !$username || !$password) {
throw new RuntimeException('Configuration incomplete');
}

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url.'/api/v1/snapshots/'.$dbname, [
'auth' => [$username, $password],
]);
$this->apiUrl = '/api/v1/snapshots/'.$dbname;
$res = parent::execute($input, $output);

echo $res->getBody();
}
Expand Down