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