2014-10-29 08:02:03 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2023-09-14 22:28:31 +02:00
|
|
|
import Minimist from 'minimist';
|
|
|
|
import GameDig from './../lib/index.js';
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2019-01-14 06:54:36 +01:00
|
|
|
const argv = Minimist(process.argv.slice(2), {
|
2021-07-09 04:51:34 +02:00
|
|
|
boolean: ['pretty','debug','givenPortOnly','requestRules'],
|
2022-12-13 10:46:43 +01:00
|
|
|
string: ['guildId','listenUdpPort','ipFamily']
|
2019-01-14 06:54:36 +01:00
|
|
|
});
|
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
const debug = argv.debug;
|
2014-10-29 08:02:03 +01:00
|
|
|
delete argv.debug;
|
2019-01-12 11:43:36 +01:00
|
|
|
const pretty = !!argv.pretty || debug;
|
|
|
|
delete argv.pretty;
|
2020-08-24 20:27:44 +02:00
|
|
|
const givenPortOnly = argv.givenPortOnly;
|
|
|
|
delete argv.givenPortOnly;
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2023-09-11 01:16:26 +02:00
|
|
|
let options = {};
|
2017-08-09 11:05:55 +02:00
|
|
|
for(const key of Object.keys(argv)) {
|
|
|
|
const value = argv[key];
|
2023-09-11 01:16:26 +02:00
|
|
|
|
|
|
|
if(key === '_' || key.charAt(0) === '$')
|
2017-08-09 12:32:09 +02:00
|
|
|
continue;
|
2023-09-11 01:16:26 +02:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
options[key] = value;
|
2014-10-29 08:02:03 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:43:36 +01:00
|
|
|
if (argv._.length >= 1) {
|
|
|
|
const target = argv._[0];
|
|
|
|
const split = target.split(':');
|
|
|
|
options.host = split[0];
|
|
|
|
if (split.length >= 2) {
|
|
|
|
options.port = split[1];
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 05:38:49 +01:00
|
|
|
if (debug) {
|
|
|
|
options.debug = true;
|
|
|
|
}
|
2020-08-24 20:27:44 +02:00
|
|
|
if (givenPortOnly) {
|
|
|
|
options.givenPortOnly = true;
|
|
|
|
}
|
2017-03-14 09:40:02 +01:00
|
|
|
|
2023-09-11 01:16:26 +02:00
|
|
|
const printOnPretty = (object) => {
|
|
|
|
if(pretty) {
|
|
|
|
console.log(JSON.stringify(object,null,' '));
|
|
|
|
} else {
|
|
|
|
console.log(JSON.stringify(object));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const gamedig = new GameDig(options);
|
2021-05-19 06:13:18 +02:00
|
|
|
gamedig.query(options)
|
2023-09-11 01:16:26 +02:00
|
|
|
.then(printOnPretty)
|
2017-08-09 12:32:09 +02:00
|
|
|
.catch((error) => {
|
2017-08-09 11:05:55 +02:00
|
|
|
if (debug) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
console.log(error.stack);
|
|
|
|
} else {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
error = error.message;
|
|
|
|
}
|
2023-09-11 01:16:26 +02:00
|
|
|
|
|
|
|
printOnPretty({error: error});
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
2017-08-09 12:32:09 +02:00
|
|
|
});
|