-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
78 lines (68 loc) · 2.34 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Args } from "https://deno.land/[email protected]/flags/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { getLink } from "./elk.ts";
import { parseFilters, removeUndefined } from "./transform.ts";
function parseArguments(args: string[]): Args {
return parse(args, {
boolean: ["follow"],
negatable: ["follow"],
string: ["space", "stack", "stage", "app"],
collect: ["column", "filter"],
stopEarly: false,
"--": true,
default: {
follow: true,
column: ["message"],
space: "default",
filter: [],
},
});
}
function printHelp(): void {
console.log(`Usage: devx-logs [OPTIONS...]`);
console.log("\nOptional flags:");
console.log(" --help Display this help and exit");
console.log(" --space The Kibana space to use");
console.log(" --stack The stack tag to filter by");
console.log(" --stage The stage tag to filter by");
console.log(" --app The app tag to filter by");
console.log(
" --column Which columns to display. Multiple: true. Default: 'message'",
);
console.log(
" --filter Additional filters to apply. Multiple: true. Format: key=value",
);
console.log(" --no-follow Don't open the link in the browser");
console.log("\nExample:");
console.log(
" devx-logs --space devx --stack deploy --stage PROD --app riff-raff",
);
console.log("\nAdvanced example:");
console.log(
" devx-logs --space devx --stack deploy --stage PROD --app riff-raff --filter level=INFO --filter region=eu-west-1 --column message --column logger_name",
);
}
function main(inputArgs: string[]) {
const args = parseArguments(inputArgs);
if (args.help) {
printHelp();
Deno.exit(0);
}
const { space, stack, stage, app, column, filter, follow } = args;
const mergedFilters: Record<string, string | undefined> = {
...parseFilters(filter),
"stack.keyword": stack,
"stage.keyword": stage,
"app.keyword": app,
};
const filters = removeUndefined(mergedFilters);
const link = getLink(space, filters, column);
console.log(link);
if (follow) {
new Deno.Command("open", { args: [link] }).spawn();
}
}
// Learn more at https://deno.land/manual/examples/module_metadata#concepts
if (import.meta.main) {
main(Deno.args);
}