@controlplane/cli
Version:
Control Plane Corporation CLI
149 lines • 5.36 kB
JavaScript
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; return ((_a = o.response) === null || _a === void 0 ? void 0 : _a.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) {
if ((options === null || options === void 0 ? void 0 : options.output) == 'tf') {
return await toTerraform(request, obj);
}
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);
}
//# sourceMappingURL=format.js.map
;