Skip to content

Commit

Permalink
feat: add support for adding alias to existing users and delete org c…
Browse files Browse the repository at this point in the history
…onfiguration
  • Loading branch information
Codeneos committed Aug 6, 2024
1 parent 9ea7261 commit 29c135e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
11 changes: 10 additions & 1 deletion packages/util/src/sfdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ export namespace sfdx {
}

if (options?.alias) {
await setAlias(details.username, options.alias);
const aliases = Array.isArray(options?.alias) ? options?.alias : [ options?.alias ];
aliases.forEach(alias => stateAggregator.aliases.set(alias, details.username));
}

await stateAggregator.orgs.write(details.username);
}

Expand Down Expand Up @@ -445,6 +447,13 @@ export namespace sfdx {
await options.fs.writeFile(configPath, Buffer.from(JSON.stringify(newConfig, undefined, 2)));
}

export async function removeOrg(username: string) {
const stateAggregator = await salesforce.StateAggregator.getInstance();
stateAggregator.aliases.unsetAll(username);
await stateAggregator.orgs.remove(username);
await stateAggregator.aliases.write();
}

/**
* Default implementation of the {@link DeviceLoginFlow} that wraps
* the {@link salesforce.DeviceOauthService} and {@link salesforce.DeviceCodeResponse}.
Expand Down
42 changes: 39 additions & 3 deletions packages/vscode-extension/src/commands/selectOrgCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { SalesforceAuthResult, SalesforceOrgInfo, sfdx } from '@vlocode/util';
import { CommandBase } from '../lib/commandBase';
import { vscodeCommand } from '../lib/commandRouter';
import { VlocodeCommand } from '../constants';
import { QuickPick } from '../lib/ui/quickPick';

type SelectOrgQuickPickItem = vscode.QuickPickItem & { orgInfo?: SalesforceOrgInfo; instanceUrl?: string };

@vscodeCommand(VlocodeCommand.selectOrg)
export default class SelectOrgCommand extends CommandBase {

private readonly setAliasIcon = new vscode.ThemeIcon('account');
private readonly deleteOrg = new vscode.ThemeIcon('trash');

private readonly newOrgOption : SelectOrgQuickPickItem = {
label: '$(key) Authorize new org',
description: 'You will be prompted for the login url'
Expand Down Expand Up @@ -62,7 +66,11 @@ export default class SelectOrgCommand extends CommandBase {
return orgList.map(orgInfo => ({
label: this.getOrgLabel(orgInfo),
description: this.getOrgDescription(orgInfo),
orgInfo
orgInfo,
buttons: [
{ iconPath: this.deleteOrg, tooltip: 'Remove Org' },
{ iconPath: this.setAliasIcon, tooltip: 'Add Alias' }
]
}));
}

Expand Down Expand Up @@ -90,8 +98,7 @@ export default class SelectOrgCommand extends CommandBase {
selectionOptions.push({ label: '', kind: -1 }, ...knownOrgs)
}

const selectedOrg = await vscode.window.showQuickPick(selectionOptions,
{ placeHolder: 'Select an existing Salesforce org -or- authorize a new one' });
const selectedOrg = await this.showOrgSelection(selectionOptions);

if (!selectedOrg) {
return;
Expand All @@ -112,6 +119,35 @@ export default class SelectOrgCommand extends CommandBase {
}
}

protected showOrgSelection(orgs: SelectOrgQuickPickItem[]) {
const quickPickMenu = QuickPick.create(orgs, {
placeHolder: 'Select an existing Salesforce org -or- authorize a new one'
});

quickPickMenu.onTriggerItemButtom(async ({ item, button }) => {
if (item.orgInfo && button?.iconPath == this.setAliasIcon) {
const alias = await vscode.window.showInputBox({
placeHolder: `Enter an alias for ${item.orgInfo.username}`
});
if (alias) {
await sfdx.setAlias(item.orgInfo.username, alias);
}
} else if (item.orgInfo && button?.iconPath == this.deleteOrg) {
const confirmation = await vscode.window.showWarningMessage(
`Are you sure you want to permanently delete the org configuration for ${item.orgInfo.username}?\n\n` +
'You will lose the ability to connect to this org as well as any stored Access and Refresh tokens.',
{ modal: true }, 'Yes'
);
if (confirmation === 'Yes') {
await sfdx.removeOrg(item.orgInfo.username);
vscode.window.showInformationMessage(`Org configuration for '${item.orgInfo.username}' removed.`);
}
}
});

return quickPickMenu.onAccept();
}

protected async authorizeNewOrg() : Promise<SalesforceAuthResult | undefined> {
const flowType = await vscode.window.showQuickPick(this.authFlows,
{ placeHolder: 'Select the authorization flows you want use' });
Expand Down

0 comments on commit 29c135e

Please sign in to comment.