-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
jolinotif
executable file
·233 lines (192 loc) · 6.41 KB
/
jolinotif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env php
<?php
use Joli\JoliNotif\DefaultNotifier;
use Joli\JoliNotif\Notification;
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
$loader = require(__DIR__ . '/vendor/autoload.php');
} elseif (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
$loader = require(__DIR__ . '/../../../vendor/autoload.php');
} else {
throw new \RuntimeException('Unable to load autoloader.');
}
final class Cli
{
const DESCRIPTION = 'Send notifications to your desktop directly from your terminal.';
const RULES = [
'title' => [
'name' => 'title',
'info' => 'Notification title.',
'required' => true,
],
'body' => [
'name' => 'body',
'info' => 'Notification body.',
'required' => true,
],
'icon' => [
'name' => 'icon',
'info' => 'Notification icon.',
'required' => false,
],
'subtitle' => [
'name' => 'subtitle',
'info' => 'Notification subtitle. Only works on macOS (AppleScriptNotifier).',
'required' => false,
],
'sound' => [
'name' => 'sound',
'info' => 'Notification sound. Only works on macOS (AppleScriptNotifier).',
'required' => false,
],
'url' => [
'name' => 'url',
'info' => 'Notification url. Only works on macOS (TerminalNotifierNotifier).',
'required' => false,
],
'help' => [
'name' => 'help',
'info' => 'Show this help.',
'required' => false,
'flag' => true,
],
'verbose' => [
'name' => 'verbose',
'info' => 'Output debug information.',
'required' => false,
'flag' => true,
],
];
/** @var array<string, mixed> */
private array $arguments = [];
private readonly string $command;
public function __construct()
{
$this->command = $_SERVER['argv'][0];
}
public function parse(): void
{
$options = '';
$longOptions = array_map(function ($rule) {
$flag = $rule['required'] ? ':' : '::';
return $rule['name'] . $flag;
}, self::RULES);
$this->arguments = getopt($options, $longOptions) ?: [];
}
public function getOption(string $name): mixed
{
return $this->arguments[$name] ?: false;
}
public function getStringOption(string $name): string
{
$option = $this->getOption($name);
if (is_array($option)) {
throw new Exception("Option --{$name} can be specified only once.");
}
if (!is_string($option) && !is_numeric($option)) {
// Probably not possible to reach this point
throw new Exception("Invalid type given for option --{$name}.");
}
return (string) $option;
}
public function hasOption(string $name): bool
{
return isset($this->arguments[$name]);
}
public function validate(): bool
{
$valid = true;
foreach (self::RULES as $rule) {
if ($rule['required'] && !$this->hasOption($rule['name'])) {
$this->log("Please specify notification {$rule['name']} with the option --{$rule['name']}");
$valid = false;
}
if ($this->hasOption($rule['name']) && is_array($this->getOption($rule['name']))) {
$this->log("Option --{$rule['name']} can be specified only once.");
$valid = false;
}
}
return $valid;
}
public function showUsage(): void
{
$required = [];
$optional = [];
$usage = $this->command;
foreach (self::RULES as $name => $rule) {
$prefix = $postfix = '';
if ($rule['required']) {
$required[$name] = $rule;
} else {
$optional[$name] = $rule;
$prefix = '[';
$postfix = ']';
}
$usage .= ' ' . $prefix . $this->formatUsage($name, $rule) . $postfix;
}
$this->log(self::DESCRIPTION);
$this->log(PHP_EOL . 'Usage: ' . trim($usage));
$this->log(PHP_EOL . 'Required Arguments:');
foreach ($required as $name => $info) {
$value = $this->formatUsage($name, $info);
$this->log("\t{$value}");
$this->log("\t\t{$info['info']}");
}
$this->log(PHP_EOL . 'Optional Arguments:');
foreach ($optional as $name => $info) {
$value = $this->formatUsage($name, $info);
$this->log("\t{$value}");
$this->log("\t\t{$info['info']}");
}
}
public function log(string $message): void
{
echo $message . PHP_EOL;
}
/** @param array{name: string, info: string, required: bool, flag?: bool} $rule */
private function formatUsage(string $name, array $rule): string
{
$example = $rule['required'] ? " {$name}" : "=\"{$name}\"";
$value = isset($rule['flag']) && $rule['flag'] ? '' : $example;
return '--' . $name . $value;
}
}
$cli = new Cli();
try {
$cli->parse();
} catch (Exception $e) {
$cli->log($e->getMessage());
exit(1);
}
if ($cli->hasOption('help')) {
$cli->showUsage();
exit(0);
}
if (!$cli->validate()) {
exit(1);
}
$notifier = new DefaultNotifier();
$notification = (new Notification())
->setTitle($cli->getStringOption('title'))
->setBody($cli->getStringOption('body'));
if ($cli->hasOption('icon')) {
$notification->setIcon($cli->getStringOption('icon'));
}
if ($cli->hasOption('subtitle')) {
$notification->addOption('subtitle', $cli->getStringOption('subtitle'));
}
if ($cli->hasOption('sound')) {
$notification->addOption('sound', $cli->getStringOption('sound'));
}
if ($cli->hasOption('url')) {
$notification->addOption('url', $cli->getStringOption('url'));
}
$result = $notifier->send($notification);
$driver = $notifier->getDriver();
if ($cli->hasOption('verbose')) {
if (!$driver) {
$cli->log('No driver available to display a notification on your system.');
} else {
$cli->log(sprintf('Notification %s with %s. ', $result ? 'successfully sent' : 'failed', str_replace('Joli\\JoliNotif\\Driver\\', '', $driver::class)));
}
}
exit($result ? 0 : 1);