From 5c94144d30c505a22142977dcec7684c8d7093ae Mon Sep 17 00:00:00 2001 From: Rodrigo Espinosa de los Monteros <1084688+RodEsp@users.noreply.github.com> Date: Thu, 3 Jun 2021 14:00:36 -0400 Subject: [PATCH 1/5] feat: add env display command --- README.md | 2 +- messages/display.md | 28 ++++++++++++++ messages/list.md | 20 ++++++++++ src/commands/env/display.ts | 77 +++++++++++++++++++++++++++++++++++++ src/commands/env/list.ts | 24 +++++------- 5 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 messages/display.md create mode 100644 src/commands/env/display.ts diff --git a/README.md b/README.md index fe19ee83..5fef21cc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# plugin-env; +# plugin-env [![NPM](https://img.shields.io/npm/v/@salesforce/plugin-env.svg?label=@salesforce/plugin-env)](https://www.npmjs.com/package/@salesforce/plugin-env) [![CircleCI](https://circleci.com/gh/salesforcecli/plugin-env/tree/main.svg?style=shield)](https://circleci.com/gh/salesforcecli/plugin-env/tree/main) [![Downloads/week](https://img.shields.io/npm/dw/@salesforce/plugin-env.svg)](https://npmjs.org/package/@salesforce/plugin-env) [![License](https://img.shields.io/badge/License-BSD%203--Clause-brightgreen.svg)](https://raw.githubusercontent.com/salesforcecli/plugin-env/main/LICENSE.txt) diff --git a/messages/display.md b/messages/display.md new file mode 100644 index 00000000..a732d1c9 --- /dev/null +++ b/messages/display.md @@ -0,0 +1,28 @@ +# description + +Display details about a specific environment + +# examples + +sf env display -e my-scratch-org +sf env display -e user@name.com + +# flags.environment.summary + +Environment name or alias to display. + +# error.NoResultsFound + +No results found + +# error.NoEnvFound + +No environment found for %s. + +# error.NoDefaultEnv + +No default environment found. Use -e or --environment to specify which env to open. + +# error.NoAuthsAvailable + +There are no authentications available. diff --git a/messages/list.md b/messages/list.md index e69de29b..66bf479e 100644 --- a/messages/list.md +++ b/messages/list.md @@ -0,0 +1,20 @@ +# description + +List the environments you’ve created or logged into. + +# examples + +sf env list +sf env list --all + +# flags.all.summary + +Show all environments, including inactive orgs. + +# error.NoAuthsAvailable + +There are no authentications available. + +# error.NoResultsFound + +No results found diff --git a/src/commands/env/display.ts b/src/commands/env/display.ts new file mode 100644 index 00000000..eb9aa6f3 --- /dev/null +++ b/src/commands/env/display.ts @@ -0,0 +1,77 @@ +/* + * 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 } from 'cli-ux'; +import { AuthInfo, SfOrg, Messages, SfdxError } from '@salesforce/core'; + +Messages.importMessagesDirectory(__dirname); +const messages = Messages.loadMessages('@salesforce/plugin-env', 'display'); + +export default class EnvDisplay extends Command { + public static readonly description = messages.getMessage('description'); + public static readonly examples = messages.getMessage('examples').split(EOL); + public static flags = { + environment: flags.string({ + char: 'e', + description: messages.getMessage('flags.environment.summary'), + }), + }; + + public flags: { + environment: string; + }; + + // TODO: Change SfOrg type to a more generalized auth type once we have Functions envs integrated. + + public async run(): Promise { + this.flags = this.parse(EnvDisplay).flags; + + let authorizations: SfOrg[]; + let foundAuthorization: SfOrg; + + try { + if (await AuthInfo.hasAuthentications()) { + authorizations = await AuthInfo.listAllAuthorizations(); + + if (!this.flags.environment) { + // TODO this should be retrieved from sf config once we have those commands. If not found, still throw. + throw messages.createError('error.NoDefaultEnv'); + } + + foundAuthorization = authorizations.filter((auth) => auth.username === this.flags.environment)[0]; + if (!foundAuthorization) { + foundAuthorization = authorizations.filter((auth) => auth.alias === this.flags.environment)[0]; + } + + if (foundAuthorization) { + const columns = { + key: {}, + value: {}, + }; + + cli.table( + Object.keys(foundAuthorization).map((key, i) => ({ key, value: Object.values(foundAuthorization)[i] })), + columns + ); + } else { + throw new SfdxError(messages.getMessage('error.NoEnvFound')); + } + } else { + throw messages.createError('error.NoAuthsAvailable'); + } + } catch (error) { + const err = error as SfdxError; + cli.log(messages.getMessage('error.NoResultsFound')); + cli.error(err); + } + + return authorizations; + } +} diff --git a/src/commands/env/list.ts b/src/commands/env/list.ts index 69eb5898..f147a352 100644 --- a/src/commands/env/list.ts +++ b/src/commands/env/list.ts @@ -9,24 +9,20 @@ import { EOL } from 'os'; import { Command, flags } from '@oclif/command'; import { cli, Table } from 'cli-ux'; -import { AuthInfo, SfOrg, SfdxError } from '@salesforce/core'; +import { AuthInfo, SfOrg, Messages, SfdxError } from '@salesforce/core'; -// TODO: add back once md messages are supported -// Messages.importMessagesDirectory(__dirname); -// const messages = Messages.loadMessages('@salesforce/plugin-env', 'list'); +Messages.importMessagesDirectory(__dirname); +const messages = Messages.loadMessages('@salesforce/plugin-env', 'list'); export type SfOrgs = SfOrg[]; 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 readonly description = messages.getMessage('description'); + public static readonly examples = messages.getMessage('examples').split(EOL); public static flags = { all: flags.boolean({ char: 'a', - description: "show all environments regardless of whether they're connected or not", + description: messages.getMessage('flags.all.summary'), }), ...cli.table.flags(), }; @@ -65,15 +61,13 @@ export default class EnvList extends Command { get: (row) => row.error ?? '', } as Table.table.Columns>; } - cli.styledHeader('Authenticated Envs'); - cli.table(authorizations, columns, this.flags); + cli.table(authorizations, columns, { title: 'Authenticated Envs', ...this.flags }); } else { - throw new SfdxError('There are no authorizations available.'); + throw messages.createError('error.NoAuthsAvailable'); } } catch (error) { const err = error as SfdxError; - // TODO: add back once md messages are supported - // cli.log(messages.getMessage('noResultsFound')); + cli.log(messages.getMessage('error.NoResultsFound')); cli.error(err); } From 0e9a9a1c9b0009fc6a1a226c56ce43a1761d9390 Mon Sep 17 00:00:00 2001 From: Rodrigo Espinosa de los Monteros <1084688+RodEsp@users.noreply.github.com> Date: Thu, 3 Jun 2021 16:34:04 -0400 Subject: [PATCH 2/5] chore: command-snapshot --- command-snapshot.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/command-snapshot.json b/command-snapshot.json index 1f045d34..eaad2550 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -4,6 +4,11 @@ "plugin": "@salesforce/plugin-env", "flags": ["all", "columns", "csv", "extended", "filter", "no-header", "no-truncate", "output", "sort"] }, + { + "command": "env:display", + "plugin": "@salesforce/plugin-env", + "flags": ["environment"] + }, { "command": "env:open", "plugin": "@salesforce/plugin-env", From c9a5c9e0a9ecdad43e4284eb2fcd32b842914576 Mon Sep 17 00:00:00 2001 From: Rodrigo Espinosa de los Monteros <1084688+RodEsp@users.noreply.github.com> Date: Thu, 3 Jun 2021 16:53:38 -0400 Subject: [PATCH 3/5] fix: no env found message --- src/commands/env/display.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/env/display.ts b/src/commands/env/display.ts index eb9aa6f3..3d582a9a 100644 --- a/src/commands/env/display.ts +++ b/src/commands/env/display.ts @@ -61,7 +61,7 @@ export default class EnvDisplay extends Command { columns ); } else { - throw new SfdxError(messages.getMessage('error.NoEnvFound')); + throw new SfdxError(messages.getMessage('error.NoEnvFound', [this.flags.environment])); } } else { throw messages.createError('error.NoAuthsAvailable'); From 5d16fcfe050d66635f02d463f117dd4706428e5d Mon Sep 17 00:00:00 2001 From: Rodrigo Espinosa de los Monteros <1084688+RodEsp@users.noreply.github.com> Date: Mon, 7 Jun 2021 12:17:13 -0400 Subject: [PATCH 4/5] chore: rebase on main & command-snapshot --- command-snapshot.json | 8 ++-- schemas/env-display.json | 94 +++++++++++++++++++++++++++++++++++++ src/commands/env/display.ts | 4 +- 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 schemas/env-display.json diff --git a/command-snapshot.json b/command-snapshot.json index eaad2550..9bd12145 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -1,13 +1,13 @@ [ { - "command": "env:list", + "command": "env:display", "plugin": "@salesforce/plugin-env", - "flags": ["all", "columns", "csv", "extended", "filter", "no-header", "no-truncate", "output", "sort"] + "flags": ["environment"] }, { - "command": "env:display", + "command": "env:list", "plugin": "@salesforce/plugin-env", - "flags": ["environment"] + "flags": ["all", "columns", "csv", "extended", "filter", "no-header", "no-truncate", "output", "sort"] }, { "command": "env:open", diff --git a/schemas/env-display.json b/schemas/env-display.json new file mode 100644 index 00000000..11ac4ad5 --- /dev/null +++ b/schemas/env-display.json @@ -0,0 +1,94 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/SfOrgs", + "definitions": { + "SfOrgs": { + "type": "array", + "items": { + "$ref": "#/definitions/SfOrg" + } + }, + "SfOrg": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Optional%3CAnyJson%3E" + }, + "properties": { + "alias": { + "type": "string" + }, + "username": { + "type": "string" + }, + "orgId": { + "type": "string" + }, + "instanceUrl": { + "type": "string" + }, + "accessToken": { + "type": "string" + }, + "oauthMethod": { + "type": "string", + "enum": ["jwt", "web", "token", "unknown"] + }, + "error": { + "type": "string" + } + } + }, + "Optional": { + "anyOf": [ + { + "$ref": "#/definitions/AnyJson" + }, + { + "not": {} + } + ], + "description": "A union type for either the parameterized type `T` or `undefined` -- the opposite of {@link NonOptional } ." + }, + "AnyJson": { + "anyOf": [ + { + "$ref": "#/definitions/JsonPrimitive" + }, + { + "$ref": "#/definitions/JsonCollection" + } + ], + "description": "Any valid JSON value." + }, + "JsonPrimitive": { + "type": ["null", "boolean", "number", "string"], + "description": "Any valid JSON primitive value." + }, + "JsonCollection": { + "anyOf": [ + { + "$ref": "#/definitions/JsonMap" + }, + { + "$ref": "#/definitions/JsonArray" + } + ], + "description": "Any valid JSON collection value." + }, + "JsonMap": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Optional%3CAnyJson%3E" + }, + "properties": {}, + "description": "Any JSON-compatible object." + }, + "JsonArray": { + "type": "array", + "items": { + "$ref": "#/definitions/AnyJson" + }, + "description": "Any JSON-compatible array." + } + } +} diff --git a/src/commands/env/display.ts b/src/commands/env/display.ts index 3d582a9a..24f16287 100644 --- a/src/commands/env/display.ts +++ b/src/commands/env/display.ts @@ -14,6 +14,8 @@ import { AuthInfo, SfOrg, Messages, SfdxError } from '@salesforce/core'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-env', 'display'); +export type SfOrgs = SfOrg[]; + export default class EnvDisplay extends Command { public static readonly description = messages.getMessage('description'); public static readonly examples = messages.getMessage('examples').split(EOL); @@ -30,7 +32,7 @@ export default class EnvDisplay extends Command { // TODO: Change SfOrg type to a more generalized auth type once we have Functions envs integrated. - public async run(): Promise { + public async run(): Promise { this.flags = this.parse(EnvDisplay).flags; let authorizations: SfOrg[]; From 0e6dcf124871ff6c352470be8622fabaec119335 Mon Sep 17 00:00:00 2001 From: Rodrigo Espinosa de los Monteros <1084688+RodEsp@users.noreply.github.com> Date: Tue, 8 Jun 2021 17:49:15 -0400 Subject: [PATCH 5/5] fix: switch to getMessages() for examples --- messages/display.md | 4 ++-- messages/list.md | 4 ++-- src/commands/env/display.ts | 4 +--- src/commands/env/list.ts | 4 +--- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/messages/display.md b/messages/display.md index a732d1c9..aed227b5 100644 --- a/messages/display.md +++ b/messages/display.md @@ -4,8 +4,8 @@ Display details about a specific environment # examples -sf env display -e my-scratch-org -sf env display -e user@name.com +- sf env display -e my-scratch-org +- sf env display -e user@name.com # flags.environment.summary diff --git a/messages/list.md b/messages/list.md index 66bf479e..270dd5be 100644 --- a/messages/list.md +++ b/messages/list.md @@ -4,8 +4,8 @@ List the environments you’ve created or logged into. # examples -sf env list -sf env list --all +- sf env list +- sf env list --all # flags.all.summary diff --git a/src/commands/env/display.ts b/src/commands/env/display.ts index 24f16287..568e78ad 100644 --- a/src/commands/env/display.ts +++ b/src/commands/env/display.ts @@ -5,8 +5,6 @@ * 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 } from 'cli-ux'; import { AuthInfo, SfOrg, Messages, SfdxError } from '@salesforce/core'; @@ -18,7 +16,7 @@ export type SfOrgs = SfOrg[]; export default class EnvDisplay extends Command { public static readonly description = messages.getMessage('description'); - public static readonly examples = messages.getMessage('examples').split(EOL); + public static readonly examples = messages.getMessages('examples'); public static flags = { environment: flags.string({ char: 'e', diff --git a/src/commands/env/list.ts b/src/commands/env/list.ts index f147a352..8feb3092 100644 --- a/src/commands/env/list.ts +++ b/src/commands/env/list.ts @@ -5,8 +5,6 @@ * 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, SfOrg, Messages, SfdxError } from '@salesforce/core'; @@ -18,7 +16,7 @@ export type SfOrgs = SfOrg[]; export default class EnvList extends Command { public static readonly description = messages.getMessage('description'); - public static readonly examples = messages.getMessage('examples').split(EOL); + public static readonly examples = messages.getMessages('examples'); public static flags = { all: flags.boolean({ char: 'a',