-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcastor.php
120 lines (106 loc) · 3.11 KB
/
castor.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
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
<?php
namespace grafkit;
use Castor\Attribute\AsTask;
use Grafkit\App;
use Grafkit\Lib\ReplacementPairs;
use Grafkit\Lib\SearchResult;
#[AsTask]
function batch_replace(string $filename, bool $dry_run = false): void
{
$pairs = ReplacementPairs::fromFile($filename);
foreach ($pairs as $search => $replace) {
replace($search, $replace, $dry_run);
}
}
#[AsTask]
function cache(string $label): void
{
$label = ($label !== "") ? $label : null;
App::getInstance()->refreshDashboardCache($label);
}
#[AsTask]
function find(string $search): void
{
$searchResults = App::getInstance()->searchDashboards("$search");
displaySearchResults($searchResults, $search);
}
#[AsTask]
function replace(string $search, string $replace, bool $dry_run = false): void
{
$searchResults = App::getInstance()->searchDashboards("$search");
displaySearchResults($searchResults, $search, $replace);
if ($dry_run === false) {
promptForConfirmation();
$replaceResults = App::getInstance()->replaceInDashboards($search, $replace, $searchResults);
displayReplaceResults($replaceResults);
}
}
/**
* @return void
*/
function promptForConfirmation(): void
{
$confirmation = trim(readline("Are you sure you want to proceed? (y/N) "));
if ($confirmation !== 'y') {
echo "Bye!" . PHP_EOL . PHP_EOL;
exit (0);
}
}
/**
* @param SearchResult[] $searchResults
* @param string $search
* @param string|null $replace
* @return void
*/
function displaySearchResults(array $searchResults, string $search, ?string $replace = null): void
{
// Get unique counts
$uniqueUrls = [];
$uniqueLabels = [];
foreach ($searchResults as $searchResult) {
$domain = App::getInstance()->getConfiguration()->hostnames->hostnames[$searchResult->label]->url;
$url = implode('', [$domain, $searchResult->url]);
$uniqueUrls[$url] = true;
$uniqueLabels[$searchResult->label] = true;
}
// Prepare display URLs
$displayUrls = array_keys($uniqueUrls);
sort($displayUrls);
// Display search parameters
echo PHP_EOL;
echo "---" . PHP_EOL;
echo "# SEARCH PARAMETERS" . PHP_EOL;
echo "- Search: {$search}" . PHP_EOL;
if ($replace !== null) {
echo "- Replace: {$replace}" . PHP_EOL;
}
echo PHP_EOL;
// Display dashboard URLs
echo "# DASHBOARDS" . PHP_EOL;
foreach ($displayUrls as $displayUrl) {
echo ("- {$displayUrl}" . PHP_EOL);
}
echo PHP_EOL;
// Display summary
$numResults = count($searchResults);
$numUrls = count($uniqueUrls);
$numLabels = count($uniqueLabels);
echo "# SEARCH RESULTS" . PHP_EOL;
echo "- {$numResults} string matches" . PHP_EOL;
echo "- {$numUrls} dashboards" . PHP_EOL;
echo "- {$numLabels} Grafana instances" . PHP_EOL;
echo PHP_EOL;
}
/**
* @param array $replaceResults
* @return void
*/
function displayReplaceResults(array $replaceResults): void
{
echo PHP_EOL;
echo "# REPLACE RESULTS" . PHP_EOL;
foreach ($replaceResults as $replaceResult) {
echo ("- {$replaceResult}" . PHP_EOL);
}
echo '---' . PHP_EOL;
}