control of errors
This commit is contained in:
parent
a21441fe47
commit
1e50339230
|
@ -0,0 +1,150 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const fetch = require('node-fetch');
|
||||
const colors = require('colors');
|
||||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
const getopts = require('getopts');
|
||||
|
||||
const homeDir = os.homedir();
|
||||
const configFile = `${homeDir}/.grafana-find.json`;
|
||||
|
||||
if (!fs.existsSync(configFile)) {
|
||||
console.error(`Configuration file not found: ${configFile}`.red);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const user = process.argv[2];
|
||||
const findAll = process.argv[3];
|
||||
|
||||
const config = require(configFile);
|
||||
const grafanaUrl = config.grafanaUrl;
|
||||
|
||||
const grafanaApi = `${grafanaUrl}/api`;
|
||||
const urlDashboards = `${grafanaApi}/search`;
|
||||
const urlUID = `${grafanaApi}/dashboards/uid/`;
|
||||
let numberOfDashboards = 0;
|
||||
let numberOfPanels = 0;
|
||||
let numberOfVariables = 0;
|
||||
let numberOfObjects = 0;
|
||||
let titlePanels = new Array;
|
||||
let nameVariables = new Array;
|
||||
|
||||
const regexRawSQL = new RegExp(findAll, 'i');
|
||||
|
||||
async function main(){
|
||||
|
||||
const readline = require('readline');
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
rl.stdoutMuted = true;
|
||||
|
||||
const answer = await new Promise(resolve => {
|
||||
rl.question(colors.yellow('Enter your password: '), resolve);
|
||||
rl._writeToOutput = function _writeToOutput(stringToWrite) {
|
||||
if (rl.stdoutMuted)
|
||||
rl.output.write("*");
|
||||
else
|
||||
rl.output.write(stringToWrite);
|
||||
};
|
||||
});
|
||||
const passw = `${answer}`;
|
||||
if (!answer) {
|
||||
console.error(`You need to put a password`.red);
|
||||
process.exit(0);
|
||||
}
|
||||
rl.history = rl.history.slice(1)
|
||||
rl.close();
|
||||
const credentials = `Basic ` + Buffer.from(`${user}:${passw}`).toString('base64');
|
||||
|
||||
console.clear();
|
||||
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
|
||||
|
||||
let responseAllUID = await fetch(urlDashboards, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: credentials
|
||||
}
|
||||
});
|
||||
|
||||
let allUID = await responseAllUID.json();
|
||||
|
||||
if (allUID.message=='invalid username or password') {
|
||||
console.error(`Invalid username or password\n`.red);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
for (let i=0; i < allUID.length; i++) {
|
||||
|
||||
let url = urlUID + allUID[i].uid;
|
||||
let response = await fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: credentials,
|
||||
}
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
|
||||
let isFound = false;
|
||||
|
||||
const dashboard = data.dashboard;
|
||||
if (dashboard) {
|
||||
if (dashboard.panels)
|
||||
for (const panel of dashboard.panels) {
|
||||
if (panel.targets)
|
||||
for (const target of panel.targets) {
|
||||
isFound = regexRawSQL.test(target.rawSql);
|
||||
if (isFound) {
|
||||
if (panel.title)
|
||||
titlePanels.push(panel.title);
|
||||
else
|
||||
titlePanels.push("?");
|
||||
numberOfPanels++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dashboard.templating)
|
||||
for (const list of dashboard.templating.list) {
|
||||
isFound = regexRawSQL.test(list.query);
|
||||
if (isFound) {
|
||||
nameVariables.push(list.name)
|
||||
numberOfVariables++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isFound) {
|
||||
const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}`;
|
||||
console.log(colors.yellow(linkUrl));
|
||||
if (numberOfPanels) {
|
||||
console.log(colors.cyan.bold(`> ${numberOfPanels} panels`));
|
||||
console.log(colors.cyan(titlePanels.toString().split(",")));
|
||||
}
|
||||
if (numberOfVariables) {
|
||||
console.log(colors.magenta.bold(`> ${numberOfVariables} variables`));
|
||||
console.log(colors.magenta(nameVariables.toString().split(",")));
|
||||
}
|
||||
console.log('')
|
||||
numberOfDashboards++;
|
||||
}
|
||||
|
||||
titlePanels = [];
|
||||
nameVariables= [];
|
||||
numberOfObjects = numberOfPanels + numberOfVariables + numberOfObjects
|
||||
numberOfPanels=0;
|
||||
numberOfVariables=0;
|
||||
}
|
||||
|
||||
if (numberOfDashboards==0)
|
||||
console.log(`No results found\n`.red);
|
||||
else
|
||||
console.log(colors.green.bold(`-------- Have been found ${numberOfObjects} results in ${numberOfDashboards} dashboards -------\n`));
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
main();
|
35
main.js
35
main.js
|
@ -1,22 +1,38 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const packageJson = require('./package.json');
|
||||
const fetch = require('node-fetch');
|
||||
const colors = require('colors');
|
||||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
const getopts = require('getopts');
|
||||
|
||||
const homeDir = os.homedir();
|
||||
const configFile = `${homeDir}/.grafana-find.json`;
|
||||
const error = `[ERROR]: `.bold;
|
||||
|
||||
console.log(
|
||||
`Grafana-Find (${packageJson.description})`.yellow.bold,
|
||||
`v${packageJson.version}`.cyan.bold
|
||||
);
|
||||
|
||||
if (!fs.existsSync(configFile)) {
|
||||
console.error(`Configuration file not found: ${configFile}`.red);
|
||||
console.error(`${error}Configuration file not found: ${configFile}\n`.red);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const user = process.argv[2];
|
||||
const findAll = process.argv[3];
|
||||
|
||||
if (!process.argv[2] || process.argv[2]===`?` || process.argv[2]===`/?` || process.argv[2]===`help` || process.argv[2]===`/help`) {
|
||||
console.log(`Usage:`.gray, `grafana-find`, `<user>`.blue, `<string to search>\n`.magenta);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!process.argv[3]) {
|
||||
console.error(`${error}The string to search for is missing\n`.red);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const config = require(configFile);
|
||||
const grafanaUrl = config.grafanaUrl;
|
||||
|
||||
|
@ -42,7 +58,6 @@ async function main(){
|
|||
|
||||
rl.stdoutMuted = true;
|
||||
|
||||
rl.query = "Password : ";
|
||||
const answer = await new Promise(resolve => {
|
||||
rl.question(colors.yellow('Enter your password: '), resolve);
|
||||
rl._writeToOutput = function _writeToOutput(stringToWrite) {
|
||||
|
@ -54,7 +69,7 @@ async function main(){
|
|||
});
|
||||
const passw = `${answer}`;
|
||||
if (!answer) {
|
||||
console.error(`You need to put a password`.red);
|
||||
console.error(`\n${error}You need to put a password\n`.red);
|
||||
process.exit(0);
|
||||
}
|
||||
rl.history = rl.history.slice(1)
|
||||
|
@ -73,8 +88,8 @@ async function main(){
|
|||
|
||||
let allUID = await responseAllUID.json();
|
||||
|
||||
if (allUID.message=='invalid username or password') {
|
||||
console.error(`Invalid username or password\n`.red);
|
||||
if (allUID.message==='invalid username or password') {
|
||||
console.error(`${error}Invalid username or password\n`.red);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
|
@ -141,10 +156,10 @@ async function main(){
|
|||
numberOfVariables=0;
|
||||
}
|
||||
|
||||
if (numberOfDashboards==0)
|
||||
console.log(`No results found\n`.red);
|
||||
else
|
||||
console.log(colors.green.bold(`-------- Have been found ${numberOfObjects} results in ${numberOfDashboards} dashboards -------`));
|
||||
if (numberOfDashboards===0)
|
||||
console.log(`No results found\n`.yellow.bold);
|
||||
|
||||
console.log(colors.green.bold(`-------- Have been found ${numberOfObjects} results in ${numberOfDashboards} dashboards -------\n`));
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
|
|
@ -11,11 +11,6 @@
|
|||
"node": ">=0.1.90"
|
||||
}
|
||||
},
|
||||
"node_modules/getopts": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz",
|
||||
"integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA=="
|
||||
},
|
||||
"node_modules/node-fetch": {
|
||||
"version": "2.6.7",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
Copyright © Jorge Bucaran <<https://jorgebucaran.com>>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,264 +0,0 @@
|
|||
# Getopts
|
||||
|
||||
> Parse CLI arguments.
|
||||
|
||||
- Lightweight drop-in replacement for `minimist` and clones.
|
||||
- Small (180 LOC), focused, no dependencies.
|
||||
- Up to [6x faster](#benchmarks) than alternatives!
|
||||
|
||||
Break up command-line arguments into key-value pairs for easy look-up and retrieval. Built upon [utility conventions](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02) that have been used for decades, Getopts sane defaults help you write CLI tools that look and feel like the real deal.
|
||||
|
||||
```console
|
||||
$ example --type=module -o main.js *.{js,json}
|
||||
```
|
||||
|
||||
```js
|
||||
import getopts from "getopts"
|
||||
|
||||
const options = getopts(process.argv.slice(2), {
|
||||
alias: {
|
||||
output: ["o", "f"],
|
||||
type: "t",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
The result is an object populated with all the parsed arguments.
|
||||
|
||||
```js
|
||||
{
|
||||
_: ["index.js", "package.json"],
|
||||
output: "main.js",
|
||||
type: "module",
|
||||
o: "main.js",
|
||||
f: "main.js",
|
||||
t: "module",
|
||||
}
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```console
|
||||
npm install getopts
|
||||
```
|
||||
|
||||
## Parsing rules
|
||||
|
||||
### Short options
|
||||
|
||||
A short option consists of a `-` followed by a single alphabetic character. Multiple options can be grouped together without spaces. Short options are boolean by default unless followed by an [operand](#operand) (non-option) or if adjacent to any non-alphabetic characters:
|
||||
|
||||
```js
|
||||
getopts(["-ab", "-c"]) //=> { _: [], a:true, b:true, c:true }
|
||||
```
|
||||
|
||||
```js
|
||||
getopts(["-a", "alpha"]) //=> { _: [], a:"alpha" }
|
||||
```
|
||||
|
||||
```js
|
||||
getopts(["-abc1"]) //=> { _: [], a:true, b:true, c:1 }
|
||||
```
|
||||
|
||||
Use [`opts.string`](#optsstring) to parse an option as a string regardless.
|
||||
|
||||
```js
|
||||
getopts(["-kF12"], {
|
||||
string: ["k"],
|
||||
}) //=> { _: [], k:"F12" }
|
||||
```
|
||||
|
||||
The first operand following an option will be used as its value. Use [`opts.boolean`](#optsboolean) to specify that an option should be parsed as a boolean regardless, causing the following argument to be treated as an operand instead.
|
||||
|
||||
```js
|
||||
getopts(["-a", "alpha"], {
|
||||
boolean: ["a"],
|
||||
}) //=> { _: ["alpha"], a:true }
|
||||
```
|
||||
|
||||
Any character listed in the ASCII table can be used as a short option if it's the first character after the dash.
|
||||
|
||||
```js
|
||||
getopts(["-9", "-#10", "-%0.01"]) //=> { _:[], 9:true, #:10, %:0.01 }
|
||||
```
|
||||
|
||||
### Long options
|
||||
|
||||
A long option consists of a `--` followed by a name and a value separated by an `=`. Long options without a value are boolean by default.
|
||||
|
||||
```js
|
||||
getopts(["--turbo", "--warp=10"]) //=> { _: [], turbo:true, warp:10 }
|
||||
```
|
||||
|
||||
```js
|
||||
getopts(["--warp=e=mc^2"]) //=> { _: [], warp:"e=mc^2" }
|
||||
```
|
||||
|
||||
```js
|
||||
getopts(["--@", "alpha"]) //=> { _: [], @:"alpha" }
|
||||
```
|
||||
|
||||
Negated options start with `--no-` and are always `false`.
|
||||
|
||||
```js
|
||||
getopts(["--no-turbo"]) //=> { _: [], turbo:false }
|
||||
```
|
||||
|
||||
### Operands
|
||||
|
||||
Every non-option argument is an operand. Operands are saved to the `result._` operands array.
|
||||
|
||||
```js
|
||||
getopts(["alpha", "-w9", "bravo"]) //=> { _: ["alpha", "bravo"], w:9 }
|
||||
```
|
||||
|
||||
```js
|
||||
getopts(["--code=alpha", "bravo"]) //=> { _: ["bravo"], code:"alpha" }
|
||||
```
|
||||
|
||||
Everything after a standalone `--` is an operand.
|
||||
|
||||
```js
|
||||
getopts(["--alpha", "--", "--bravo", "--turbo"]) //=> { _:["--bravo", "--turbo"], alpha:true }
|
||||
```
|
||||
|
||||
A single `-` is also treated as an operand.
|
||||
|
||||
```js
|
||||
getopts(["--turbo", "-"]) //=> { _:["-"], turbo:true }
|
||||
```
|
||||
|
||||
### Other
|
||||
|
||||
Options specified as boolean or string will be added to the result object as `false` or `""` (even if missing from the arguments array).
|
||||
|
||||
```js
|
||||
getopts([], {
|
||||
string: ["a"],
|
||||
boolean: ["b"],
|
||||
}) //=> { _:[], a:"", b:false }
|
||||
```
|
||||
|
||||
Repeated options are stored as arrays with every value in order of appearance.
|
||||
|
||||
```js
|
||||
getopts(["-x?alpha=bravo", "-x3.14", "-x"] //=> { _:[], a:["?alpha=bravo", 3.14, true] }
|
||||
```
|
||||
|
||||
A value may contain newlines or other control characters.
|
||||
|
||||
```js
|
||||
getopts(["--text=top\n\tbottom"]) //=> { _:[], text:"top\n\tbottom" }
|
||||
```
|
||||
|
||||
`="false"` is converted to boolean by default.
|
||||
|
||||
```js
|
||||
getopts(["--turbo=false"]) //=> { _:[], turbo:false }
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `getopts(argv, opts)`
|
||||
|
||||
Parse command-line arguments. Returns an object mapping argument names to their values.
|
||||
|
||||
### `argv[]`
|
||||
|
||||
An array of arguments, usually [`process.argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv).
|
||||
|
||||
### `opts.alias`
|
||||
|
||||
An object of option aliases. An alias can be a string or an array of strings. Aliases let you declare substitute names for an option, e.g., the short (abbreviated) and long (canonical) variations.
|
||||
|
||||
```js
|
||||
getopts(["-t"], {
|
||||
alias: {
|
||||
turbo: ["t", "T"],
|
||||
},
|
||||
}) //=> { _:[], t:true, T:true, turbo:true }
|
||||
```
|
||||
|
||||
### `opts.boolean`
|
||||
|
||||
An array of options to parse as boolean. In the example below, `t` is parsed as a boolean, causing the following argument to be treated as an operand.
|
||||
|
||||
```js
|
||||
getopts(["-t", "alpha"], {
|
||||
boolean: ["t"],
|
||||
}) //=> { _:["alpha"], t:true }
|
||||
```
|
||||
|
||||
### `opts.string`
|
||||
|
||||
An array of flags to parse as strings. In the example below, `t` is parsed as a string, causing all adjacent characters to be treated as a single value and not as individual options.
|
||||
|
||||
```js
|
||||
getopts(["-atabc"], {
|
||||
string: ["t"],
|
||||
}) //=> { _:[], a:true, t:"abc" }
|
||||
```
|
||||
|
||||
### `opts.default`
|
||||
|
||||
An object of default values for options not present in the arguments array.
|
||||
|
||||
```js
|
||||
getopts(["--warp=10"], {
|
||||
default: {
|
||||
warp: 15,
|
||||
turbo: true,
|
||||
},
|
||||
}) //=> { _:[], warp:10, turbo:true }
|
||||
```
|
||||
|
||||
### `opts.unknown()`
|
||||
|
||||
We call this function for each unknown option. Return `false` to discard the option. Unknown options are those that appear in the arguments array, but are not in `opts.string`, `opts.boolean`, `opts.default`, or `opts.alias`.
|
||||
|
||||
```js
|
||||
getopts(["-abc"], {
|
||||
unknown: (option) => "a" === option,
|
||||
}) //=> { _:[], a:true }
|
||||
```
|
||||
|
||||
### `opts.stopEarly`
|
||||
|
||||
A boolean property. If `true`, the operands array `_` will be populated with all the arguments after the first operand.
|
||||
|
||||
```js
|
||||
getopts(["-w9", "alpha", "--turbo", "bravo"], {
|
||||
stopEarly: true,
|
||||
}) //=> { _:["alpha", "--turbo", "bravo"], w:9 }
|
||||
```
|
||||
|
||||
This property is useful when implementing sub-commands in a CLI.
|
||||
|
||||
```js
|
||||
import getopts from "getopts"
|
||||
import { install, update, uninstall } from "./commands.js"
|
||||
|
||||
const options = getopts(process.argv.slice(2), {
|
||||
stopEarly: true,
|
||||
})
|
||||
|
||||
const [command, subargv] = options._
|
||||
|
||||
if (command === "install") {
|
||||
install(subargv)
|
||||
} else if (command === "update") {
|
||||
update(subargv)
|
||||
} else if (command === "uninstall") {
|
||||
uninstall(subargv)
|
||||
}
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
```console
|
||||
npm --prefix bench start
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE.md)
|
|
@ -1,193 +0,0 @@
|
|||
const EMPTYARR = []
|
||||
const SHORTSPLIT = /$|[!-@[-`{-~][\s\S]*/g
|
||||
const isArray = Array.isArray
|
||||
|
||||
const parseValue = function (any) {
|
||||
if (any === "") return ""
|
||||
if (any === "false") return false
|
||||
const maybe = +any
|
||||
return maybe * 0 === 0 ? maybe : any
|
||||
}
|
||||
|
||||
const parseAlias = function (aliases) {
|
||||
let out = {},
|
||||
alias,
|
||||
prev,
|
||||
any
|
||||
|
||||
for (let key in aliases) {
|
||||
any = aliases[key]
|
||||
alias = out[key] = isArray(any) ? any : [any]
|
||||
|
||||
for (let i = 0; i < alias.length; i++) {
|
||||
prev = out[alias[i]] = [key]
|
||||
|
||||
for (let k = 0; k < alias.length; k++) {
|
||||
if (i !== k) prev.push(alias[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
const parseDefault = function (aliases, defaults) {
|
||||
let out = {},
|
||||
alias,
|
||||
value
|
||||
|
||||
for (let key in defaults) {
|
||||
alias = aliases[key]
|
||||
value = defaults[key]
|
||||
|
||||
out[key] = value
|
||||
|
||||
if (alias === undefined) {
|
||||
aliases[key] = EMPTYARR
|
||||
} else {
|
||||
for (let i = 0; i < alias.length; i++) {
|
||||
out[alias[i]] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
const parseOptions = function (aliases, options, value) {
|
||||
let out = {},
|
||||
key,
|
||||
alias
|
||||
|
||||
if (options !== undefined) {
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
key = options[i]
|
||||
alias = aliases[key]
|
||||
|
||||
out[key] = value
|
||||
|
||||
if (alias === undefined) {
|
||||
aliases[key] = EMPTYARR
|
||||
} else {
|
||||
for (let k = 0, end = alias.length; k < end; k++) {
|
||||
out[alias[k]] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
const write = function (out, key, value, aliases, unknown) {
|
||||
let prev,
|
||||
alias = aliases[key],
|
||||
len = alias === undefined ? -1 : alias.length
|
||||
|
||||
if (len >= 0 || unknown === undefined || unknown(key)) {
|
||||
prev = out[key]
|
||||
|
||||
if (prev === undefined) {
|
||||
out[key] = value
|
||||
} else {
|
||||
if (isArray(prev)) {
|
||||
prev.push(value)
|
||||
} else {
|
||||
out[key] = [prev, value]
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
out[alias[i]] = out[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function (argv, opts) {
|
||||
let unknown = (opts = opts || {}).unknown,
|
||||
aliases = parseAlias(opts.alias),
|
||||
strings = parseOptions(aliases, opts.string, ""),
|
||||
values = parseDefault(aliases, opts.default),
|
||||
bools = parseOptions(aliases, opts.boolean, false),
|
||||
stopEarly = opts.stopEarly,
|
||||
_ = [],
|
||||
out = { _ },
|
||||
key,
|
||||
arg,
|
||||
end,
|
||||
match,
|
||||
value
|
||||
|
||||
for (let i = 0, len = argv.length; i < len; i++) {
|
||||
arg = argv[i]
|
||||
|
||||
if (arg[0] !== "-" || arg === "-") {
|
||||
if (stopEarly) {
|
||||
while (i < len) {
|
||||
_.push(argv[i++])
|
||||
}
|
||||
} else {
|
||||
_.push(arg)
|
||||
}
|
||||
} else if (arg === "--") {
|
||||
while (++i < len) {
|
||||
_.push(argv[i])
|
||||
}
|
||||
} else if (arg[1] === "-") {
|
||||
end = arg.indexOf("=", 2)
|
||||
if (arg[2] === "n" && arg[3] === "o" && arg[4] === "-") {
|
||||
key = arg.slice(5, end >= 0 ? end : undefined)
|
||||
value = false
|
||||
} else if (end >= 0) {
|
||||
key = arg.slice(2, end)
|
||||
value =
|
||||
bools[key] !== undefined ||
|
||||
(strings[key] === undefined
|
||||
? parseValue(arg.slice(end + 1))
|
||||
: arg.slice(end + 1))
|
||||
} else {
|
||||
key = arg.slice(2)
|
||||
value =
|
||||
bools[key] !== undefined ||
|
||||
(len === i + 1 || argv[i + 1][0] === "-"
|
||||
? strings[key] === undefined
|
||||
? true
|
||||
: ""
|
||||
: strings[key] === undefined
|
||||
? parseValue(argv[++i])
|
||||
: argv[++i])
|
||||
}
|
||||
write(out, key, value, aliases, unknown)
|
||||
} else {
|
||||
SHORTSPLIT.lastIndex = 2
|
||||
match = SHORTSPLIT.exec(arg)
|
||||
end = match.index
|
||||
value = match[0]
|
||||
|
||||
for (let k = 1; k < end; k++) {
|
||||
write(
|
||||
out,
|
||||
(key = arg[k]),
|
||||
k + 1 < end
|
||||
? strings[key] === undefined ||
|
||||
arg.substring(k + 1, (k = end)) + value
|
||||
: value === ""
|
||||
? len === i + 1 || argv[i + 1][0] === "-"
|
||||
? strings[key] === undefined || ""
|
||||
: bools[key] !== undefined ||
|
||||
(strings[key] === undefined ? parseValue(argv[++i]) : argv[++i])
|
||||
: bools[key] !== undefined ||
|
||||
(strings[key] === undefined ? parseValue(value) : value),
|
||||
aliases,
|
||||
unknown
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let key in values) if (out[key] === undefined) out[key] = values[key]
|
||||
for (let key in bools) if (out[key] === undefined) out[key] = false
|
||||
for (let key in strings) if (out[key] === undefined) out[key] = ""
|
||||
|
||||
return out
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
/**
|
||||
* @param argv Arguments to parse.
|
||||
* @param options Parsing options (configuration).
|
||||
* @returns The parsed arguments.
|
||||
*/
|
||||
declare function getopts(
|
||||
argv: string[],
|
||||
options?: getopts.Options
|
||||
): getopts.ParsedOptions
|
||||
|
||||
export = getopts
|
||||
|
||||
declare namespace getopts {
|
||||
export interface ParsedOptions {
|
||||
_: string[]
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
export interface Options {
|
||||
alias?: { [key: string]: string | string[] }
|
||||
string?: string[]
|
||||
boolean?: string[]
|
||||
default?: { [key: string]: any }
|
||||
unknown?: (optionName: string) => boolean
|
||||
stopEarly?: boolean
|
||||
}
|
||||
}
|
|
@ -1,193 +0,0 @@
|
|||
const EMPTYARR = []
|
||||
const SHORTSPLIT = /$|[!-@[-`{-~][\s\S]*/g
|
||||
const isArray = Array.isArray
|
||||
|
||||
const parseValue = function (any) {
|
||||
if (any === "") return ""
|
||||
if (any === "false") return false
|
||||
const maybe = +any
|
||||
return maybe * 0 === 0 ? maybe : any
|
||||
}
|
||||
|
||||
const parseAlias = function (aliases) {
|
||||
let out = {},
|
||||
alias,
|
||||
prev,
|
||||
any
|
||||
|
||||
for (let key in aliases) {
|
||||
any = aliases[key]
|
||||
alias = out[key] = isArray(any) ? any : [any]
|
||||
|
||||
for (let i = 0; i < alias.length; i++) {
|
||||
prev = out[alias[i]] = [key]
|
||||
|
||||
for (let k = 0; k < alias.length; k++) {
|
||||
if (i !== k) prev.push(alias[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
const parseDefault = function (aliases, defaults) {
|
||||
let out = {},
|
||||
alias,
|
||||
value
|
||||
|
||||
for (let key in defaults) {
|
||||
alias = aliases[key]
|
||||
value = defaults[key]
|
||||
|
||||
out[key] = value
|
||||
|
||||
if (alias === undefined) {
|
||||
aliases[key] = EMPTYARR
|
||||
} else {
|
||||
for (let i = 0; i < alias.length; i++) {
|
||||
out[alias[i]] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
const parseOptions = function (aliases, options, value) {
|
||||
let out = {},
|
||||
key,
|
||||
alias
|
||||
|
||||
if (options !== undefined) {
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
key = options[i]
|
||||
alias = aliases[key]
|
||||
|
||||
out[key] = value
|
||||
|
||||
if (alias === undefined) {
|
||||
aliases[key] = EMPTYARR
|
||||
} else {
|
||||
for (let k = 0, end = alias.length; k < end; k++) {
|
||||
out[alias[k]] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
const write = function (out, key, value, aliases, unknown) {
|
||||
let prev,
|
||||
alias = aliases[key],
|
||||
len = alias === undefined ? -1 : alias.length
|
||||
|
||||
if (len >= 0 || unknown === undefined || unknown(key)) {
|
||||
prev = out[key]
|
||||
|
||||
if (prev === undefined) {
|
||||
out[key] = value
|
||||
} else {
|
||||
if (isArray(prev)) {
|
||||
prev.push(value)
|
||||
} else {
|
||||
out[key] = [prev, value]
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
out[alias[i]] = out[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default function (argv, opts) {
|
||||
let unknown = (opts = opts || {}).unknown,
|
||||
aliases = parseAlias(opts.alias),
|
||||
strings = parseOptions(aliases, opts.string, ""),
|
||||
values = parseDefault(aliases, opts.default),
|
||||
bools = parseOptions(aliases, opts.boolean, false),
|
||||
stopEarly = opts.stopEarly,
|
||||
_ = [],
|
||||
out = { _ },
|
||||
key,
|
||||
arg,
|
||||
end,
|
||||
match,
|
||||
value
|
||||
|
||||
for (let i = 0, len = argv.length; i < len; i++) {
|
||||
arg = argv[i]
|
||||
|
||||
if (arg[0] !== "-" || arg === "-") {
|
||||
if (stopEarly) {
|
||||
while (i < len) {
|
||||
_.push(argv[i++])
|
||||
}
|
||||
} else {
|
||||
_.push(arg)
|
||||
}
|
||||
} else if (arg === "--") {
|
||||
while (++i < len) {
|
||||
_.push(argv[i])
|
||||
}
|
||||
} else if (arg[1] === "-") {
|
||||
end = arg.indexOf("=", 2)
|
||||
if (arg[2] === "n" && arg[3] === "o" && arg[4] === "-") {
|
||||
key = arg.slice(5, end >= 0 ? end : undefined)
|
||||
value = false
|
||||
} else if (end >= 0) {
|
||||
key = arg.slice(2, end)
|
||||
value =
|
||||
bools[key] !== undefined ||
|
||||
(strings[key] === undefined
|
||||
? parseValue(arg.slice(end + 1))
|
||||
: arg.slice(end + 1))
|
||||
} else {
|
||||
key = arg.slice(2)
|
||||
value =
|
||||
bools[key] !== undefined ||
|
||||
(len === i + 1 || argv[i + 1][0] === "-"
|
||||
? strings[key] === undefined
|
||||
? true
|
||||
: ""
|
||||
: strings[key] === undefined
|
||||
? parseValue(argv[++i])
|
||||
: argv[++i])
|
||||
}
|
||||
write(out, key, value, aliases, unknown)
|
||||
} else {
|
||||
SHORTSPLIT.lastIndex = 2
|
||||
match = SHORTSPLIT.exec(arg)
|
||||
end = match.index
|
||||
value = match[0]
|
||||
|
||||
for (let k = 1; k < end; k++) {
|
||||
write(
|
||||
out,
|
||||
(key = arg[k]),
|
||||
k + 1 < end
|
||||
? strings[key] === undefined ||
|
||||
arg.substring(k + 1, (k = end)) + value
|
||||
: value === ""
|
||||
? len === i + 1 || argv[i + 1][0] === "-"
|
||||
? strings[key] === undefined || ""
|
||||
: bools[key] !== undefined ||
|
||||
(strings[key] === undefined ? parseValue(argv[++i]) : argv[++i])
|
||||
: bools[key] !== undefined ||
|
||||
(strings[key] === undefined ? parseValue(value) : value),
|
||||
aliases,
|
||||
unknown
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let key in values) if (out[key] === undefined) out[key] = values[key]
|
||||
for (let key in bools) if (out[key] === undefined) out[key] = false
|
||||
for (let key in strings) if (out[key] === undefined) out[key] = ""
|
||||
|
||||
return out
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
{
|
||||
"name": "getopts",
|
||||
"version": "2.3.0",
|
||||
"type": "module",
|
||||
"main": "index.cjs",
|
||||
"types": "index.d.ts",
|
||||
"description": "Parse CLI arguments.",
|
||||
"repository": "jorgebucaran/getopts",
|
||||
"license": "MIT",
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"require": "./index.cjs",
|
||||
"import": "./index.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"*.*(c)[tj]s"
|
||||
],
|
||||
"author": "Jorge Bucaran",
|
||||
"keywords": [
|
||||
"cli",
|
||||
"argv",
|
||||
"flags",
|
||||
"parse",
|
||||
"getopts",
|
||||
"minimist",
|
||||
"cli-parser"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "c8 twist tests/*.js",
|
||||
"build": "node -e \"fs.writeFileSync('index.cjs',fs.readFileSync('index.js','utf8').replace(/export default/,'module.exports ='),'utf8')\"",
|
||||
"deploy": "npm test && git commit --all --message $tag && git tag --sign $tag --message $tag && git push && git push --tags",
|
||||
"release": "tag=$npm_package_version npm run deploy && npm publish --access public",
|
||||
"prepare": "npm run build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"c8": "*",
|
||||
"twist": "*"
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@
|
|||
"license": "GPL-3.0",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"getopts": "^2.3.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"object-hash": "^3.0.0"
|
||||
},
|
||||
|
@ -24,11 +23,6 @@
|
|||
"node": ">=0.1.90"
|
||||
}
|
||||
},
|
||||
"node_modules/getopts": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz",
|
||||
"integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA=="
|
||||
},
|
||||
"node_modules/node-fetch": {
|
||||
"version": "2.6.7",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
|
||||
|
@ -82,11 +76,6 @@
|
|||
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
|
||||
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
|
||||
},
|
||||
"getopts": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz",
|
||||
"integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA=="
|
||||
},
|
||||
"node-fetch": {
|
||||
"version": "2.6.7",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
{
|
||||
"name": "grafana-find",
|
||||
"version": "1.0",
|
||||
"author": "Verdnatura Levante SL",
|
||||
"description": "MySQL Version Control",
|
||||
"description": "Grafana Find Tool",
|
||||
"license": "GPL-3.0",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"getopts": "^2.3.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"object-hash": "^3.0.0"
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue