forked from lieldulev/swagger-to-locustfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·61 lines (53 loc) · 2.25 KB
/
index.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
#!/usr/bin/env node
var program = require('commander'),
chalk = require('chalk'),
package = require('./package.json'),
s2l = require('./lib/swagger2locust'),
getArgs = require('cli-pipe');
program
.description("swagger2locust is a CLI tool that creates locust.io config tasks file from swagger specification.\n\n Website: https://github.com/lieldulev/swagger-to-locustfile\n\n Copyright: (C) 2016 Liel Dulev")
.version(package['version'])
.usage('[options] <file>')
.arguments('<file>')
.option('-m, --min <n>', 'minimum time, in milliseconds, that a simulated user will wait between executing each task (default: 1000).', parseInt)
.option('-x, --max <n>', 'maximum time, in milliseconds, that a simulated user will wait between executing each task (default: 3000).', parseInt)
.option('-H, --host <host>', 'The host attribute is a URL prefix (i.e. “http://google.com”) to the host that is to be loaded.')
.action(function(file, options) {
cmdValue = 'convert';
var actualOptions = s2l.defaultOptions;
if (options.host) { actualOptions['host'] = options.host}
if (options.min) { actualOptions['min_wait'] = options.min}
if (options.max) { actualOptions['max_wait'] = options.max}
if (file.indexOf('{') == 0) { // piped in JSON string
console.log(s2l.convertJSON(file, actualOptions));
} else if (file.indexOf('swagger:') == 0) { // piped in yaml string
console.log(s2l.convertYAML(file, actualOptions));
} else if (!s2l.validFile(file)) { // bad file
console.error(chalk.red("Error: File not found or not a valid swagger yaml/json file: "+file));
console.log(chalk.yellow("Hint: Check that file exsits and extension is either .json or .yaml"));
} else { // good file
console.log(s2l.convertFile(file, actualOptions));
}
});
if(process.stdin.isTTY) {
if (process.argv.length < 3) {
showHelp();
} else {
program.parse(process.argv);
}
}
else {
getArgs(function(argsAndPipe){
if (argsAndPipe.length < 3) {
showHelp();
} else {
program.parse(argsAndPipe);
}
});
}
// Catch missing actions
function showHelp(){
console.log(chalk.yellow("\n Error: Missing swagger specifications input file."));
program.outputHelp();
process.exit(1);
}