UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

164 lines (156 loc) 5.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderResponse = exports.parseAsYaml = exports.parseAsJson = void 0; exports.resolveTeamNames = resolveTeamNames; var _chalk = _interopRequireDefault(require("chalk")); var _dayjs = _interopRequireDefault(require("dayjs")); var _easyTable = _interopRequireDefault(require("easy-table")); var _jsYaml = require("js-yaml"); var _get = _interopRequireDefault(require("lodash/get")); var _set = _interopRequireDefault(require("lodash/set")); var _CoreConfigController = require("./CoreConfigController"); var _amplifyCliUtils = require("@axway/amplify-cli-utils"); var _types = require("./types"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * HACK: removing "---" delimiter printing from the lib. * Currently this is not supported in library itself so have to override prototype methods. */ _easyTable.default.prototype.pushDelimeter = function () { return this; }; /** * Parse JSON object | array of objects as YAML * @param response request response payload * @param console current console * @returns parsed string with YAML objects representation */ const parseAsYaml = response => { let result = ''; if (Array.isArray(response)) { for (const i of response) { result += `\n---\n${(0, _jsYaml.dump)(i)}`; } } else { result = (0, _jsYaml.dump)(response); } return result; }; /** * Parse JSON object | array of objects as simple text table, * NOTE: currently can build table only for "Environment" type. * @param response request response payload * @param console current console * @param columns columns config from CommandLineInterface resource definition * @returns parsed string with table objects representation */ exports.parseAsYaml = parseAsYaml; const parseAsTable = (response, columns) => { const data = Array.isArray(response) ? response : [response]; const t = new _easyTable.default(); for (const i of data) { for (const col of columns) { // jsonPath starts with '.' so using the substring let value = (0, _get.default)(i, col.jsonPath.substring(1)); let deletingState = (0, _get.default)(i, "metadata.state"); if (col.type === 'date') { value = (0, _dayjs.default)(value).fromNow(); } else if (col.type === 'teamGuid' && !value) { value = _chalk.default.gray('---'); } else if (value && value.length > _types.MAX_TABLE_STRING_LENGTH + 3) { value = value.substring(0, _types.MAX_TABLE_STRING_LENGTH / 2) + '...' + value.substring(value.length - _types.MAX_TABLE_STRING_LENGTH / 2); } if (deletingState) { t.cell(col.name.toUpperCase(), _chalk.default.yellow(value)); } else { t.cell(col.name.toUpperCase(), value); } } t.newRow(); } return data.length ? `\n${t.toString()}` : '\nNo resources found.'; }; /** * Parse JSON object | array of objects as is but without any replacing like [object Object]. * @param response request response payload * @param console current console * @returns parsed string with JSON objects representation */ const parseAsJson = response => JSON.stringify(response, null, 4); /** * Util function to render JSON object | array of objects based on output type provided * @param response request response payload * @param output type of output to render (table (default) / yaml / json) * @param console current console */ exports.parseAsJson = parseAsJson; const renderResponse = (console, response, output, columns) => { switch (output) { case _types.OutputTypes.yaml: console.log(parseAsYaml(response)); break; case _types.OutputTypes.json: console.log(parseAsJson(response)); break; default: // @ts-ignore TODO: fix types error once more types are used console.log(parseAsTable(response, columns)); } }; exports.renderResponse = renderResponse; /** * If a team guid column is being rendered, it resolves the team name and injects it into * the response payload. * @param columns an array of columns being rendered * @param response request response payload */ async function resolveTeamNames({ columns, response }) { // check that we even have a team guid column const column = columns === null || columns === void 0 ? void 0 : columns.find(col => col.type === 'teamGuid'); if (!column || !_CoreConfigController.CoreConfigController.devOpsAccount) { return; } const jsonPath = column.jsonPath.substring(1); const results = Array.isArray(response) ? response : [response]; const teamNames = {}; const { devOpsAccount } = _CoreConfigController.CoreConfigController; const { sdk } = (0, _amplifyCliUtils.initSDK)({ env: devOpsAccount.auth.env }); // build the team name lookup const { teams } = await sdk.team.list(devOpsAccount); for (const team of teams) { teamNames[team.guid] = team.name; } // create the new jsonPath and update the column const targetJsonPath = jsonPath.split('.').slice(0, -1).join('.') + '.teamName'; column.jsonPath = `.${targetJsonPath}`; // next loop over data and set the team name for (let { data } of results) { if (!data || typeof data !== 'object') { continue; } if (!Array.isArray(data)) { data = [data]; } for (const obj of data) { const value = (0, _get.default)(obj, jsonPath, null); if (value !== null) { (0, _set.default)(obj, targetJsonPath, value && teamNames[value] || value || ''); } } } }