1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
|
9 |
|
10 | import * as stringify from "json-stringify-safe";
|
11 | import { automationClient } from "../lib/automationClient";
|
12 | import {
|
13 | Configuration,
|
14 | loadConfiguration,
|
15 | } from "../lib/configuration";
|
16 | import { HandlerContext } from "../lib/HandlerContext";
|
17 | import { CommandInvocation } from "../lib/internal/invoker/Payload";
|
18 | import { consoleMessageClient } from "../lib/internal/message/ConsoleMessageClient";
|
19 | import { guid } from "../lib/internal/util/string";
|
20 | import { enableDefaultScanning } from "../lib/scan";
|
21 | import { AutomationServer } from "../lib/server/AutomationServer";
|
22 |
|
23 |
|
24 |
|
25 | main()
|
26 | .catch((err: Error) => {
|
27 | console.error(`Unhandled exception: ${err.message}`);
|
28 | process.exit(101);
|
29 | });
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | async function main(): Promise<void> {
|
36 | if (!process.argv[2]) {
|
37 | console.error(`[ERROR] Missing command, you must supply the CommandInvocation on the command line`);
|
38 | process.exit(3);
|
39 | }
|
40 | if (process.argv.length > 3) {
|
41 | console.warn(`[WARN] Extra command line arguments will be ignored: ${process.argv.slice(3).join(" ")}`);
|
42 | }
|
43 | const ciString = process.argv[2];
|
44 | try {
|
45 | const ci: CommandInvocation = JSON.parse(ciString);
|
46 | const configuration = await loadConfiguration();
|
47 | enableDefaultScanning(configuration);
|
48 | const node = automationClient(configuration);
|
49 | await invokeOnConsole(node.automationServer, ci, createHandlerContext(configuration));
|
50 | } catch (e) {
|
51 | console.error(`[ERROR] Unhandled exception: ${e.message}`);
|
52 | process.exit(101);
|
53 | }
|
54 | console.error(`[ERROR] Illegal state: unhandled execution path`);
|
55 | process.exit(99);
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function createHandlerContext(config: Configuration): HandlerContext {
|
63 | return {
|
64 | workspaceId: config.workspaceIds[0],
|
65 | correlationId: guid(),
|
66 | messageClient: consoleMessageClient,
|
67 | };
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | async function invokeOnConsole(automationServer: AutomationServer, ci: CommandInvocation, ctx: HandlerContext): Promise<void> {
|
79 |
|
80 |
|
81 | const handler = automationServer.automations.commands.find(c => c.name === ci.name);
|
82 | if (!handler) {
|
83 | const commands = automationServer.automations.commands.map(c => c.name).join(" ");
|
84 | console.error(`[ERROR] Unable to find command ${ci.name}, available commands: ${commands}`);
|
85 | process.exit(4);
|
86 | }
|
87 | const invocation: CommandInvocation = {
|
88 | name: ci.name,
|
89 | args: ci.args ? ci.args.filter(a =>
|
90 | handler.parameters.some(p => p.name === a.name)) : undefined,
|
91 | mappedParameters: ci.args ? ci.args.filter(a =>
|
92 | handler.mapped_parameters.some(p => p.name === a.name)) : undefined,
|
93 | secrets: ci.args ? ci.args.filter(a => handler.secrets.some(p => p.name === a.name))
|
94 | .map(a => {
|
95 | const s = handler.secrets.find(p => p.name === a.name);
|
96 | return { uri: s.uri, value: String(a.value) };
|
97 | }) : undefined,
|
98 | };
|
99 |
|
100 | try {
|
101 | automationServer.validateCommandInvocation(invocation);
|
102 | } catch (e) {
|
103 | console.error(`[ERROR] Invalid parameters: ${e.message}`);
|
104 | process.exit(2);
|
105 | }
|
106 | try {
|
107 | const result = await automationServer.invokeCommand(invocation, ctx);
|
108 | console.log(`Command succeeded: ${stringify(result, undefined, 2)}`);
|
109 | } catch (e) {
|
110 | console.error(`[ERROR] Command failed: ${stringify(e, undefined, 2)}`);
|
111 | process.exit(1);
|
112 | }
|
113 | process.exit(0);
|
114 | }
|