From d660c94d3851d078957c1b65f7ff0b0aaeb01cfc Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 2 May 2019 15:28:11 +0200 Subject: [PATCH] add output options and days until expiry --- README.md | 19 +++++--- src/Deblan/Command/CheckCommand.php | 72 ++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 753465c..de84457 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,17 @@ $ composer install ``` $ php7.3 ./domain-expiration check example.com other-example.com -+------------------------+---------------------+ -| Domain | Date | -+------------------------+---------------------+ -| example.com | YYYY-MM-DD HH:MM:SS | -| other-example.com | YYYY-MM-DD HH:MM:SS | -+------------------------+---------------------+ ++-------------------+------+---------------------+ +| Domain | Days | Date | ++-------------------+------+---------------------+ +| example.com | XX | YYYY-MM-DD HH:MM:SS | +| other-example.com | XXX | YYYY-MM-DD HH:MM:SS | ++-------------------+------+---------------------+ ``` + +You are able specify output options: + +* `--table` (`-t`): output is a table (default) + * `--short` (`-s`): removes borders + * `--no-header`: removes headers +* `--json` (`-j`): output is a json diff --git a/src/Deblan/Command/CheckCommand.php b/src/Deblan/Command/CheckCommand.php index 9795eca..b84ae95 100644 --- a/src/Deblan/Command/CheckCommand.php +++ b/src/Deblan/Command/CheckCommand.php @@ -45,8 +45,11 @@ class CheckCommand extends BaseCommand { $this ->setName('check') - ->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains.') - ->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the output.') + ->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains') + ->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the table output') + ->addOption('no-headers', null, InputOption::VALUE_NONE, 'Remove the table headers') + ->addOption('json', 'j', InputOption::VALUE_NONE, 'Select json as output') + ->addOption('table', 't', InputOption::VALUE_NONE, 'Select table as output') ->setHelp(<<<'EOF' The %command.name% retrieves the expiration dates of the given domains. @@ -65,23 +68,69 @@ EOF $this->checkDomains(); - $table = new Table($output); - $table->setHeaders(['Domain', 'Date']); + $short = $this->input->getOption('short'); + $json = $this->input->getOption('json'); + $table = $this->input->getOption('table') || !$json; + $noHeaders = $this->input->getOption('no-headers'); - if ($this->input->getOption('short')) { + $successes = $this->sort($this->successes); + $fails = $this->sort($this->fails); + + if ($json) { + return $this->output->write(json_encode(array_merge($successes, $fails))); + } + + if ($table) { + $this->renderTable($successes, $fails, $short, $noHeaders); + } + } + + /** + * Render a Json. + * + * @param array $successes + * @param array $fails + */ + protected function renderJson(array $successes, array $fails):void + { + $data = array_merge($successes, $fails); + $json = json_encode($data); + + $this->output->write($json); + } + + /** + * Renders a Table. + * + * @param array $successes + * @param array $fails + * @param bool $short + * @param bool $noHeader + */ + protected function renderTable(array $successes, array $fails, bool $short, bool $noHeaders):void + { + $table = new Table($this->output); + + if (!$noHeaders) { + $table->setHeaders(['Domain', 'Days', 'Date']); + } + + if ($short) { $table->setStyle('compact'); } - foreach ($this->sort($this->successes) as $result) { + foreach ($successes as $result) { $table->addRow([ $result['domain'], + $result['dayUntilExpiry'], $this->createDateRender($result['expiryDate']), ]); } - foreach ($this->sort($this->fails) as $result) { + foreach ($fails as $result) { $table->addRow([ $result['domain'], + $result['dayUntilExpiry'], 'FAIL', ]); } @@ -139,9 +188,18 @@ EOF $parser = new Parser($whois); $expiryDate = $parser->getExpiryDate(); + if ($expiryDate) { + $comparison = $expiryDate->getTimestamp(); + $dayUntilExpiry = floor(($expiryDate->getTimestamp() - time()) / 3600 / 24); + } else { + $comparison = 'FAIL'; + $dayUntilExpiry = null; + } + return [ 'domain' => $domain, 'expiryDate' => $expiryDate, + 'dayUntilExpiry' => $dayUntilExpiry, 'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL', ]; }