UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

149 lines 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cli_table3_1 = tslib_1.__importDefault(require("cli-table3")); const core_1 = require("@oclif/core"); const base_command_1 = tslib_1.__importDefault(require("../base-command")); const gql_1 = require("../gql"); const config_1 = require("../helpers/config"); const errors_1 = require("../helpers/errors"); const texture_1 = require("../helpers/texture/texture"); const myTokenInfoQuery = (0, gql_1.graphql)(/* GraphQL */ ` query myTokenInfo($showAll: Boolean!) { whoAmI { title resolvedPermissions(includeAll: $showAll) { level resolvedResourceIds title resolvedPermissionGroups { title permissions { isGranted permission { id title description } } } } } } `); class WhoAmI extends base_command_1.default { async run() { var _a, _b; const { flags } = await this.parse(WhoAmI); let registry, token; try { registry = this.ensure({ key: 'registry.endpoint', legacyFlagName: 'registry', args: flags, defaultValue: config_1.graphqlEndpoint, env: 'HIVE_REGISTRY', description: WhoAmI.flags['registry.endpoint'].description, }); } catch (e) { throw new errors_1.MissingEndpointError(); } try { token = this.ensure({ key: 'registry.accessToken', legacyFlagName: 'token', args: flags, env: 'HIVE_TOKEN', description: WhoAmI.flags['registry.accessToken'].description, }); } catch (e) { throw new errors_1.MissingRegistryTokenError(); } const result = await this.registryApi(registry, token).request({ operation: myTokenInfoQuery, variables: { showAll: flags.all, }, }); if (result.whoAmI == null) { throw new errors_1.InvalidRegistryTokenError(); } const data = result.whoAmI; // Print header this.log(`\n=== ${data.title} ===\n`); // Iterate and display each permission group for (const permLevel of data.resolvedPermissions) { this.log(`Level: ${permLevel.level}`); this.log(`Resources: ${(_b = (_a = permLevel.resolvedResourceIds) === null || _a === void 0 ? void 0 : _a.join(', ')) !== null && _b !== void 0 ? _b : '<none>'}`); const data = [['Group', 'Permission ID', 'Title', 'Granted', 'Description']]; for (const group of permLevel.resolvedPermissionGroups) { for (const perm of group.permissions) { data.push([ group.title, perm.permission.id, perm.permission.title, perm.isGranted ? texture_1.colors.green('✓') : texture_1.colors.red('✗'), perm.permission.description, ]); } } const colWidths = calculateColWidths(data); const table = new cli_table3_1.default({ head: data[0], wordWrap: true, style: { head: ['cyan'] }, colWidths, }); data.slice(1).forEach(row => table.push(row)); this.log(table.toString()); } } } WhoAmI.description = 'shows information about the current token'; WhoAmI.flags = { 'registry.endpoint': core_1.Flags.string({ description: 'registry endpoint', }), /** @deprecated */ registry: core_1.Flags.string({ description: 'registry address', deprecated: { message: 'use --registry.endpoint instead', version: '0.21.0', }, }), 'registry.accessToken': core_1.Flags.string({ description: 'registry access token', }), /** @deprecated */ token: core_1.Flags.string({ description: 'api token', deprecated: { message: 'use --registry.accessToken instead', version: '0.21.0', }, }), all: core_1.Flags.boolean({ description: 'Also show non-granted permissions.', default: false, }), }; exports.default = WhoAmI; function calculateColWidths(data) { const colCount = data[0].length; const maxColWidths = new Array(colCount).fill(0); data.forEach(row => { row.forEach((cell, i) => { maxColWidths[i] = Math.max(maxColWidths[i], cell.length); }); }); const terminalWidth = process.stdout.columns || 80; // Account for borders: numCols + 1 const totalBorder = colCount + 1; const totalContentWidth = maxColWidths.reduce((a, b) => a + b, 0); // Calculate scaled width for each column return maxColWidths.map(w => Math.floor((w / totalContentWidth) * (terminalWidth - totalBorder))); } //# sourceMappingURL=whoami.js.map