add output options and days until expiry

This commit is contained in:
Simon Vieille 2019-05-02 15:28:11 +02:00
parent 4640e705c1
commit d660c94d38
Signed by: deblan
GPG key ID: 03383D15A1D31745
2 changed files with 78 additions and 13 deletions

View file

@ -15,10 +15,17 @@ $ composer install
``` ```
$ php7.3 ./domain-expiration check example.com other-example.com $ php7.3 ./domain-expiration check example.com other-example.com
+------------------------+---------------------+ +-------------------+------+---------------------+
| Domain | Date | | Domain | Days | Date |
+------------------------+---------------------+ +-------------------+------+---------------------+
| example.com | YYYY-MM-DD HH:MM:SS | | example.com | XX | YYYY-MM-DD HH:MM:SS |
| other-example.com | 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

View file

@ -45,8 +45,11 @@ class CheckCommand extends BaseCommand
{ {
$this $this
->setName('check') ->setName('check')
->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains.') ->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains')
->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the output.') ->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' ->setHelp(<<<'EOF'
The <info>%command.name%</info> retrieves the expiration dates of the given domains. The <info>%command.name%</info> retrieves the expiration dates of the given domains.
@ -65,23 +68,69 @@ EOF
$this->checkDomains(); $this->checkDomains();
$table = new Table($output); $short = $this->input->getOption('short');
$table->setHeaders(['Domain', 'Date']); $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'); $table->setStyle('compact');
} }
foreach ($this->sort($this->successes) as $result) { foreach ($successes as $result) {
$table->addRow([ $table->addRow([
$result['domain'], $result['domain'],
$result['dayUntilExpiry'],
$this->createDateRender($result['expiryDate']), $this->createDateRender($result['expiryDate']),
]); ]);
} }
foreach ($this->sort($this->fails) as $result) { foreach ($fails as $result) {
$table->addRow([ $table->addRow([
$result['domain'], $result['domain'],
$result['dayUntilExpiry'],
'FAIL', 'FAIL',
]); ]);
} }
@ -139,9 +188,18 @@ EOF
$parser = new Parser($whois); $parser = new Parser($whois);
$expiryDate = $parser->getExpiryDate(); $expiryDate = $parser->getExpiryDate();
if ($expiryDate) {
$comparison = $expiryDate->getTimestamp();
$dayUntilExpiry = floor(($expiryDate->getTimestamp() - time()) / 3600 / 24);
} else {
$comparison = 'FAIL';
$dayUntilExpiry = null;
}
return [ return [
'domain' => $domain, 'domain' => $domain,
'expiryDate' => $expiryDate, 'expiryDate' => $expiryDate,
'dayUntilExpiry' => $dayUntilExpiry,
'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL', 'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL',
]; ];
} }