-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
81 lines (73 loc) · 1.58 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* moleculer-cli
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer-cli)
* MIT Licensed
*/
const connectHandler = require("../connect-handler");
/**
* Yargs command
*/
module.exports = {
command: "call <actionName>",
describe: "Connect & call a service",
builder(yargs) {
yargs.options({
"config": {
alias: "c",
default: "",
describe: "Load configuration from a file",
type: "string"
},
"transporter": {
alias: "t",
default: null,
describe: "Transporter connection string (NATS, nats://127.0.0.1:4222, ...etc)",
type: "string"
},
"ns": {
default: "",
describe: "Namespace",
type: "string"
},
"level": {
default: "silent",
describe: "Logging level",
type: "string"
},
"id": {
default: null,
describe: "NodeID",
type: "string"
},
"serializer": {
default: null,
describe: "Serializer",
type: "string"
}
});
},
async handler(opts) {
try {
//console.log(opts);
const broker = await connectHandler(opts);
const params = {};
const meta = {};
Object.keys(opts).map(key => {
if (key.startsWith("@"))
params[key.slice(1)] = opts[key];
if (key.startsWith("#"))
meta[key.slice(1)] = opts[key];
});
if (opts.level != "silent") {
console.log("Params:", params);
console.log("Meta:", meta);
}
const res = await broker.call(opts.actionName, params, { meta });
console.log(JSON.stringify(res, null, 4));
await broker.stop();
} catch(err) {
console.error("ERROR", err);
process.exit(1);
}
}
};