-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-freq.ts
64 lines (60 loc) · 1.47 KB
/
get-freq.ts
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
import { cli, Command, Flags } from "https://deno.land/x/[email protected]/mod.ts";
import {
connect,
Empty,
ErrorCode,
JSONCodec,
NatsConnection,
ServiceErrorHeader,
} from "./natslib.ts";
const root = cli({
use: "get-freq ",
run: async (
cmd: Command,
_args: string[],
flags: Flags,
): Promise<number> => {
const nc = await createConnection(flags);
return nc.request(
"badge.freq",
Empty,
{ timeout: 30 * 1000 },
).then((msg) => {
if (msg.headers?.get(ServiceErrorHeader)) {
cmd.stdout(
`error processing your request: ${
msg.headers.get(ServiceErrorHeader)
}`,
);
return 1;
}
const jc = JSONCodec();
const m = jc.decode(msg.data);
if (Object.keys(m).length) {
console.table(m);
} else {
cmd.stdout("no badge generation requests seen");
}
return 0;
}).catch((err) => {
if (err.code === ErrorCode.NoResponders) {
cmd.stdout(`sorry no frequency-service services were found`);
} else {
cmd.stdout(`error: ${err.message}`);
}
return 1;
});
},
});
root.addFlag({
name: "server",
type: "string",
usage: "NATS server to connect to",
default: "demo.nats.io",
persistent: true,
});
function createConnection(flags: Flags): Promise<NatsConnection> {
const servers = [flags.value<string>("server")];
return connect({ servers });
}
Deno.exit(await root.execute(Deno.args));