Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snapshot): add --snapshotId flag to push and preview commands #387

Merged
merged 14 commits into from
Aug 3, 2021
Merged
31 changes: 30 additions & 1 deletion packages/cli/src/commands/org/config/preview.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/org/config/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class Pull extends Command {
}
cli.action.start('Creating Snapshot');
return SnapshotFactory.createFromOrg(
this.ResourceSnapshotTypesToExport,
this.resourceSnapshotTypesToExport,
target
);
}
Expand All @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/org/config/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 22 additions & 5 deletions packages/cli/src/lib/snapshot/snapshotCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<dryRunOptions> = {
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);
Expand Down Expand Up @@ -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);
}