UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

235 lines (234 loc) 7.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestCmd = void 0; const _ = require("lodash"); const options_1 = require("./options"); const command_1 = require("../cli/command"); const functions_1 = require("../util/functions"); const io_1 = require("../util/io"); const format_1 = require("../util/format"); function toAxiosConfig(opts) { var _a; if (!opts.header) { return {}; } const h = Object.fromEntries((_a = opts.header) === null || _a === void 0 ? void 0 : _a.map((s) => { const parts = s.split(':'); return [parts[0].trim(), parts.slice(1).join(':').trim()]; })); return { headers: h, }; } function withBodyOptions(yargs) { return yargs.options({ file: { alias: 'f', description: 'File to load the request from. Use `--file -` to enable input from stdin.', nargs: 1, requiresArg: true, }, }); } function withPositional(yargs) { return yargs.positional('path', { describe: 'Resource path (e.g., /org/test/user/adam)', demandOption: true, }); } function withHeaders(yargs) { return yargs.options({ header: { alias: 'H', description: 'HTTP headers in curl format: "x-header: value"', multiple: true, array: true, requiresArg: true, }, }); } class RestCmd extends command_1.Command { constructor() { super(...arguments); this.command = 'rest'; this.describe = 'Submit REST requests against the API'; } builder(yargs) { return ((0, functions_1.pipe)( // options_1.withProfileOptions, options_1.withStandardOptions)(yargs) .demandCommand() .version(false) .help() // extend .command(new Get().toYargs()) .command(new Patch().toYargs()) .command(new Delete().toYargs()) .command(new Edit().toYargs()) .command(new Create().toYargs()) .command(new Post().toYargs()) .command(new Put().toYargs())); } handle() { } } exports.RestCmd = RestCmd; class Get extends command_1.Command { constructor() { super(...arguments); this.command = 'get <path>'; this.describe = 'Submit a GET request against the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withHeaders)(yargs); } async handle(args) { let body = await this.client.get(args.path, toAxiosConfig(args)); this.session.outFormat(body); } } class Post extends command_1.Command { constructor() { super(...arguments); this.command = 'post <path>'; this.describe = 'Submit a POST request against the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withBodyOptions, withHeaders)(yargs); } async handle(args) { let body; if (args.file) { let bodyAsString = await (0, io_1.readTextFile)(args.file); body = (0, io_1.loadObject)(bodyAsString, args.file); } else { body = null; } let data = await this.client.post(args.path, body, toAxiosConfig(args)); this.session.outFormat(data); } } class Put extends command_1.Command { constructor() { super(...arguments); this.command = 'put <path>'; this.describe = 'Submit a PUT request against the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withBodyOptions, withHeaders)(yargs); } async handle(args) { let body; if (args.file) { let bodyAsString = await (0, io_1.readTextFile)(args.file); body = (0, io_1.loadObject)(bodyAsString, args.file); } else { body = null; } let data = await this.client.put(args.path, body, toAxiosConfig(args)); this.session.outFormat(data); } } class Delete extends command_1.Command { constructor() { super(...arguments); this.command = 'delete <path>'; this.describe = 'Submit a DELETE request against the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withHeaders)(yargs); } async handle(args) { let body = await this.client.delete(args.path, toAxiosConfig(args)); this.session.outFormat(body); } } class Patch extends command_1.Command { constructor() { super(...arguments); this.command = 'patch <path>'; this.describe = 'Submit a PATCH request against the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withBodyOptions, withHeaders)(yargs); } async handle(args) { let bodyAsString; if (args.file) { bodyAsString = await (0, io_1.readTextFile)(args.file); } else { this.session.abort({ message: 'No input file provided' }); } const reqBody = (0, io_1.loadObject)(bodyAsString, args.file); let respBody = await this.client.patch(args.path, reqBody, toAxiosConfig(args)); this.session.outFormat(respBody); } } class Edit extends command_1.Command { constructor() { super(...arguments); this.command = 'edit <path>'; this.describe = 'Launches the default editor with the contents of the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withHeaders)(yargs); } async handle(args) { var _a; let body = await this.client.get(args.path); const header = `# You are about to PATCH ${args.path} # You are performing this operation as ${((_a = this.session.profile.authInfo) === null || _a === void 0 ? void 0 : _a.email) || '<system>'} `; const editableYaml = header + (0, format_1.toSortedYamlString)(body); const editMod = require('../editor/editor'); const newContent = await editMod.edit(editableYaml, 'yaml'); if (newContent === undefined) { this.session.end('Resource was not modified. No changes have been submitted'); } const newPayload = (0, io_1.loadObject)(newContent, '<editor>'); if (_.isEqual(body, newPayload)) { this.session.end('Resource was not modified. No changes have been submitted'); } body = await this.client.patch(args.path, newPayload, toAxiosConfig(args)); this.session.outFormat(body); } } class Create extends command_1.Command { constructor() { super(...arguments); this.command = 'create <path>'; this.describe = 'Submit a POST request followed by a GET request against the referenced path'; } builder(yargs) { return (0, functions_1.pipe)( // withPositional, withBodyOptions, withHeaders)(yargs); } async handle(args) { let bodyAsString; if (args.file) { bodyAsString = await (0, io_1.readTextFile)(args.file); } else { this.session.abort({ message: 'No input file provided' }); } const reqBody = (0, io_1.loadObject)(bodyAsString, args.file); let respBody = await this.client.create(args.path, reqBody, toAxiosConfig(args)); this.session.outFormat(respBody); } } //# sourceMappingURL=rest.js.map