From d1a873ba2a9e19fbd1d44d38c3380dae260352fc Mon Sep 17 00:00:00 2001
From: guillermo <guillermo@verdnatura.es>
Date: Tue, 21 Jun 2022 17:54:01 +0200
Subject: [PATCH] improvements

---
 README.md |  4 +++-
 main.js   | 44 ++++++++++++++++++++++++++++++++------------
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/README.md b/README.md
index b2f7ad8..486af18 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,9 @@ Create a file ".grafana-find.json" with this content:
 
 ```text
 {
-	"grafanaUrl": "<server>"
+	"grafanaUrl": "<server>",
+	"user": "<user>",
+	"password": "<pass>"
 }
 ```
 
diff --git a/main.js b/main.js
index 055d399..45feb68 100644
--- a/main.js
+++ b/main.js
@@ -25,7 +25,6 @@ const usage = {
 
 const opts = getopts(process.argv.slice(2), {
     alias: {
-        user: 'u',
         version: 'v',
         help: 'h'
     },
@@ -39,15 +38,16 @@ if (opts.version) {
     process.exit();
 }
 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();
 }
+
 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);
 }
 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);
 }
 
@@ -87,6 +87,24 @@ let nameVariables = new Array;
 const regexRawSQL = new RegExp(findAll, 'i');
 
 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) {
         const readline = require('readline');
         const rl = readline.createInterface({
@@ -131,10 +149,9 @@ async function main(){
 
     console.clear();
     console.log(
-        `----------`.white,
-        `Grafana-Find (${packageJson.description})`.yellow.bold,
+        `--------- Grafana-Find (${packageJson.description})`.yellow.bold,
         `v${packageJson.version}`.cyan.bold,
-        `---------`.white
+        `--------`.yellow.bold,
     );
     console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
 
@@ -184,6 +201,7 @@ async function main(){
             let data = await response.json();
 
             let isFound = false;
+            let isFoundSomething = false;
 
             const dashboard = data.dashboard;
             if (dashboard) {
@@ -196,8 +214,9 @@ async function main(){
                             if (panel.title)
                                 titlePanels.push(panel.title);
                             else
-                                titlePanels.push("?");
+                                titlePanels.push(`null`);
                             numberOfPanels++;
+                            isFoundSomething=true;
                         }
                     }
                 }
@@ -208,13 +227,14 @@ async function main(){
                     if (isFound) {
                         nameVariables.push(list.name)
                         numberOfVariables++;
+                        isFoundSomething=true;
                     }
                 }
             }
 
-            if (isFound) {
-                const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}`;
-                console.log(colors.yellow(linkUrl));
+            if (isFoundSomething) {
+                const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}?orgId=${AllOrganizations[x].id}`;
+                console.log((linkUrl).yellow.underline, dashboard.title);
                 if (numberOfPanels) {
                     console.log(colors.cyan.bold(`> ${numberOfPanels} panels`));
                     console.log(colors.cyan(titlePanels.toString().split(",")));
@@ -235,7 +255,7 @@ async function main(){
         }
 
         if (!numberOfDashboards)
-            console.log(`No results found\n`.yellow.bold);
+            console.log(`No results found\n`.green);
 
     };