-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
114 lines (104 loc) · 4.24 KB
/
cli.js
File metadata and controls
114 lines (104 loc) · 4.24 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require('./constants');
const Tcp = require('./client');
const Udp = require('./udp/client');
const yargs = require('yargs');
const fs = require('fs');
checkHelpCommands = (argv) => {
if (argv._[0] === HELP_CONSTANT) {
if (argv._.length > 1) {
if (argv._[1] === GET_CONSTANT) {
console.log('\nUsage: httpc get [-v] [-h key:value] URL\n\n' +
'Get executes a HTTP GET request for a given URL\n\n' +
'Options:\n' +
' -v\t\tPrints the detail of the response such as protocol, status, and headers.\n' +
' -h key:value\tAssociates headers to HTTP Request with the format \'key:value\'.');
process.exit(0)
} else if (argv._[1] === POST_CONSTANT) {
console.log('\nUsage: httpc post [-v] [-h key:value] [-d inline-data] [-f file] URL\n\n' +
'Post executes a HTTP POST request for a given URL with inline data or from file.\n\n' +
'Options:\n' +
' -v\t\tPrints the detail of the response such as protocol, status, and headers.\n' +
' -h key:value\tAssociates headers to HTTP Request with the format \'key:value\'.\n' +
' -d string\tAssociates an inline data to the body HTTP POST request.\n' +
' -f file\tAssociates the content of a file to the body HTTP POST request.\n\n' +
'Either [-d] or [-f] can be used but not both.');
process.exit(0)
} else {
console.log('Invalid command\nhttpc: try \'httpc help\' or \'httpc --help\' for more information\n');
process.exit(0)
}
}
}
};
getHeaders = (argv) => {
let headers_array = [];
if (argv.hasOwnProperty('h')) {
if (Array.isArray(argv.h)) {
headers_array = headers_array.concat(argv.h);
} else {
headers_array.push(argv.h);
}
}
return headers_array;
};
getJSONRequestArguments = (argv, headers) => {
let args = {
method: argv._[0],
url: argv.url,
v: argv.v,
h: headers
};
if (argv.hasOwnProperty('o')) args.o = argv.o;
return args;
};
exports.init = () => {
let argv = yargs.usage('Usage: httpc <command> [arguments]')
.command('get <url> [arguments]', 'Get executes a HTTP GET request' +
' for a given URL and prints response', () => {
}, (argv) => {
if (argv.hasOwnProperty('d')) {
console.log('GET Request cannot be used with -d or --d option');
process.exit(0);
} else if (argv.hasOwnProperty('f')) {
console.log('GET Request cannot be used with -f option');
process.exit(0);
}
const headers = getHeaders(argv);
const args = getJSONRequestArguments(argv, headers);
if (UDP_REQUEST_TYPE) {
Udp.initRequest(args);
} else {
Tcp.initRequest(args);
}
})
.command('post <url> [arguments]', 'Post executes a HTTP POST request and prints the response.', () => {
}, (argv) => {
const headers = getHeaders(argv);
const args = getJSONRequestArguments(argv, headers);
if (argv.hasOwnProperty('d') && argv.hasOwnProperty('f')) {
console.log('Either [-d] or [-f] can be used but not both.');
process.exit(0);
} else if (argv.hasOwnProperty('d')) {
args.d = argv.d;
} else if (argv.hasOwnProperty('f')) {
args.f = argv.f;
fs.access(args.f, fs.constants.F_OK, (err) => {
if (err) console.log(`${args.f} does not exist`);
});
}
if (UDP_REQUEST_TYPE) {
Udp.initRequest(args);
} else {
Tcp.initRequest(args);
}
})
.option('verbose', {
alias: 'v',
default: false,
description: 'Prints the detail of the response such as protocol, status,' +
'and headers.'
})
.help('help')
.argv;
checkHelpCommands(argv);
};