This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·78 lines (70 loc) · 3.15 KB
/
app.js
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
#!/usr/bin/env node
const cli = require('commander');
const colors = require('colors');
const { getQuestionDataToFile, FILE_FORMATS } = require('./lib');
function parseList(val) {
return val.split(',');
}
function errorAndExitWithHelp(msg) {
console.error(colors.red(msg));
cli.help();
process.exit();
}
let host;
let username;
let password;
let start;
let end;
cli.version('1.0.0')
.arguments('<host> <username> <password> [start] [end]')
.option('-t, --topics <topics>', 'A comma separated list of topics to filter questions by. If multiple topics are supplied, the question must be ALL topics to be returned.', parseList)
.option('-s, --space <space>', 'Filter by questions belonging to a certain space. ')
.option('-p, --page-size <pageSize>', 'The page size to use for each request. Lower it if the tool seems to fail or be slow.', parseInt, 15)
.option('-f, --file-type <format>', `Export the data in a particular format (default is csv). \n\t Formats available: ${FILE_FORMATS.join(',')}`)
.option('-c, --concurrency <concurrency> ', 'Number of concurrent API requests allowed. Lower concurrency == Less Stress on API', parseInt, 10)
.action((hostVal, usernameVal, passwordVal, startTime, endTime) => {
host = hostVal;
username = usernameVal;
password = passwordVal;
if (startTime) start = Date.parse(startTime);
if (endTime) end = (endTime && Date.parse(endTime)) || Date.now();
})
.on('--help', () => {
console.log('');
console.log('[start] and [end] represent date range filters.');
console.log('They must be provided in ISO-8061 format.');
console.log('Examples:');
console.log(' 2008-09-15');
console.log(' 2008-09-15T15:53:00');
console.log(' 2008-09-15T15:53:00+05:00');
})
.parse(process.argv);
// validate
if (!host) errorAndExitWithHelp('No host was provided. Please provide the hostname of your Answerhub server. Ex. apidocs.cloud.answerhub.com');
if (!username) errorAndExitWithHelp('No username was provided');
if (!password) errorAndExitWithHelp('No password was provided');
if (start !== undefined && isNaN(start)) errorAndExitWithHelp('Start time could not be parsed. Ensure it is in ISO-8061 format.');
if (end !== undefined && isNaN(end)) errorAndExitWithHelp('End time could not be parsed. Ensure it is in ISO-8061 format.');
if (start >= end) errorAndExitWithHelp('Start time must be before the end time.');
const options = {
pageSize: cli.pageSize,
fileType: cli.fileType || 'csv',
concurrency: cli.concurrency,
};
if (start) options.start = start;
if (end) options.end = end;
if (cli.space) options.space = cli.space;
if (cli.topics) options.topics = cli.topics.join(',');
if (cli.fileType && !FILE_FORMATS.includes(cli.fileType)) {
errorAndExitWithHelp('Invalid File Format provided.');
}
getQuestionDataToFile(host, username, password, options).then(
(result) => {
const { fileName, questionsCount } = result;
if (questionsCount > 0) {
console.log(colors.green(`Wrote ${questionsCount} question data payloads to ${fileName}.`));
} else {
console.log(colors.yellow('No questions were found with the given filters.'));
}
},
).catch(err => console.log(colors.red(err)));