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