UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

178 lines 6.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.slimObject = exports.write = exports.formatAsync = exports.format = void 0; const format_1 = require("../util/format"); const api_1 = require("../rest/api"); const client_1 = require("../session/client"); const errors_1 = require("../util/errors"); function format(obj, options) { var _a; if (typeof obj == 'string') { return obj; } if (obj == null) { // null or undefined return ''; } if (((_a = options === null || options === void 0 ? void 0 : options._hints) === null || _a === void 0 ? void 0 : _a.kind) === 'error') { if (!Array.isArray(obj)) { obj = [obj]; } obj = obj.map((o) => { var _a, _b, _c, _d, _e, _f; if ((_b = (_a = o.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.status) { return (_c = o.response) === null || _c === void 0 ? void 0 : _c.data; } if (o.isAxiosError) { const axiosError = o; return { status: (_d = axiosError.response) === null || _d === void 0 ? void 0 : _d.status, message: (_e = axiosError.response) === null || _e === void 0 ? void 0 : _e.data, }; } return ((_f = o.response) === null || _f === void 0 ? void 0 : _f.data) || o.message; }); } switch (options === null || options === void 0 ? void 0 : options.output) { case 'yaml-slim': obj = slimObject(obj); return toPrettyYaml(obj, options === null || options === void 0 ? void 0 : options.color); case 'yaml': return toPrettyYaml(obj, options === null || options === void 0 ? void 0 : options.color); case 'json-slim': obj = slimObject(obj); return toPrettyJson(obj, options === null || options === void 0 ? void 0 : options.color); case 'json': return toPrettyJson(obj, options === null || options === void 0 ? void 0 : options.color); case 'names': return toNames(obj); default: try { return toPrettyTable(obj, options); } catch (e) { const message = `ERROR: An issue prevented the data from being formatted into a table. Attempt the operation again with '-o json' for output in JSON format. For support, please provide the following error message to the Control Plane team:`; throw new errors_1.PrettifyTableError(message, e); } } } exports.format = format; async function formatAsync(request, obj, options) { switch (options === null || options === void 0 ? void 0 : options.output) { case 'tf': return await toTerraform(request, obj); case 'crd': return await toCrd(request, obj, options); } return format(obj, options); } exports.formatAsync = formatAsync; function write(stream, value) { if (typeof value === 'object') { value = format(value, { output: 'json' }); } if (stream == process.stdout) { console.log(value); } else if (stream == process.stderr) { console.error(value); } else { // only gets here in unit tests // handle in-memory buffers (for testing only) throw new Error('can only work with console streams'); } } exports.write = write; function slimObject(obj) { if (Array.isArray(obj)) { obj = obj.map(slimObject); } else if (obj.kind === 'list') { delete obj.links; obj.items = obj.items.map(slimObject); } else { delete obj.id; delete obj.version; delete obj.created; delete obj.lastModified; delete obj.links; delete obj.status; delete obj.alias; delete obj.origin; delete obj.repository; delete obj.tag; delete obj.manifest; delete obj.digest; if (obj.tags) { delete obj.tags['cpln/deployTimestamp']; } } return obj; } exports.slimObject = slimObject; function toPrettyJson(obj, color) { if (color) { return require('json-colorizer')((0, format_1.toSortedJson)(obj), { pretty: true }) + '\n'; } else { let sorted = (0, format_1.toSortedJson)(obj); return JSON.stringify(sorted, undefined, ' '); } } function toPrettyYaml(obj, color) { if (color) { const highlight = require('cli-highlight').highlight; const fmt = (0, format_1.toSortedYamlString)(obj); return highlight(fmt, { language: 'yaml', ignoreIllegals: true }); } else { return (0, format_1.toSortedYamlString)(obj); } } function toNames(obj) { const objects = getObjectsArray(obj); const names = objects.map((obj) => { if (!obj.hasOwnProperty('name')) { throw new errors_1.FormatNamesError('ERROR: Unable to format to "names", one or more objects are missing the "name" property.'); } return obj.name; }); return names.join('\n').trim(); } function getObjectsArray(obj) { if (Array.isArray(obj)) { return obj; } if (typeof obj === 'object' && obj.kind === 'list' && Array.isArray(obj.items)) { return obj.items; } return [obj]; } function toPrettyTable(obj, options) { // lazily require the table formatter as the lib is big return require('./table').toPrettyTable(obj, options !== null && options !== void 0 ? options : {}); } async function toTerraform(request, obj) { const resource = obj; const selfLink = resource.links.find((link) => link.rel === 'self').href; // Determine service endpoint const serviceEndpoint = await (0, api_1.getDiscoveryEndpoint)(request.endpoint, 'terraform-exporter'); const client = (0, client_1.makeClient)(request, serviceEndpoint); return await client.get(selfLink); } async function toCrd(request, obj, options) { const resource = obj; // Find the self link of the resource so we can export by link const selfLink = resource.links.find((link) => link.rel === 'self').href; // Get the service endpoint const serviceEndpoint = await (0, api_1.getDiscoveryEndpoint)(request.endpoint, 'k8s-crd-exporter'); // Create the client const client = (0, client_1.makeClient)(request, serviceEndpoint); // Get the resource by its self link const response = await client.get(selfLink); // Return the YAML representation of the response return toPrettyYaml(response, options === null || options === void 0 ? void 0 : options.color); } //# sourceMappingURL=format.js.map