add output options and days until expiry

このコミットが含まれているのは:
Simon Vieille 2019-05-02 15:28:11 +02:00
コミット d660c94d38
署名者: deblan
GPGキーID: 03383D15A1D31745
2個のファイルの変更78行の追加13行の削除

ファイルの表示

@ -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',
];
}