improvements

This commit is contained in:
Guillermo Bonet 2022-06-21 17:54:01 +02:00
parent 03d083179f
commit d1a873ba2a
2 changed files with 35 additions and 13 deletions

View File

@ -26,7 +26,9 @@ Create a file ".grafana-find.json" with this content:
```text ```text
{ {
"grafanaUrl": "<server>" "grafanaUrl": "<server>",
"user": "<user>",
"password": "<pass>"
} }
``` ```

44
main.js
View File

@ -25,7 +25,6 @@ const usage = {
const opts = getopts(process.argv.slice(2), { const opts = getopts(process.argv.slice(2), {
alias: { alias: {
user: 'u',
version: 'v', version: 'v',
help: 'h' help: 'h'
}, },
@ -39,15 +38,16 @@ if (opts.version) {
process.exit(); process.exit();
} }
if (opts.help) { if (opts.help) {
console.log(`Usage:`.gray, `grafana-find`, `<user>`.blue, `<string to search>\n`.magenta); console.log(`Usage:`.gray, `grafana-find`, `<string to search>`.magenta);
process.exit(); process.exit();
} }
if (!opts._[0]) { if (!opts._[0]) {
console.error(`${error}The string to search for is missing\n`.red); console.error(`${error}The string to search for is missing`.red);
process.exit(1); process.exit(1);
} }
if (opts._.length > 1) { if (opts._.length > 1) {
console.error(`${error}This command doesn't allow more parameters\n`.red); console.error(`${error}This command doesn't allow more parameters`.red);
process.exit(1); process.exit(1);
} }
@ -87,6 +87,24 @@ let nameVariables = new Array;
const regexRawSQL = new RegExp(findAll, 'i'); const regexRawSQL = new RegExp(findAll, 'i');
async function main(){ async function main(){
if (!user) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const answer = await new Promise(resolve => {
rl.question(colors.green('Enter your user: '), resolve);
});
user = `${answer}`;
if (!answer) {
console.error(`\n${error}You need to put a user\n`.red);
process.exit(0);
}
rl.close();
}
if (!passw) { if (!passw) {
const readline = require('readline'); const readline = require('readline');
const rl = readline.createInterface({ const rl = readline.createInterface({
@ -131,10 +149,9 @@ async function main(){
console.clear(); console.clear();
console.log( console.log(
`----------`.white, `--------- Grafana-Find (${packageJson.description})`.yellow.bold,
`Grafana-Find (${packageJson.description})`.yellow.bold,
`v${packageJson.version}`.cyan.bold, `v${packageJson.version}`.cyan.bold,
`---------`.white `--------`.yellow.bold,
); );
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`)); console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
@ -184,6 +201,7 @@ async function main(){
let data = await response.json(); let data = await response.json();
let isFound = false; let isFound = false;
let isFoundSomething = false;
const dashboard = data.dashboard; const dashboard = data.dashboard;
if (dashboard) { if (dashboard) {
@ -196,8 +214,9 @@ async function main(){
if (panel.title) if (panel.title)
titlePanels.push(panel.title); titlePanels.push(panel.title);
else else
titlePanels.push("?"); titlePanels.push(`null`);
numberOfPanels++; numberOfPanels++;
isFoundSomething=true;
} }
} }
} }
@ -208,13 +227,14 @@ async function main(){
if (isFound) { if (isFound) {
nameVariables.push(list.name) nameVariables.push(list.name)
numberOfVariables++; numberOfVariables++;
isFoundSomething=true;
} }
} }
} }
if (isFound) { if (isFoundSomething) {
const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}`; const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}?orgId=${AllOrganizations[x].id}`;
console.log(colors.yellow(linkUrl)); console.log((linkUrl).yellow.underline, dashboard.title);
if (numberOfPanels) { if (numberOfPanels) {
console.log(colors.cyan.bold(`> ${numberOfPanels} panels`)); console.log(colors.cyan.bold(`> ${numberOfPanels} panels`));
console.log(colors.cyan(titlePanels.toString().split(","))); console.log(colors.cyan(titlePanels.toString().split(",")));
@ -235,7 +255,7 @@ async function main(){
} }
if (!numberOfDashboards) if (!numberOfDashboards)
console.log(`No results found\n`.yellow.bold); console.log(`No results found\n`.green);
}; };