Skip to content

Commit 41903da

Browse files
authored
Merge pull request #66 from ccampanale/feat-call-local
Adds `--local` opt to `call` cmd
2 parents 7ef4e61 + 2c19cd3 commit 41903da

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/commands/call.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ async function handler(broker, args) {
108108
}
109109
}
110110

111+
// Remove non-standard call opts
112+
[{
113+
key: "local",
114+
replaceKey: "nodeID",
115+
value: args.nodeID,
116+
}].forEach(opt => {
117+
if (callOpts[opt.key]) {
118+
delete callOpts[opt.key];
119+
opt.replaceKey ? callOpts[opt.replaceKey] = opt.value : undefined;
120+
}
121+
});
122+
111123
const startTime = process.hrtime();
112124
const nodeID = args.nodeID;
113125
meta.$repl = true;
@@ -123,8 +135,8 @@ async function handler(broker, args) {
123135
isStream(payload) ? "" : payload,
124136
meta ? kleur.yellow().bold("with meta:") : "",
125137
meta ? meta : "",
126-
callOpts ? kleur.yellow().bold("with options:") : "",
127-
callOpts ? callOpts : ""
138+
Object.keys(callOpts).length ? kleur.yellow().bold("with options:") : "",
139+
Object.keys(callOpts).length ? callOpts : ""
128140
);
129141

130142
try {
@@ -209,6 +221,7 @@ function declaration(program, broker, cmdHandler) {
209221
"--loadFull [filename]",
210222
'Load params and meta from file (e.g., {"params":{}, "meta":{}, "options":{}})'
211223
)
224+
.option("--$local", "Call the local service broker")
212225
.option("--stream [filename]", "Send a file as stream")
213226
.option("--save [filename]", "Save response to file")
214227
.allowUnknownOption(true)
@@ -228,6 +241,7 @@ function declaration(program, broker, cmdHandler) {
228241
thisCommand.params = {
229242
options: parsedArgs,
230243
actionName,
244+
nodeID: parsedArgs.$local ? broker.nodeID : undefined,
231245
...(jsonParams !== undefined ? { jsonParams } : undefined),
232246
rawCommand,
233247
};

test/call.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ describe("Test 'call' command", () => {
156156
"call math.add --load my-params.json --stream my-picture.jpg --save my-response.json --loadFull params.json",
157157
});
158158
});
159+
160+
it("should call 'call' targeting local broker", async () => {
161+
const command = `call "math.add" --$local --load my-params.json --stream my-picture.jpg --save my-response.json --loadFull params.json`;
162+
163+
await program.parseAsync(
164+
parseArgsStringToArgv(command, "node", "REPL")
165+
);
166+
167+
expect(cmdHandler).toHaveBeenCalledTimes(1);
168+
expect(cmdHandler).toHaveBeenCalledWith(expect.any(ServiceBroker), {
169+
options: {
170+
$local: true,
171+
load: "my-params.json",
172+
stream: "my-picture.jpg",
173+
save: "my-response.json",
174+
loadFull: "params.json",
175+
},
176+
actionName: "math.add",
177+
nodeID: broker.nodeID,
178+
rawCommand:
179+
"call math.add --$local --load my-params.json --stream my-picture.jpg --save my-response.json --loadFull params.json",
180+
});
181+
});
159182
});
160183

161184
describe("Test 'dcall' command", () => {

0 commit comments

Comments
 (0)