-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.php
87 lines (69 loc) · 2.25 KB
/
items.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
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
<?php
include 'vendor/autoload.php';
use Alfred\Workflows\Workflow;
use Cloudflare\API\Auth\APIToken;
use Cloudflare\API\Auth\APIKey;
$workflow = new Workflow();
$cacheSeconds = $workflow->env('CACHE_SECONDS', 30);
$hasToken = $workflow->env('CLOUDFLARE_API_TOKEN');
$hasGlobalCredentials = $workflow->env('CLOUDFLARE_EMAIL') &&
$workflow->env('CLOUDFLARE_API_KEY');
if (!$hasToken && !$hasGlobalCredentials) {
$workflow->output('You must provide an API token or email+key credentials in the workflow environment variables.');
return;
}
$data = $workflow->cache()->readJson(null, false);
$lastCached = $data->saved ?? null;
$now = time();
$shouldRefreshCache = ! $lastCached || ($now - $lastCached) > $cacheSeconds;
if ($shouldRefreshCache) {
$workflow->logger()->info('Refreshing data...');
if ($hasToken) {
$authorization = new APIToken(
$workflow->env('CLOUDFLARE_API_TOKEN')
);
} else {
$authorization = new APIKey(
$workflow->env('CLOUDFLARE_EMAIL'),
$workflow->env('CLOUDFLARE_API_KEY')
);
}
$adapter = new Cloudflare\API\Adapter\Guzzle($authorization);
$zones = (new Cloudflare\API\Endpoints\Zones($adapter))->listZones()->result ?? [];
$workflow->logger()->log((new Cloudflare\API\Endpoints\Zones($adapter))->listZones());
$data = (object)[
'zones' => stripUnnecessaryZoneProperties($zones),
'saved' => $now,
];
$workflow->cache()->writeJson($data);
} else {
$workflow->logger()->info('Using cached data...');
}
$zones = $data->zones ?? [];
foreach ($zones as $zone) {
$dashboardUrl = sprintf('https://dash.cloudflare.com/%s/%s',
$zone->account->id,
$zone->name
);
$workflow->item()
->title($zone->name)
->arg($dashboardUrl);
}
$workflow->output();
/**
* Removes properties from each zone that we don’t need to save.
* @param $zones
* @return mixed
*/
function stripUnnecessaryZoneProperties($zones): array
{
$keepProperties = ['id', 'name', 'status', 'account'];
foreach ($zones as &$zone) {
foreach ($zone as $key => $value) {
if (! in_array($key, $keepProperties, true)) {
unset($zone->{$key});
}
}
}
return $zones;
}