From 9a22dd7a1344e6907bb572c4199161b84397e8b0 Mon Sep 17 00:00:00 2001 From: Rodrigo Espinosa de los Monteros <1084688+RodEsp@users.noreply.github.com> Date: Tue, 11 May 2021 17:01:38 -0400 Subject: [PATCH] feat: env list skeleton --- command-snapshot.json | 5 +++ messages/list.md | 0 src/commands/env/list.ts | 80 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 messages/list.md create mode 100644 src/commands/env/list.ts diff --git a/command-snapshot.json b/command-snapshot.json index 35d96ab5..88e5c7cc 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -3,5 +3,10 @@ "command": "env:connect", "plugin": "@salesforce/plugin-env", "flags": ["client-id", "instance-url", "jwt-key-file", "username"] + }, + { + "command": "env:list", + "plugin": "@salesforce/plugin-env", + "flags": ["all"] } ] diff --git a/messages/list.md b/messages/list.md new file mode 100644 index 00000000..e69de29b diff --git a/src/commands/env/list.ts b/src/commands/env/list.ts new file mode 100644 index 00000000..72b76ec1 --- /dev/null +++ b/src/commands/env/list.ts @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2021, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +import { EOL } from 'os'; + +import { Command, flags } from '@oclif/command'; +import { cli, Table } from 'cli-ux'; +import { AuthInfo, Authorization, SfdxError } from '@salesforce/core'; + +// TODO: add back once md messages are supported +// Messages.importMessagesDirectory(__dirname); +// const messages = Messages.loadMessages('@salesforce/plugin-env', 'list'); + +export default class EnvList extends Command { + // TODO: add back once md messages are supported + // public static readonly description = messages.getMessage('description'); + // public static readonly examples = messages.getMessage('examples').split(EOL); + public static readonly description = 'list environments'; + public static readonly examples = 'sf env list\nsf env list --all'.split(EOL); + public static flags = { + all: flags.boolean({ + char: 'a', + description: "show all environments regardless of whether they're connected or not", + }), + ...cli.table.flags(), + }; + + public flags: { + all: boolean; + }; + + public async run(): Promise { + this.flags = this.parse(EnvList).flags; + + let authorizations: Authorization[]; + + try { + if (await AuthInfo.hasAuthentications()) { + authorizations = await AuthInfo.listAllAuthorizations(); + const hasErrors = authorizations.filter((auth) => !!auth.error).length > 0; + const columns = { + alias: { + get: (row) => row.alias ?? '', + }, + username: {}, + orgId: { + header: 'Org ID', + }, + instanceUrl: { + header: 'Instance URL', + }, + oauthMethod: { + header: 'OAuth Method', + }, + } as Table.table.Columns>; + if (hasErrors) { + columns.error = { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access + get: (row) => row.error ?? '', + } as Table.table.Columns>; + } + cli.styledHeader('Authenticated Envs'); + cli.table(authorizations, columns, this.flags); + } else { + throw new SfdxError('There are no authorizations available.'); + } + } catch (error) { + const err = error as SfdxError; + // TODO: add back once md messages are supported + // cli.log(messages.getMessage('noResultsFound')); + cli.error(err); + } + + return authorizations; + } +}