1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
|
13 | const stringify = require("json-stringify-safe");
|
14 | const automationClient_1 = require("../lib/automationClient");
|
15 | const configuration_1 = require("../lib/configuration");
|
16 | const ConsoleMessageClient_1 = require("../lib/internal/message/ConsoleMessageClient");
|
17 | const string_1 = require("../lib/internal/util/string");
|
18 | const scan_1 = require("../lib/scan");
|
19 | main();
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function main() {
|
25 | return __awaiter(this, void 0, void 0, function* () {
|
26 | if (!process.argv[2]) {
|
27 | console.error(`[ERROR] Missing command, you must supply the CommandInvocation on the command line`);
|
28 | process.exit(3);
|
29 | }
|
30 | if (process.argv.length > 3) {
|
31 | console.warn(`[WARN] Extra command line arguments will be ignored: ${process.argv.slice(3).join(" ")}`);
|
32 | }
|
33 | const ciString = process.argv[2];
|
34 | try {
|
35 | const ci = JSON.parse(ciString);
|
36 | const configuration = yield configuration_1.loadConfiguration();
|
37 | scan_1.enableDefaultScanning(configuration);
|
38 | const node = automationClient_1.automationClient(configuration);
|
39 | yield invokeOnConsole(node.automationServer, ci, createHandlerContext(configuration));
|
40 | }
|
41 | catch (e) {
|
42 | console.error(`[ERROR] Unhandled exception: ${e.message}`);
|
43 | process.exit(101);
|
44 | }
|
45 | console.error(`[ERROR] Illegal state: unhandled execution path`);
|
46 | process.exit(99);
|
47 | });
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function createHandlerContext(config) {
|
54 | return {
|
55 | workspaceId: config.workspaceIds[0],
|
56 | correlationId: string_1.guid(),
|
57 | messageClient: ConsoleMessageClient_1.consoleMessageClient,
|
58 | };
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function invokeOnConsole(automationServer, ci, ctx) {
|
69 | return __awaiter(this, void 0, void 0, function* () {
|
70 |
|
71 | const handler = automationServer.automations.commands.find(c => c.name === ci.name);
|
72 | if (!handler) {
|
73 | const commands = automationServer.automations.commands.map(c => c.name).join(" ");
|
74 | console.error(`[ERROR] Unable to find command ${ci.name}, available commands: ${commands}`);
|
75 | process.exit(4);
|
76 | }
|
77 | const invocation = {
|
78 | name: ci.name,
|
79 | args: ci.args ? ci.args.filter(a => handler.parameters.some(p => p.name === a.name)) : undefined,
|
80 | mappedParameters: ci.args ? ci.args.filter(a => handler.mapped_parameters.some(p => p.name === a.name)) : undefined,
|
81 | secrets: ci.args ? ci.args.filter(a => handler.secrets.some(p => p.name === a.name))
|
82 | .map(a => {
|
83 | const s = handler.secrets.find(p => p.name === a.name);
|
84 | return { uri: s.uri, value: a.value };
|
85 | }) : undefined,
|
86 | };
|
87 | try {
|
88 | automationServer.validateCommandInvocation(invocation);
|
89 | }
|
90 | catch (e) {
|
91 | console.error(`[ERROR] Invalid parameters: ${e.message}`);
|
92 | process.exit(2);
|
93 | }
|
94 | try {
|
95 | const result = yield automationServer.invokeCommand(invocation, ctx);
|
96 | console.log(`Command succeeded: ${stringify(result, null, 2)}`);
|
97 | }
|
98 | catch (e) {
|
99 | console.error(`[ERROR] Command failed: ${stringify(e, null, 2)}`);
|
100 | process.exit(1);
|
101 | }
|
102 | process.exit(0);
|
103 | });
|
104 | }
|
105 |
|
\ | No newline at end of file |