Skip to content

Commit 91bb920

Browse files
authored
Merge pull request #2 from salesforcecli/sm/org-display
add org:display and org:list
2 parents c0f353e + 56d7907 commit 91bb920

File tree

23 files changed

+2982
-1553
lines changed

23 files changed

+2982
-1553
lines changed

.codecov.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ coverage:
88
# IDEs and editors
99
- '.vscode'
1010
# other (more specific)
11-
- 'bin'
12-
- 'messages'
11+
- 'bin'
12+
- 'messages'

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ coverage
2323

2424
# generated docs
2525
docs
26+
tmp
2627

2728
# -- CLEAN ALL
2829
node_modules
@@ -32,4 +33,4 @@ node_modules
3233

3334
# os specific files
3435
.DS_Store
35-
.idea
36+
.idea

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
"type": "node",
3232
"request": "launch",
3333
"name": "Run Current Test",
34+
"sourceMaps": true,
3435
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
3536
"args": ["--inspect", "--no-timeouts", "--colors", "${file}"],
3637
"env": {
3738
"NODE_ENV": "development",
3839
"SFDX_ENV": "development"
3940
},
40-
"sourceMaps": true,
4141
"smartStep": true,
4242
"internalConsoleOptions": "openOnSessionStart",
4343
"preLaunchTask": "Compile"

command-snapshot.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
[
22
{
3-
"command": "hello:org",
4-
"plugin": "@salesforce/plugin-template",
5-
"flags": ["apiversion", "force", "json", "loglevel", "name", "targetdevhubusername", "targetusername"]
3+
"command": "force:org:display",
4+
"plugin": "@salesforce/plugin-org",
5+
"flags": ["apiversion", "json", "loglevel", "targetdevhubusername", "targetusername", "verbose"]
6+
},
7+
{
8+
"command": "force:org:list",
9+
"plugin": "@salesforce/plugin-org",
10+
"flags": ["all", "clean", "json", "loglevel", "noprompt", "skipconnectionstatus", "verbose"]
611
}
712
]

messages/display.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"description": "get the description for the current or target org.\nOutput includes your access token, client Id, connected status, org ID, instance URL, username, and alias, if applicable.\nUse --verbose to include the SFDX auth URL.\nIncluding --verbose displays the sfdxAuthUrl property only if you authenticated to the org using auth:web:login (not auth:jwt:grant)",
3+
"examples": [
4+
"sfdx force:org:display",
5+
"sfdx force:org:display -u [email protected]",
6+
"sfdx force:org:display -u TestOrg1 --json",
7+
"sfdx force:org:display -u TestOrg1 --json > tmp/MyOrgDesc.json"
8+
]
9+
}

messages/list.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"description": "list all orgs that the Salesforce CLI has created or authenticated to",
3+
"examples": [
4+
"sfdx force:org:list",
5+
"sfdx force:org:list --verbose --json",
6+
"sfdx force:org:list --verbose --json > tmp/MyOrgList.json"
7+
],
8+
9+
"verbose": "Lists more information about each org.",
10+
"all": "Lists all authenticated orgs, including expired, deleted, and unknown-status scratch orgs.",
11+
"clean": "Remove all local org authorizations for deleted or expired orgs.",
12+
"noPrompt": "Do not prompt for confirmation.",
13+
"skipConnectionStatus": "Skips retrieving the connection status of non-scratch orgs",
14+
15+
"prompt": "Found (%s) org configurations to delete. Are you sure (yes/no)?",
16+
"noActiveScratchOrgs": "No active scratch orgs found. Specify --all to see all scratch orgs",
17+
"deleteOrgs": "You have %s expired or deleted local scratch org authorizations. To remove authorizations for inactive orgs, run force:org:list --clean.",
18+
"noOrgsFound": "No orgs can be found.",
19+
"noOrgsFoundAction": "Use one of the commands in force:auth or force:org:create to add or create new scratch orgs."
20+
}

messages/org.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"name": "@salesforce/plugin-template",
2+
"name": "@salesforce/plugin-org",
33
"description": "A template repository for sfdx plugins",
44
"version": "1.0.0",
55
"author": "Salesforce",
66
"bugs": "https://github.com/forcedotcom/cli/issues",
77
"dependencies": {
88
"@oclif/config": "^1",
99
"@salesforce/command": "^3.0.3",
10-
"@salesforce/core": "^2.15.4",
10+
"@salesforce/core": "^2.16.3",
1111
"tslib": "^1"
1212
},
1313
"devDependencies": {
@@ -21,6 +21,7 @@
2121
"@typescript-eslint/eslint-plugin": "^2.30.0",
2222
"@typescript-eslint/parser": "^2.30.0",
2323
"chai": "^4.2.0",
24+
"chai-as-promised": "^7.1.1",
2425
"cz-conventional-changelog": "^3.2.0",
2526
"eslint": "^6.8.0",
2627
"eslint-config-prettier": "^6.11.0",
@@ -34,6 +35,7 @@
3435
"husky": "^4.2.5",
3536
"lint-staged": "^10.2.2",
3637
"mocha": "^7.2.0",
38+
"moment": "^2.29.1",
3739
"nyc": "^15.1.0",
3840
"prettier": "^2.0.5",
3941
"pretty-quick": "^2.0.1",
@@ -55,7 +57,7 @@
5557
"/messages",
5658
"/oclif.manifest.json"
5759
],
58-
"homepage": "https://github.com/salesforcecli/plugin-template",
60+
"homepage": "https://github.com/salesforcecli/plugin-org",
5961
"keywords": [
6062
"force",
6163
"salesforce",
@@ -73,12 +75,18 @@
7375
"@salesforce/plugin-command-reference"
7476
],
7577
"topics": {
76-
"hello": {
77-
"description": "Commands to say hello."
78+
"force": {
79+
"external": true,
80+
"subtopics": {
81+
"org": {
82+
"description": "manage your orgs",
83+
"longDescription": "Use the org commands to manage the orgs you use with Salesforce CLI. Create and delete scratch orgs, list your created and authorized orgs, and open orgs in your browser."
84+
}
85+
}
7886
}
7987
}
8088
},
81-
"repository": "salesforcecli/plugin-template",
89+
"repository": "salesforcecli/plugin-org",
8290
"scripts": {
8391
"build": "sf-build",
8492
"clean": "sf-clean",
@@ -91,7 +99,7 @@
9199
"postpack": "shx rm -f oclif.manifest.json",
92100
"posttest": "yarn lint && yarn test:deprecation-policy && yarn test:command-reference",
93101
"test:deprecation-policy": "./bin/run snapshot:compare",
94-
"test:command-reference": "./bin/run commandreference:generate -p @salesforce/plugin-template --erroronwarnings",
102+
"test:command-reference": "./bin/run commandreference:generate -p @salesforce/plugin-org --erroronwarnings",
95103
"prepack": "sf-build",
96104
"prepare": "sf-install",
97105
"pretest": "sf-compile-test",

src/commands/force/org/display.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)