|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import * as os from 'os'; |
| 9 | +import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; |
| 10 | +import { Aliases, AuthInfo, Messages, sfdc } from '@salesforce/core'; |
| 11 | + |
| 12 | +import { OrgDisplayReturn, ScratchOrgFields } from '../../../shared/orgTypes'; |
| 13 | +import { getAliasByUsername, camelCaseToTitleCase } from '../../../shared/utils'; |
| 14 | +import { getStyledValue } from '../../../shared/orgHighlighter'; |
| 15 | +import { OrgListUtil } from '../../../shared/orgListUtil'; |
| 16 | + |
| 17 | +Messages.importMessagesDirectory(__dirname); |
| 18 | +const messages = Messages.loadMessages('@salesforce/plugin-org', 'display'); |
| 19 | + |
| 20 | +export class OrgDisplayCommand extends SfdxCommand { |
| 21 | + public static readonly description = messages.getMessage('description'); |
| 22 | + public static readonly examples = messages.getMessage('examples').split(os.EOL); |
| 23 | + public static readonly requiresUsername = true; |
| 24 | + public static readonly supportsDevhubUsername = true; // required to check scratch orgs for scratchiness |
| 25 | + public static readonly flagsConfig: FlagsConfig = { |
| 26 | + verbose: flags.builtin(), |
| 27 | + }; |
| 28 | + |
| 29 | + public async run(): Promise<OrgDisplayReturn> { |
| 30 | + // translate to alias if necessary |
| 31 | + const username = (await Aliases.fetch(this.flags.targetusername)) ?? this.flags.targetusername; |
| 32 | + const authInfo = await AuthInfo.create({ username }); |
| 33 | + const fields = authInfo.getFields(true); |
| 34 | + |
| 35 | + const isScratchOrg = fields.devHubUsername; |
| 36 | + const scratchOrgInfo = isScratchOrg ? await this.getScratchOrgInformation(fields.orgId) : {}; |
| 37 | + |
| 38 | + const returnValue: OrgDisplayReturn = { |
| 39 | + // renamed properties |
| 40 | + id: fields.orgId, |
| 41 | + devHubId: fields.devHubUsername, |
| 42 | + |
| 43 | + // copied properties |
| 44 | + accessToken: fields.accessToken, |
| 45 | + instanceUrl: fields.instanceUrl, |
| 46 | + username: fields.username, |
| 47 | + clientId: fields.clientId, |
| 48 | + password: fields.password, |
| 49 | + ...scratchOrgInfo, |
| 50 | + |
| 51 | + // properties with more complex logic |
| 52 | + connectedStatus: isScratchOrg |
| 53 | + ? undefined |
| 54 | + : await OrgListUtil.determineConnectedStatusForNonScratchOrg(fields.username), |
| 55 | + sfdxAuthUrl: this.flags.verbose && fields.refreshToken ? authInfo.getSfdxAuthUrl() : undefined, |
| 56 | + alias: await getAliasByUsername(fields.username), |
| 57 | + }; |
| 58 | + if (!this.flags.json) { |
| 59 | + this.print(returnValue); |
| 60 | + } |
| 61 | + return returnValue; |
| 62 | + } |
| 63 | + |
| 64 | + private print(result: OrgDisplayReturn): void { |
| 65 | + const columns = { |
| 66 | + columns: [ |
| 67 | + { key: 'key', label: 'KEY' }, |
| 68 | + { key: 'value', label: 'VALUE' }, |
| 69 | + ], |
| 70 | + }; |
| 71 | + const tableRows = Object.keys(result) |
| 72 | + .filter((key) => result[key] !== undefined && result[key] !== null) // some values won't exist |
| 73 | + .sort() // this command always alphabetizes the table rows |
| 74 | + .map((key) => ({ |
| 75 | + key: camelCaseToTitleCase(key), |
| 76 | + value: getStyledValue(key, result[key]), |
| 77 | + })); |
| 78 | + |
| 79 | + this.ux.styledHeader('Org Description'); |
| 80 | + this.ux.table(tableRows, columns); |
| 81 | + } |
| 82 | + |
| 83 | + private async getScratchOrgInformation(orgId: string): Promise<ScratchOrgFields> { |
| 84 | + const hubOrg = await this.org.getDevHubOrg(); |
| 85 | + const result = ( |
| 86 | + await OrgListUtil.retrieveScratchOrgInfoFromDevHub(hubOrg.getUsername(), [sfdc.trimTo15(orgId)]) |
| 87 | + )[0]; |
| 88 | + return { |
| 89 | + status: result.Status, |
| 90 | + expirationDate: result.ExpirationDate, |
| 91 | + createdBy: result.CreatedBy?.Username, |
| 92 | + edition: result.Edition ?? undefined, // null for snapshot orgs, possibly others. Marking it undefined keeps it out of json output |
| 93 | + namespace: result.Namespace ?? undefined, // may be null on server |
| 94 | + orgName: result.OrgName, |
| 95 | + createdDate: result.CreatedDate, |
| 96 | + }; |
| 97 | + } |
| 98 | +} |
0 commit comments