UNPKG

4.68 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
13const stringify = require("json-stringify-safe");
14const automationClient_1 = require("../automationClient");
15const configuration_1 = require("../configuration");
16const ConsoleMessageClient_1 = require("../internal/message/ConsoleMessageClient");
17const logger_1 = require("../internal/util/logger");
18const string_1 = require("../internal/util/string");
19const scan_1 = require("../scan");
20logger_1.LoggingConfig.format = "cli";
21main();
22/**
23 * Parse command line CommandInvocation argument, set up, and call the
24 * command handler. This method will not return.
25 */
26function 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 * Create a simple handler context for running command handlers from
53 * the command line.
54 */
55function createHandlerContext(config) {
56 return {
57 workspaceId: config.workspaceIds[0],
58 correlationId: string_1.guid(),
59 messageClient: ConsoleMessageClient_1.consoleMessageClient,
60 };
61}
62/**
63 * Run a command handler on the command line. This function will not
64 * return.
65 *
66 * @param automationServer automation server with the command
67 * @param ci command and its parameters
68 * @param ctx suitable execution context
69 */
70function invokeOnConsole(automationServer, ci, ctx) {
71 return __awaiter(this, void 0, void 0, function* () {
72 // Set up the parameter, mappend parameters and secrets
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//# sourceMappingURL=command.js.map
\No newline at end of file