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