-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.php
54 lines (41 loc) · 1.14 KB
/
script.php
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
<?php
$longOpts = [
'url:',
'tag:',
];
$options = getopt('', $longOpts);
if (!isset($options['url'])) {
echo "\033[1;35m".'Usage: php script.php --url <url> [--tag <tag1> ...]'."\033[0m".PHP_EOL;
exit(1);
}
if (empty($options['url'])) {
echo "\033[1;35m".'The --url parameter cannot be empty.'."\033[0m".PHP_EOL;
exit(1);
}
$urlValidate = filter_var($options['url'], FILTER_VALIDATE_URL);
if (false === $urlValidate) {
echo "\033[1;35m".'The provided URL is invalid. Please enter a valid URL.'."\033[0m".PHP_EOL;
exit(1);
}
$addData = [
'url' => $options['url'],
];
if (empty($options['tag'])) {
$addData['tags'] = [];
} elseif (is_string($options['tag'])) {
$addData['tags'] = [
$options['tag'],
];
} elseif (is_array($options['tag'])) {
$addData['tags'] = $options['tag'];
}
$file = 'data.json';
if (file_exists($file)) {
$rawData = json_decode(file_get_contents($file), true) ?: [];
} else {
$rawData = [];
}
$rawData[] = $addData;
$newData = json_encode($rawData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents($file, $newData);
echo 'Operation complete.'.PHP_EOL;