Browse Source

add output options and days until expiry

develop
Simon Vieille 2 months ago
parent
commit
d660c94d38
Signed by: Simon Vieille <simon@deblan.fr> GPG Key ID: 03383D15A1D31745
2 changed files with 78 additions and 13 deletions
  1. 13
    6
      README.md
  2. 65
    7
      src/Deblan/Command/CheckCommand.php

+ 13
- 6
README.md View File

@@ -15,10 +15,17 @@ $ composer install
15 15
 
16 16
 ```
17 17
 $ php7.3 ./domain-expiration check example.com other-example.com
18
-+------------------------+---------------------+
19
-| Domain                 | Date                |
20
-+------------------------+---------------------+
21
-| example.com            | YYYY-MM-DD HH:MM:SS |
22
-| other-example.com      | YYYY-MM-DD HH:MM:SS |
23
-+------------------------+---------------------+
18
++-------------------+------+---------------------+
19
+| Domain            | Days | Date                |
20
++-------------------+------+---------------------+
21
+| example.com       | XX   | YYYY-MM-DD HH:MM:SS |
22
+| other-example.com | XXX  | YYYY-MM-DD HH:MM:SS |
23
++-------------------+------+---------------------+
24 24
 ```
25
+
26
+You are able specify output options:
27
+
28
+* `--table` (`-t`): output is a table (default)
29
+  * `--short` (`-s`): removes borders
30
+  * `--no-header`: removes headers
31
+* `--json` (`-j`): output is a json

+ 65
- 7
src/Deblan/Command/CheckCommand.php View File

@@ -45,8 +45,11 @@ class CheckCommand extends BaseCommand
45 45
     {
46 46
         $this
47 47
             ->setName('check')
48
-            ->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains.')
49
-            ->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the output.')
48
+            ->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains')
49
+            ->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the table output')
50
+            ->addOption('no-headers', null, InputOption::VALUE_NONE, 'Remove the table headers')
51
+            ->addOption('json', 'j', InputOption::VALUE_NONE, 'Select json as output')
52
+            ->addOption('table', 't', InputOption::VALUE_NONE, 'Select table as output')
50 53
             ->setHelp(<<<'EOF'
51 54
 The <info>%command.name%</info> retrieves the expiration dates of the given domains.
52 55
 
@@ -65,23 +68,69 @@ EOF
65 68
 
66 69
         $this->checkDomains();
67 70
 
68
-        $table = new Table($output);
69
-        $table->setHeaders(['Domain', 'Date']);
71
+        $short = $this->input->getOption('short');
72
+        $json = $this->input->getOption('json');
73
+        $table = $this->input->getOption('table') || !$json;
74
+        $noHeaders = $this->input->getOption('no-headers');
70 75
 
71
-        if ($this->input->getOption('short')) {
76
+        $successes = $this->sort($this->successes);
77
+        $fails = $this->sort($this->fails);
78
+
79
+        if ($json) {
80
+            return $this->output->write(json_encode(array_merge($successes, $fails)));
81
+        }
82
+
83
+        if ($table) {
84
+            $this->renderTable($successes, $fails, $short, $noHeaders);
85
+        }
86
+    }
87
+
88
+    /**
89
+     * Render a Json.
90
+     *
91
+     * @param array $successes
92
+     * @param array $fails
93
+     */
94
+    protected function renderJson(array $successes, array $fails):void
95
+    {
96
+        $data = array_merge($successes, $fails);
97
+        $json = json_encode($data);
98
+
99
+        $this->output->write($json);
100
+    }
101
+
102
+    /**
103
+     * Renders a Table.
104
+     *
105
+     * @param array $successes
106
+     * @param array $fails
107
+     * @param bool  $short
108
+     * @param bool  $noHeader
109
+     */
110
+    protected function renderTable(array $successes, array $fails, bool $short, bool $noHeaders):void
111
+    {
112
+        $table = new Table($this->output);
113
+
114
+        if (!$noHeaders) {
115
+            $table->setHeaders(['Domain', 'Days', 'Date']);
116
+        }
117
+
118
+        if ($short) {
72 119
             $table->setStyle('compact');
73 120
         }
74 121
 
75
-        foreach ($this->sort($this->successes) as $result) {
122
+        foreach ($successes as $result) {
76 123
             $table->addRow([
77 124
                 $result['domain'],
125
+                $result['dayUntilExpiry'],
78 126
                 $this->createDateRender($result['expiryDate']),
79 127
             ]);
80 128
         }
81 129
 
82
-        foreach ($this->sort($this->fails) as $result) {
130
+        foreach ($fails as $result) {
83 131
             $table->addRow([
84 132
                 $result['domain'],
133
+                $result['dayUntilExpiry'],
85 134
                 'FAIL',
86 135
             ]);
87 136
         }
@@ -139,9 +188,18 @@ EOF
139 188
         $parser = new Parser($whois);
140 189
         $expiryDate = $parser->getExpiryDate();
141 190
 
191
+        if ($expiryDate) {
192
+            $comparison = $expiryDate->getTimestamp();
193
+            $dayUntilExpiry = floor(($expiryDate->getTimestamp() - time()) / 3600 / 24);
194
+        } else {
195
+            $comparison = 'FAIL';
196
+            $dayUntilExpiry = null;
197
+        }
198
+
142 199
         return [
143 200
             'domain' => $domain,
144 201
             'expiryDate' => $expiryDate,
202
+            'dayUntilExpiry' => $dayUntilExpiry,
145 203
             'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL',
146 204
         ];
147 205
     }

Loading…
Cancel
Save