Skip to content

Commit

Permalink
feat: add a prompt for snapshot deletion (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
agomez-sf authored May 6, 2024
1 parent 061a49a commit 3ad43fb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"alias": ["force:org:snapshot:delete"],
"command": "org:delete:snapshot",
"flagAliases": ["apiversion", "targetdevhubusername"],
"flagChars": ["s", "v"],
"flags": ["api-version", "flags-dir", "json", "loglevel", "snapshot", "target-dev-hub"],
"flagChars": ["p", "s", "v"],
"flags": ["api-version", "flags-dir", "json", "loglevel", "no-prompt", "snapshot", "target-dev-hub"],
"plugin": "@salesforce/plugin-signups"
},
{
Expand Down
8 changes: 8 additions & 0 deletions messages/snapshot.delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Name or ID of snapshot to delete.

The IDs of scratch org snapshots start with 0Oo.

# flags.no-prompt.summary

Don't prompt the user to confirm the deletion.

# prompt.confirm

Are you sure you want to delete the snapshot with name: %s?

# success

Successfully deleted snapshot %s.
15 changes: 12 additions & 3 deletions src/commands/org/delete/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const messages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot.d
// jsforce can return SaveError[] or never[]
const isSaveError = (error: SaveError): error is SaveError => error.message !== undefined;

export class SnapshotDelete extends SfCommand<SaveResult> {
export class SnapshotDelete extends SfCommand<SaveResult | undefined> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
Expand All @@ -41,11 +41,20 @@ export class SnapshotDelete extends SfCommand<SaveResult> {
description: messages.getMessage('flags.snapshot.description'),
required: true,
}),
'no-prompt': Flags.boolean({
char: 'p',
summary: messages.getMessage('flags.no-prompt.summary'),
}),
};

public async run(): Promise<SaveResult> {
// resolve the query to an ID. This also verifies the snapshot exists in the org
public async run(): Promise<SaveResult | undefined> {
const { flags } = await this.parse(SnapshotDelete);
const snapshot = flags['snapshot'];
if (!flags['no-prompt'] && !(await this.confirm({ message: messages.getMessage('prompt.confirm', [snapshot]) }))) {
return;
}

// resolve the query to an ID. This also verifies the snapshot exists in the org
const conn = flags['target-dev-hub'].getConnection(flags['api-version']);
const result = await queryByNameOrId(conn, flags.snapshot);
const deleteResult = await conn.sobject('OrgSnapshot').delete(result.Id);
Expand Down
16 changes: 8 additions & 8 deletions test/nuts/snapshots.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,26 @@ describe('snapshot commands', () => {
expect(table).not.to.include('.000+0000');
});

it('can delete a snapshot by id', () => {
execCmd(`force:org:snapshot:delete -s ${aliasSnapshot.Id} --json`, {
it('can delete a snapshot by id (no prompt)', () => {
execCmd(`force:org:snapshot:delete -s ${aliasSnapshot.Id} --json --no-prompt`, {
ensureExitCode: 0,
});
});

it('can delete a snapshot by name', () => {
execCmd(`force:org:snapshot:delete -s ${orgIdSnapshot.SnapshotName} --json`, {
it('can delete a snapshot by name (no prompt)', () => {
execCmd(`force:org:snapshot:delete -s ${orgIdSnapshot.SnapshotName} --json --no-prompt`, {
ensureExitCode: 0,
});
});

it('can delete the last snapshot by name', () => {
execCmd(`force:org:snapshot:delete -s ${usernameSnapshot.SnapshotName} --json`, {
it('can delete the last snapshot by name (no prompt)', () => {
execCmd(`force:org:snapshot:delete -s ${usernameSnapshot.SnapshotName} --json --no-prompt`, {
ensureExitCode: 0,
});
});

it('fails at deleting the same snapshot twice', () => {
execCmd(`force:org:snapshot:delete -s ${usernameSnapshot.SnapshotName} --json`, {
it('fails at deleting the same snapshot twice (no prompt)', () => {
execCmd(`force:org:snapshot:delete -s ${usernameSnapshot.SnapshotName} --json --no-prompt`, {
ensureExitCode: 1,
});
});
Expand Down

0 comments on commit 3ad43fb

Please sign in to comment.