1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const url = require('url');
|
6 | const util = require('util');
|
7 |
|
8 | const pkg = require('../package.json');
|
9 | const jayson = require('../');
|
10 | const program = require('commander');
|
11 | const eyes = require('eyes');
|
12 | const net = require('net')
|
13 |
|
14 |
|
15 | program.version(pkg.version)
|
16 | .option('-m, --method [name]', 'Method', String)
|
17 | .option('-p, --params [json]', 'Array or Object to use as parameters', JSON.parse)
|
18 | .option('-u, --url [url]', 'URL to server', url.parse)
|
19 | .option('-q, --quiet', 'Only output the response value and any errors', Boolean)
|
20 | .option('-s, --socket [path] or [ip:port]', 'Path to UNIX socket, or TCP socket address', parseSocket)
|
21 | .option('-j, --json', 'Only output the response value as JSON (implies --quiet)')
|
22 | .option('-c, --color', 'Color output', Boolean)
|
23 | .parse(process.argv);
|
24 |
|
25 | const inspect = eyes.inspector({
|
26 | stream: null,
|
27 | styles: program.color ? eyes.defaults.styles : {all: false}
|
28 | });
|
29 |
|
30 |
|
31 | if(program.json) program.quiet = true;
|
32 |
|
33 |
|
34 | const std = {
|
35 | out: getPrinter({ fn: console.log }),
|
36 | err: getPrinter({ fn: console.error })
|
37 | };
|
38 |
|
39 |
|
40 | if(!(program.method && (program.url || program.socket))) {
|
41 | std.err.result(program.helpInformation());
|
42 | return process.exit(-1);
|
43 | }
|
44 |
|
45 | const client = (program.socket && program.socket.host)
|
46 | ? jayson.client.tcp(program.socket)
|
47 | : (program.url && program.url.protocol == 'https:')
|
48 | ? jayson.client.https(program.url || program.socket)
|
49 | : jayson.client.http(program.url || program.socket);
|
50 |
|
51 | std.out.noise(
|
52 | colorize('magenta', '-> %s(%s)'),
|
53 | program.method,
|
54 | Array.isArray(program.params) ? program.params.join(', ') : JSON.stringify(program.params)
|
55 | );
|
56 |
|
57 | client.request(program.method, program.params, function(err, response) {
|
58 | if(err) {
|
59 | std.err.noise(colorize('red', '<- %s'), err.stack);
|
60 | return process.exit(-1);
|
61 | }
|
62 |
|
63 | if(!response) {
|
64 | std.err.noise(colorize('red', '<- %s'), 'empty response');
|
65 | return process.exit(-1);
|
66 | }
|
67 |
|
68 | if(program.json) {
|
69 | std.out.result('%s', JSON.stringify(response).replace("\n", ""));
|
70 | return process.exit(0);
|
71 | }
|
72 |
|
73 | std.out.noise('<- %s', inspect(response), true);
|
74 | process.exit(response.error ? response.error.code : 0);
|
75 | });
|
76 |
|
77 | function parseSocket(value) {
|
78 | const addr = value.split(":");
|
79 |
|
80 | if (addr.length == 2 && (net.isIP(addr[0]) || addr[0].toLowerCase() == "localhost")) {
|
81 | return {port: addr[1], host: addr[0]};
|
82 | }
|
83 |
|
84 | return {socketPath: path.normalize(value)};
|
85 | }
|
86 |
|
87 | function colorize(color, format) {
|
88 | return program.color
|
89 | ? eyes.stylize(format, color, {})
|
90 | : format;
|
91 | }
|
92 |
|
93 | function getPrinter(options) {
|
94 |
|
95 | const fn = options.fn || console.log;
|
96 |
|
97 | return {
|
98 |
|
99 |
|
100 | noise: function() {
|
101 | if(program.quiet) return;
|
102 | return fn.apply(console, arguments);
|
103 | },
|
104 |
|
105 |
|
106 | result: function() {
|
107 | return fn.apply(console, arguments);
|
108 | }
|
109 |
|
110 | };
|
111 | }
|