UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

202 lines 7.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupCmd = void 0; const generic_1 = require("./generic"); const command_1 = require("../cli/command"); const query_1 = require("./query"); const resolver_1 = require("./resolver"); const options_1 = require("./options"); const functions_1 = require("../util/functions"); const objects_1 = require("../util/objects"); class GroupCmd extends command_1.Command { constructor() { super(...arguments); this.command = 'group'; this.describe = 'Manage groups'; } builder(yargs) { const resolver = (0, resolver_1.kindResolver)('group'); const groupSchema = { props: [...query_1.defaultProps, 'origin'], }; const userSchema = { props: [...query_1.defaultProps, 'origin', 'email', 'idp'], kinds: ['user'], }; const opts = [options_1.withOrgOptions, options_1.withStandardOptions]; const commandName = 'group'; const commandNamePlural = 'groups'; const commandNameA = 'a group'; return (yargs .demandCommand() .version(false) .help() // generic .command(new generic_1.Get(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.Edit(commandName, resolver, ...opts).toYargs()) .command(new generic_1.Query(commandNamePlural, resolver, groupSchema, ...opts).toYargs()) .command(new generic_1.Delete(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.Eventlog(commandName, resolver, ...opts).toYargs()) .command(new generic_1.Tag(commandNamePlural, resolver, ...opts).toYargs()) .command(new generic_1.Patch(commandName, resolver, ...opts).toYargs()) .command(new generic_1.ListPermissions(commandNameA, resolver, ...opts).toYargs()) .command(new generic_1.ViewAccessReport(commandName, resolver, ...opts).toYargs()) .command(new generic_1.Clone(commandName, resolver, ...opts).toYargs()) .command(new generic_1.Update(commandName, resolver, [ { path: 'description', }, { path: 'tags.<key>', }, { path: 'memberLinks', array: true, }, ], ...opts).toYargs()) // specific .command(new Create(resolver, userSchema).toYargs()) .command(new AddMembers(resolver).toYargs()) .command(new RemoveMembers(resolver).toYargs())); } handle() { } } exports.GroupCmd = GroupCmd; class Create extends command_1.Command { constructor(resolve, schema) { super(); this.resolve = resolve; this.schema = schema; this.command = 'create'; this.describe = 'Create a new group'; } withCreateOptions(yargs) { return yargs.options({ name: { describe: 'Name of the new group', demandOption: true, requiresArg: true, }, description: { alias: 'desc', requiresArg: true, describe: 'Optional description, defaults to the name if not set', }, }); } builder(yargs) { return (0, functions_1.pipe)( // this.withCreateOptions, (0, query_1.withQuerySpecOptions)(this.schema), generic_1.withTagOptions, options_1.withOrgOptions, options_1.withStandardOptions)(yargs); } async handle(args) { const body = { name: args.name, description: args.description, tags: (0, generic_1.fromTagOptions)(args), memberLinks: [], }; const query = (0, query_1.fromQuerySpecOptions)(args); if (query) { // @ts-ignore body.memberQuery = query; } const path = this.resolve.homeLink(this.session.context); const data = await this.client.create(path, body); this.session.outFormat(data); } } function withMemberOptionsAdd(yargs) { return withMemberOptions(yargs, 'add'); } function withMemberOptionsRemove(yargs) { return withMemberOptions(yargs, 'remove'); } function withMemberOptions(yargs, action) { return yargs.options({ email: { describe: `Email of user to ${action}`, requiresArg: true, multiple: true, }, serviceaccount: { describe: `Name of the service account to ${action}`, multiple: true, }, }); } class AddMembers extends command_1.Command { constructor(resolve) { super(); this.resolve = resolve; this.command = 'add-member <ref>'; this.describe = 'Add members to the referenced group'; this.excludeInvalidEmails = excludeInvalidEmails; } builder(yargs) { return (0, functions_1.pipe)( // generic_1.withSingleRef, withMemberOptionsAdd, options_1.withOrgOptions, options_1.withStandardOptions)(yargs); } async handle(args) { // validate emails and move them to ids await this.excludeInvalidEmails(args); const body = { '$append/memberLinks': [ // ...(0, objects_1.toArray)(args.email).map((e) => (0, resolver_1.resolveToLink)('user', e, this.session.context)), ...(0, objects_1.toArray)(args.serviceaccount).map((s) => (0, resolver_1.resolveToLink)('serviceaccount', s, this.session.context)), ], }; const path = this.resolve.resourceLink(args.ref, this.session.context); const data = await this.client.patch(path, body); this.session.outFormat(data); } } async function excludeInvalidEmails(opts) { const resolve = (0, resolver_1.kindResolver)('user'); const promises = []; const emails = (0, objects_1.toArray)(opts.email); for (let email of emails) { promises.push(this.client.get(`${resolve.homeLink(this.session.context)}/-for/email/${email}`)); } const responses = await Promise.allSettled(promises); const validEmails = []; responses.forEach((response, index) => { if (response.status === 'rejected') { this.session.err(`User with email <${emails[index]}> not found`); } else { validEmails.push(response.value.email); } }); opts.email = validEmails; } class RemoveMembers extends command_1.Command { constructor(resolve) { super(); this.resolve = resolve; this.command = 'remove-member <ref>'; this.describe = 'Remove members from the referenced group'; this.excludeInvalidEmails = excludeInvalidEmails; } builder(yargs) { return (0, functions_1.pipe)( // generic_1.withSingleRef, withMemberOptionsRemove, options_1.withOrgOptions, options_1.withStandardOptions)(yargs); } async handle(args) { await this.excludeInvalidEmails(args); const body = { '$drop/memberLinks': [ // ...(0, objects_1.toArray)(args.email).map((e) => (0, resolver_1.resolveToLink)('user', e, this.session.context)), ...(0, objects_1.toArray)(args.serviceaccount).map((s) => (0, resolver_1.resolveToLink)('serviceaccount', s, this.session.context)), ], }; const path = this.resolve.resourceLink(args.ref, this.session.context); const data = await this.client.patch(path, body); this.session.outFormat(data); } } //# sourceMappingURL=group.js.map