From 57de6923685ade5d3a20285528e02bc0c981a78f Mon Sep 17 00:00:00 2001 From: Yassine Date: Tue, 3 Aug 2021 13:17:42 -0400 Subject: [PATCH] feat(snapshot): add `--snapshotId` flag to `push` and `preview` commands (#387) https://coveord.atlassian.net/browse/CDX-514 --- .../cli/src/commands/org/config/preview.ts | 31 ++++++++++++++++++- packages/cli/src/commands/org/config/pull.ts | 4 +-- packages/cli/src/commands/org/config/push.ts | 2 +- .../cli/src/lib/snapshot/snapshotCommon.ts | 27 +++++++++++++--- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands/org/config/preview.ts b/packages/cli/src/commands/org/config/preview.ts index 86f74194ab..67c96b1f22 100644 --- a/packages/cli/src/commands/org/config/preview.ts +++ b/packages/cli/src/commands/org/config/preview.ts @@ -1,6 +1,8 @@ -import {DryRunOptions} from '@coveord/platform-client'; import {Command, flags} from '@oclif/command'; +import {blueBright} from 'chalk'; +import {cli} from 'cli-ux'; import {cwd} from 'process'; +import dedent from 'ts-dedent'; import { buildAnalyticsFailureHook, buildAnalyticsSuccessHook, @@ -10,11 +12,13 @@ import { IsAuthenticated, Preconditions, } from '../../../lib/decorators/preconditions'; +import {SnapshotOperationTimeoutError} from '../../../lib/errors'; import {Snapshot} from '../../../lib/snapshot/snapshot'; import { displayInvalidSnapshotError, displaySnapshotSynchronizationWarning, dryRun, + DryRunOptions, getTargetOrg, handleSnapshotError, } from '../../../lib/snapshot/snapshotCommon'; @@ -36,6 +40,12 @@ export default class Preview extends Command { default: false, required: false, }), + snapshotId: flags.string({ + char: 's', + description: + 'The unique identifier of the snapshot to preview. If not specified, a new snapshot will be created from your local project. You can list available snapshots in your organization with org:config:list', + required: false, + }), }; public static hidden = true; @@ -46,6 +56,7 @@ export default class Preview extends Command { const target = await getTargetOrg(this.configuration, flags.target); const options: DryRunOptions = { deleteMissingResources: flags.showMissingResources, + snapshotId: flags.snapshotId, }; const {reporter, snapshot, project} = await dryRun( target, @@ -69,12 +80,30 @@ export default class Preview extends Command { public async catch(err?: Error) { const {flags} = this.parse(Preview); handleSnapshotError(err); + await this.displayAdditionalErrorMessage(err); await this.config.runHook( 'analytics', buildAnalyticsFailureHook(this, flags, err) ); } + private async displayAdditionalErrorMessage(err?: Error) { + if (err instanceof SnapshotOperationTimeoutError) { + const {flags} = this.parse(Preview); + const snapshot = err.snapshot; + const target = await getTargetOrg(this.configuration, flags.target); + cli.log( + dedent` + + Once the snapshot is created, you can preview it with the following command: + + ${blueBright`coveo org:config:preview -t ${target} -s ${snapshot.id}`} + + ` + ); + } + } + private async handleReportWithErrors(snapshot: Snapshot) { // TODO: CDX-362: handle invalid snapshot cases const cfg = await this.configuration.get(); diff --git a/packages/cli/src/commands/org/config/pull.ts b/packages/cli/src/commands/org/config/pull.ts index c1bd8a15eb..06b937d9e0 100644 --- a/packages/cli/src/commands/org/config/pull.ts +++ b/packages/cli/src/commands/org/config/pull.ts @@ -120,7 +120,7 @@ export default class Pull extends Command { } cli.action.start('Creating Snapshot'); return SnapshotFactory.createFromOrg( - this.ResourceSnapshotTypesToExport, + this.resourceSnapshotTypesToExport, target ); } @@ -129,7 +129,7 @@ export default class Pull extends Command { return new Config(this.config.configDir, this.error); } - private get ResourceSnapshotTypesToExport() { + private get resourceSnapshotTypesToExport() { const {flags} = this.parse(Pull); return flags.resourceTypes.map((type) => ResourceSnapshotType[type]); } diff --git a/packages/cli/src/commands/org/config/push.ts b/packages/cli/src/commands/org/config/push.ts index 57c4284ae8..4581905e3e 100644 --- a/packages/cli/src/commands/org/config/push.ts +++ b/packages/cli/src/commands/org/config/push.ts @@ -11,11 +11,11 @@ import { displayInvalidSnapshotError, displaySnapshotSynchronizationWarning, dryRun, + DryRunOptions, getTargetOrg, handleSnapshotError, } from '../../../lib/snapshot/snapshotCommon'; import {Config} from '../../../lib/config/config'; -import {DryRunOptions} from '@coveord/platform-client'; import {cwd} from 'process'; import { buildAnalyticsFailureHook, diff --git a/packages/cli/src/lib/snapshot/snapshotCommon.ts b/packages/cli/src/lib/snapshot/snapshotCommon.ts index 56372f32a2..7e3c626f66 100644 --- a/packages/cli/src/lib/snapshot/snapshotCommon.ts +++ b/packages/cli/src/lib/snapshot/snapshotCommon.ts @@ -9,24 +9,28 @@ import dedent from 'ts-dedent'; import {Config, Configuration} from '../config/config'; import {SnapshotOperationTimeoutError} from '../errors'; -export interface dryRunOptions { +export interface DryRunOptions { deleteMissingResources?: boolean; + snapshotId?: string; } export async function dryRun( targetOrg: string, projectPath: string, - options?: dryRunOptions + options?: DryRunOptions ) { - const defaultOptions: Required = { + const defaultOptions: DryRunOptions = { deleteMissingResources: false, }; const opt = {...defaultOptions, ...options}; const project = new Project(normalize(projectPath)); - cli.action.start('Creating snapshot'); - const snapshot = await createSnapshotFromProject(project, targetOrg); + const snapshot = await getSnapshotForDryRun( + project, + targetOrg, + opt.snapshotId + ); cli.action.start('Validating snapshot'); const reporter = await snapshot.validate(opt.deleteMissingResources); @@ -102,3 +106,16 @@ async function createSnapshotFromProject( const pathToZip = await project.compressResources(); return SnapshotFactory.createFromZip(pathToZip, targetOrg); } + +async function getSnapshotForDryRun( + project: Project, + targetOrg: string, + snapshotId?: string +) { + if (snapshotId) { + cli.action.start('Retrieving Snapshot'); + return SnapshotFactory.createFromExistingSnapshot(snapshotId, targetOrg); + } + cli.action.start('Creating Snapshot'); + return createSnapshotFromProject(project, targetOrg); +}