@atomist/automation-client
Version:
Atomist API for software low-level client
107 lines • 4.68 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
const stringify = require("json-stringify-safe");
const automationClient_1 = require("../automationClient");
const configuration_1 = require("../configuration");
const ConsoleMessageClient_1 = require("../internal/message/ConsoleMessageClient");
const logger_1 = require("../internal/util/logger");
const string_1 = require("../internal/util/string");
const scan_1 = require("../scan");
logger_1.LoggingConfig.format = "cli";
main();
/**
* Parse command line CommandInvocation argument, set up, and call the
* command handler. This method will not return.
*/
function main() {
return __awaiter(this, void 0, void 0, function* () {
if (!process.argv[2]) {
console.error(`[ERROR] Missing command, you must supply the CommandInvocation on the command line`);
process.exit(3);
}
if (process.argv.length > 3) {
console.warn(`[WARN] Extra command line arguments will be ignored: ${process.argv.slice(3).join(" ")}`);
}
const ciString = process.argv[2];
try {
const ci = JSON.parse(ciString);
const configuration = yield configuration_1.loadConfiguration();
scan_1.enableDefaultScanning(configuration);
const node = automationClient_1.automationClient(configuration);
yield invokeOnConsole(node.automationServer, ci, createHandlerContext(configuration));
}
catch (e) {
console.error(`[ERROR] Unhandled exception: ${e.message}`);
process.exit(101);
}
console.error(`[ERROR] Illegal state: unhandled execution path`);
process.exit(99);
});
}
/**
* Create a simple handler context for running command handlers from
* the command line.
*/
function createHandlerContext(config) {
return {
workspaceId: config.workspaceIds[0],
correlationId: string_1.guid(),
messageClient: ConsoleMessageClient_1.consoleMessageClient,
};
}
/**
* Run a command handler on the command line. This function will not
* return.
*
* @param automationServer automation server with the command
* @param ci command and its parameters
* @param ctx suitable execution context
*/
function invokeOnConsole(automationServer, ci, ctx) {
return __awaiter(this, void 0, void 0, function* () {
// Set up the parameter, mappend parameters and secrets
const handler = automationServer.automations.commands.find(c => c.name === ci.name);
if (!handler) {
const commands = automationServer.automations.commands.map(c => c.name).join(" ");
console.error(`[ERROR] Unable to find command ${ci.name}, available commands: ${commands}`);
process.exit(4);
}
const invocation = {
name: ci.name,
args: ci.args ? ci.args.filter(a => handler.parameters.some(p => p.name === a.name)) : undefined,
mappedParameters: ci.args ? ci.args.filter(a => handler.mapped_parameters.some(p => p.name === a.name)) : undefined,
secrets: ci.args ? ci.args.filter(a => handler.secrets.some(p => p.name === a.name))
.map(a => {
const s = handler.secrets.find(p => p.name === a.name);
return { uri: s.uri, value: a.value };
}) : undefined,
};
try {
automationServer.validateCommandInvocation(invocation);
}
catch (e) {
console.error(`[ERROR] Invalid parameters: ${e.message}`);
process.exit(2);
}
try {
const result = yield automationServer.invokeCommand(invocation, ctx);
console.log(`Command succeeded: ${stringify(result, null, 2)}`);
}
catch (e) {
console.error(`[ERROR] Command failed: ${stringify(e, null, 2)}`);
process.exit(1);
}
process.exit(0);
});
}
//# sourceMappingURL=command.js.map