UNPKG

4.13 kBPlain TextView Raw
1#!/usr/bin/env node
2/*
3 * Copyright © 2018 Atomist, Inc.
4 *
5 * See LICENSE file.
6 */
7
8process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
9
10import * as stringify from "json-stringify-safe";
11import { automationClient } from "../lib/automationClient";
12import {
13 Configuration,
14 loadConfiguration,
15} from "../lib/configuration";
16import { HandlerContext } from "../lib/HandlerContext";
17import { CommandInvocation } from "../lib/internal/invoker/Payload";
18import { consoleMessageClient } from "../lib/internal/message/ConsoleMessageClient";
19import { guid } from "../lib/internal/util/string";
20import { enableDefaultScanning } from "../lib/scan";
21import { AutomationServer } from "../lib/server/AutomationServer";
22
23/* tslint:disable:no-console */
24
25main()
26 .catch((err: Error) => {
27 console.error(`Unhandled exception: ${err.message}`);
28 process.exit(101);
29 });
30
31/**
32 * Parse command line CommandInvocation argument, set up, and call the
33 * command handler. This method will not return.
34 */
35async function main(): Promise<void> {
36 if (!process.argv[2]) {
37 console.error(`[ERROR] Missing command, you must supply the CommandInvocation on the command line`);
38 process.exit(3);
39 }
40 if (process.argv.length > 3) {
41 console.warn(`[WARN] Extra command line arguments will be ignored: ${process.argv.slice(3).join(" ")}`);
42 }
43 const ciString = process.argv[2];
44 try {
45 const ci: CommandInvocation = JSON.parse(ciString);
46 const configuration = await loadConfiguration();
47 enableDefaultScanning(configuration);
48 const node = automationClient(configuration);
49 await invokeOnConsole(node.automationServer, ci, createHandlerContext(configuration));
50 } catch (e) {
51 console.error(`[ERROR] Unhandled exception: ${e.message}`);
52 process.exit(101);
53 }
54 console.error(`[ERROR] Illegal state: unhandled execution path`);
55 process.exit(99);
56}
57
58/**
59 * Create a simple handler context for running command handlers from
60 * the command line.
61 */
62function createHandlerContext(config: Configuration): HandlerContext {
63 return {
64 workspaceId: config.workspaceIds[0],
65 correlationId: guid(),
66 messageClient: consoleMessageClient,
67 };
68}
69
70/**
71 * Run a command handler on the command line. This function will not
72 * return.
73 *
74 * @param automationServer automation server with the command
75 * @param ci command and its parameters
76 * @param ctx suitable execution context
77 */
78async function invokeOnConsole(automationServer: AutomationServer, ci: CommandInvocation, ctx: HandlerContext): Promise<void> {
79
80 // Set up the parameter, mappend parameters and secrets
81 const handler = automationServer.automations.commands.find(c => c.name === ci.name);
82 if (!handler) {
83 const commands = automationServer.automations.commands.map(c => c.name).join(" ");
84 console.error(`[ERROR] Unable to find command ${ci.name}, available commands: ${commands}`);
85 process.exit(4);
86 }
87 const invocation: CommandInvocation = {
88 name: ci.name,
89 args: ci.args ? ci.args.filter(a =>
90 handler.parameters.some(p => p.name === a.name)) : undefined,
91 mappedParameters: ci.args ? ci.args.filter(a =>
92 handler.mapped_parameters.some(p => p.name === a.name)) : undefined,
93 secrets: ci.args ? ci.args.filter(a => handler.secrets.some(p => p.name === a.name))
94 .map(a => {
95 const s = handler.secrets.find(p => p.name === a.name);
96 return { uri: s.uri, value: String(a.value) };
97 }) : undefined,
98 };
99
100 try {
101 automationServer.validateCommandInvocation(invocation);
102 } catch (e) {
103 console.error(`[ERROR] Invalid parameters: ${e.message}`);
104 process.exit(2);
105 }
106 try {
107 const result = await automationServer.invokeCommand(invocation, ctx);
108 console.log(`Command succeeded: ${stringify(result, undefined, 2)}`);
109 } catch (e) {
110 console.error(`[ERROR] Command failed: ${stringify(e, undefined, 2)}`);
111 process.exit(1);
112 }
113 process.exit(0);
114}