Skip to content

Commit

Permalink
fix(snapshot): do not delete snapshot when provided with the flag (#840)
Browse files Browse the repository at this point in the history
* fix(snapshot): do not delete snapshot when provided with the flag

https://coveord.atlassian.net/browse/CDX-1042

* lint

https://coveord.atlassian.net/browse/CDX-1042

* Update packages/cli/src/commands/org/resources/pull.ts

Co-authored-by: jpmarceau <[email protected]>

* remove unused-ish code

https://coveord.atlassian.net/browse/CDX-1042

* rm unused import

https://coveord.atlassian.net/browse/CDX-1042

Co-authored-by: jpmarceau <[email protected]>
  • Loading branch information
louis-bompart and jpmarceau authored Jun 20, 2022
1 parent 0cae6ec commit 4ca2807
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 52 deletions.
14 changes: 14 additions & 0 deletions packages/cli/src/commands/org/resources/pull.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ const doMockSnapshotFactory = async () => {
download: mockedDownloadSnapshot,
} as unknown as Snapshot)
);
mockedSnapshotFactory.createFromExistingSnapshot.mockReturnValue(
Promise.resolve({
delete: mockedDeleteSnapshot,
download: mockedDownloadSnapshot,
} as unknown as Snapshot)
);
};

describe('org:resources:pull', () => {
Expand Down Expand Up @@ -166,6 +172,14 @@ describe('org:resources:pull', () => {
expect(mockedDeleteSnapshot).toHaveBeenCalled();
});

test
.stdout()
.stderr()
.command(['org:resources:pull', '-s', 'someSnapshotId'])
.it('should not delete the snapshot', () => {
expect(mockedDeleteSnapshot).not.toHaveBeenCalled();
});

test
.stdout()
.stderr()
Expand Down
116 changes: 64 additions & 52 deletions packages/cli/src/commands/org/resources/pull.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {ResourceSnapshotType} from '@coveord/platform-client';
import {Flags, Command, CliUx} from '@oclif/core';
import {blueBright, bold} from 'chalk';
import {blueBright} from 'chalk';
import {readJsonSync} from 'fs-extra';
import {cwd} from 'process';
import dedent from 'ts-dedent';
import {formatOrgId} from '../../../lib/commonFormaters';
import {Config} from '../../../lib/config/config';
import {
HasNecessaryCoveoPrivileges,
Expand Down Expand Up @@ -33,6 +34,20 @@ import {SnapshotFactory} from '../../../lib/snapshot/snapshotFactory';
import {confirmWithAnalytics} from '../../../lib/utils/cli';
import {spawnProcess} from '../../../lib/utils/process';

const PullCommandStrings = {
projectOverwriteQuestion: (
resourceFolderName: string
) => dedent`There is already a Coveo project with resources in it.
This command will delete the project in ${resourceFolderName} folder and start a new one, do you want to proceed? (y/n)`,
howToPullAfterTimeout: (targetOrgId: string, snapshotId: string) => dedent`
Once the snapshot is created, you can pull it with the following command:
${blueBright`coveo org:resources:pull -o ${targetOrgId} -s ${snapshotId}`}
`,
};

export default class Pull extends Command {
public static description = '(beta) Pull resources from an organization';

Expand Down Expand Up @@ -103,10 +118,18 @@ export default class Pull extends Command {
CliUx.ux.action.start('Updating project with Snapshot');
await this.refreshProject(project, snapshot);
project.writeResourcesManifest(targetOrganization);
await snapshot.delete();

if (await this.shouldDeleteSnapshot()) {
await snapshot.delete();
}

CliUx.ux.action.stop('Project updated');
}

private async shouldDeleteSnapshot() {
return !(await this.parse(Pull)).flags.snapshotId;
}

@Trackable()
public async catch(err?: Error & {exitCode?: number}) {
cleanupProject(this.projectPath);
Expand All @@ -120,15 +143,7 @@ export default class Pull extends Command {
if (err instanceof SnapshotOperationTimeoutError) {
const snapshot = err.snapshot;
const target = await this.getTargetOrg();
this.log(
dedent`
Once the snapshot is created, you can pull it with the following command:
${blueBright`coveo org:resources:pull -t ${target} -s ${snapshot.id}`}
`
);
this.log(PullCommandStrings.howToPullAfterTimeout(target, snapshot.id));
}
}

Expand All @@ -144,41 +159,54 @@ export default class Pull extends Command {
}

private async ensureProjectReset(project: Project) {
const flags = await this.getFlags();
if (!flags.overwrite && project.contains(Project.resourceFolderName)) {
const question = dedent`There is already a Coveo project with resources in it.
This command will overwrite the ${Project.resourceFolderName} folder content, do you want to proceed? (y/n)`;

const overwrite = await confirmWithAnalytics(
question,
'project overwrite'
);
if (!overwrite) {
throw new ProcessAbort();
}
if (await this.shouldAbortProjectReset(project)) {
throw new ProcessAbort();
}

project.reset();
}

private async shouldAbortProjectReset(project: Project) {
const flags = await this.getFlags();
return (
!flags.overwrite &&
project.contains(Project.resourceFolderName) &&
!(await this.askUserShouldWeOverwrite())
);
}

private async askUserShouldWeOverwrite() {
const question = PullCommandStrings.projectOverwriteQuestion(
Project.resourceFolderName
);
return confirmWithAnalytics(question, 'project overwrite');
}

private async getSnapshot() {
const flags = await this.getFlags();
const target = await this.getTargetOrg();
if (flags.snapshotId) {
CliUx.ux.action.start('Retrieving Snapshot');
const waitOption = await this.getWaitOption();
return SnapshotFactory.createFromExistingSnapshot(
flags.snapshotId,
target,
waitOption
);
}
return flags.snapshotId
? this.getExistingSnapshot(flags.snapshotId, target)
: this.createAndGetNewSnapshot(target);
}

private async createAndGetNewSnapshot(target: string) {
const resourcesToExport = await this.getResourceSnapshotTypesToExport();
CliUx.ux.action.start(`Creating Snapshot from ${bold.cyan(target)}`);
CliUx.ux.action.start(`Creating Snapshot from ${formatOrgId(target)}`);
const waitOption = await this.getWaitOption();
return SnapshotFactory.createFromOrg(resourcesToExport, target, waitOption);
}

private async getExistingSnapshot(snapshotId: string, target: string) {
CliUx.ux.action.start('Retrieving Snapshot');
const waitOption = await this.getWaitOption();
return SnapshotFactory.createFromExistingSnapshot(
snapshotId,
target,
waitOption
);
}

private async getWaitOption(): Promise<WaitUntilDoneOptions> {
const flags = await this.getFlags();
return {wait: flags.wait};
Expand All @@ -190,25 +218,9 @@ export default class Pull extends Command {

private async getResourceSnapshotTypesToExport(): Promise<SnapshotPullModelResources> {
const flags = await this.getFlags();
if (flags.model) {
const cfg = this.configuration.get();
if (cfg.organization !== flags.model.orgId) {
const question = dedent`You are currently connected to the ${bold.cyan(
cfg.organization
)} organization, but are about to pull resources from the ${bold.cyan(
flags.model.orgId
)} organization.
Do you wish to continue? (y/n)`;
const pull = await confirmWithAnalytics(question, 'resource pull');
if (!pull) {
throw new ProcessAbort();
}
}

return flags.model.resourcesToExport;
} else {
return buildResourcesToExport(flags.resourceTypes);
}
return flags.model
? flags.model.resourcesToExport
: buildResourcesToExport(flags.resourceTypes);
}

private async getTargetOrg() {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/lib/commonFormaters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import chalk from 'chalk';
export const formatOrgId = chalk.bold.cyan;

0 comments on commit 4ca2807

Please sign in to comment.