UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

129 lines 4.32 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 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 table = new cli_table3_1.default({ head: ['Group', 'Permission ID', 'Title', 'Granted', 'Description'], wordWrap: true, style: { head: ['cyan'] }, }); for (const group of permLevel.resolvedPermissionGroups) { for (const perm of group.permissions) { table.push([ group.title, perm.permission.id, perm.permission.title, perm.isGranted ? '✓' : '✗', perm.permission.description, ]); } } 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; //# sourceMappingURL=whoami.js.map