add output options and days until expiry
This commit is contained in:
parent
4640e705c1
commit
d660c94d38
19
README.md
19
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
|
||||
|
|
|
@ -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 <info>%command.name%</info> 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',
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue