UNPKG

msbot

Version:

MSBot command line tool for manipulating Microsoft Bot Framework .bot files

119 lines 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright(c) Microsoft Corporation.All rights reserved. * Licensed under the MIT License. */ // tslint:disable:no-console // tslint:disable:no-object-literal-type-assertion const botframework_config_1 = require("botframework-config"); const chalk = require("chalk"); const program = require("commander"); const fs = require("fs-extra"); const stdioAsync_1 = require("./stdioAsync"); const utils_1 = require("./utils"); program.Command.prototype.unknownOption = (flag) => { console.error(chalk.default.redBright(`Unknown arguments: ${flag}`)); showErrorHelp(); }; program .name('msbot connect cosmosdb') .description('Connect the bot to Azure CosmosDb Service') .option('-n, --name <name>', 'friendly name (defaults to serviceName)') .option('-t, --tenantId <tenantId>', 'Azure Tenant id (either GUID or xxx.onmicrosoft.com)') .option('-s, --subscriptionId <subscriptionId>', 'Azure Subscription Id') .option('-r, --resourceGroup <resourceGroup>', 'Azure resource group name') .option('--serviceName <serviceName>', 'Azure service name') .option('-e, --endpoint <endpoint>', 'CosmosDB endpoint url') .option('-k, --key <key>', 'CosmosDB auth key') .option('-d, --database <database>', 'database name') .option('-c, --collection <collection>', 'collection name') .option('-b, --bot <path>', 'path to bot file. If omitted, local folder will look for a .bot file') .option('--input <jsonfile>', 'path to arguments in JSON format { id:\'\',name:\'\', ... }') .option('--secret <secret>', 'bot file secret password for encrypting service secrets') .option('--stdin', 'arguments are passed in as JSON object via stdin') .option('--prefix', 'Append [msbot] prefix to all messages') .action((cmd, actions) => undefined); const command = program.parse(process.argv); const args = {}; Object.assign(args, command); if (args.stdin) { //force verbosity output if args are passed via stdin process.env.PREFIX = 'prefix'; } if (process.argv.length < 3) { program.help(); } else { if (!args.bot) { botframework_config_1.BotConfiguration.loadBotFromFolder(process.cwd(), args.secret) .then(processConnectAzureArgs) .catch((reason) => { console.error(chalk.default.redBright(reason.toString().split('\n')[0])); showErrorHelp(); }); } else { botframework_config_1.BotConfiguration.load(args.bot, args.secret) .then(processConnectAzureArgs) .catch((reason) => { console.error(chalk.default.redBright(reason.toString().split('\n')[0])); showErrorHelp(); }); } } async function processConnectAzureArgs(config) { if (args.stdin) { Object.assign(args, JSON.parse(await utils_1.getStdin())); } else if (args.input != null) { Object.assign(args, await fs.readJSON(args.input)); } if (!args.serviceName || args.serviceName.length === 0) { throw new Error('Bad or missing --serviceName'); } if (!args.tenantId || args.tenantId.length === 0) { throw new Error('Bad or missing --tenantId'); } if (!args.subscriptionId || !utils_1.uuidValidate(args.subscriptionId)) { throw new Error('Bad or missing --subscriptionId'); } if (!args.resourceGroup || args.resourceGroup.length === 0) { throw new Error('Bad or missing --resourceGroup'); } if (!args.endpoint || args.endpoint.length === 0) { throw new Error('Bad or missing --key'); } if (!args.key || args.key.length === 0) { throw new Error('Bad or missing --key'); } if (!args.database || args.database.length === 0) { throw new Error('Bad or missing --database'); } if (!args.collection || args.collection.length === 0) { throw new Error('Bad or missing --collection'); } const service = new botframework_config_1.CosmosDbService({ name: args.hasOwnProperty('name') ? args.name : args.serviceName, serviceName: args.serviceName, tenantId: args.tenantId, subscriptionId: args.subscriptionId, resourceGroup: args.resourceGroup, endpoint: args.endpoint, key: args.key, database: args.database, collection: args.collection }); const id = config.connectService(service); await config.save(args.secret); await stdioAsync_1.stdoutAsync(JSON.stringify(config.findService(id), null, 2)); return config; } function showErrorHelp() { program.outputHelp((str) => { console.error(str); return ''; }); process.exit(1); } //# sourceMappingURL=msbot-connect-cosmosdb.js.map