From a6a2439b32792738651746de7cb63d1e81776eee Mon Sep 17 00:00:00 2001 From: "kaushik.prajapati" Date: Tue, 2 Jan 2018 16:06:40 +0530 Subject: [PATCH] Card #540: DbDb client Enhancement --- src/Command/AbstractDbCommand.php | 30 +++++++++++++++++++++ src/Command/DbPropertyCommand.php | 17 +++--------- src/Command/DbSearchCommand.php | 41 ++++++++++++++++++----------- src/Command/DbShowCommand.php | 19 +++---------- src/Command/SnapshotFullCommand.php | 17 +++--------- 5 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 src/Command/AbstractDbCommand.php diff --git a/src/Command/AbstractDbCommand.php b/src/Command/AbstractDbCommand.php new file mode 100644 index 0000000..db594df --- /dev/null +++ b/src/Command/AbstractDbCommand.php @@ -0,0 +1,30 @@ +request('GET', $url.$this->apiUrl, [ + 'auth' => [$username, $password], + ]); + + return $res; + } +} diff --git a/src/Command/DbPropertyCommand.php b/src/Command/DbPropertyCommand.php index 818aa81..4045433 100644 --- a/src/Command/DbPropertyCommand.php +++ b/src/Command/DbPropertyCommand.php @@ -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() { @@ -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(); } diff --git a/src/Command/DbSearchCommand.php b/src/Command/DbSearchCommand.php index efa2f8e..0602c31 100644 --- a/src/Command/DbSearchCommand.php +++ b/src/Command/DbSearchCommand.php @@ -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() { @@ -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(); } } diff --git a/src/Command/DbShowCommand.php b/src/Command/DbShowCommand.php index 686b62b..c6eb706 100644 --- a/src/Command/DbShowCommand.php +++ b/src/Command/DbShowCommand.php @@ -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() { @@ -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(); } } diff --git a/src/Command/SnapshotFullCommand.php b/src/Command/SnapshotFullCommand.php index aa3ae5d..92760b9 100644 --- a/src/Command/SnapshotFullCommand.php +++ b/src/Command/SnapshotFullCommand.php @@ -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() { @@ -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(); }