Skip to content

Commit b15bc17

Browse files
committed
feat: datasets rm / key-value-stores rm
1 parent ee9cbcc commit b15bc17

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

src/commands/datasets/rm.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Args } from '@oclif/core';
2+
import type { ApifyApiError } from 'apify-client';
3+
import chalk from 'chalk';
4+
5+
import { ApifyCommand } from '../../lib/apify_command.js';
6+
import { confirmAction } from '../../lib/commands/confirm.js';
7+
import { tryToGetDataset } from '../../lib/commands/storages.js';
8+
import { error, info, success } from '../../lib/outputs.js';
9+
import { getLoggedClientOrThrow } from '../../lib/utils.js';
10+
11+
export class DatasetsRmCommand extends ApifyCommand<typeof DatasetsRmCommand> {
12+
static override description = 'Deletes a Dataset';
13+
14+
static override args = {
15+
datasetNameOrId: Args.string({
16+
description: 'The Dataset ID or name to delete',
17+
required: true,
18+
}),
19+
};
20+
21+
async run() {
22+
const { datasetNameOrId } = this.args;
23+
24+
const client = await getLoggedClientOrThrow();
25+
26+
const existingDataset = await tryToGetDataset(client, datasetNameOrId);
27+
28+
if (!existingDataset) {
29+
error({
30+
message: `Dataset with ID or name "${datasetNameOrId}" not found.`,
31+
});
32+
33+
return;
34+
}
35+
36+
const confirmed = await confirmAction({ type: 'Dataset' });
37+
38+
if (!confirmed) {
39+
info({ message: 'Dataset deletion has been aborted.' });
40+
return;
41+
}
42+
43+
const { id, name } = existingDataset.dataset;
44+
45+
try {
46+
await existingDataset.datasetClient.delete();
47+
48+
success({
49+
message: `Dataset with ID ${chalk.yellow(id)}${name ? ` (called ${chalk.yellow(name)})` : ''} has been deleted.`,
50+
stdout: true,
51+
});
52+
} catch (err) {
53+
const casted = err as ApifyApiError;
54+
55+
error({
56+
message: `Failed to delete Dataset with ID ${chalk.yellow(id)}\n ${casted.message || casted}`,
57+
});
58+
}
59+
}
60+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Args } from '@oclif/core';
2+
import type { ApifyApiError } from 'apify-client';
3+
import chalk from 'chalk';
4+
5+
import { ApifyCommand } from '../../lib/apify_command.js';
6+
import { confirmAction } from '../../lib/commands/confirm.js';
7+
import { tryToGetKeyValueStore } from '../../lib/commands/storages.js';
8+
import { error, info, success } from '../../lib/outputs.js';
9+
import { getLoggedClientOrThrow } from '../../lib/utils.js';
10+
11+
export class KeyValueStoresRmCommand extends ApifyCommand<typeof KeyValueStoresRmCommand> {
12+
static override description = 'Deletes a Key-value Store';
13+
14+
static override hiddenAliases = ['kvs:rm'];
15+
16+
static override args = {
17+
keyValueStoreNameOrId: Args.string({
18+
description: 'The Key-value Store ID or name to delete',
19+
required: true,
20+
}),
21+
};
22+
23+
async run() {
24+
const { keyValueStoreNameOrId } = this.args;
25+
26+
const client = await getLoggedClientOrThrow();
27+
28+
const existingKvs = await tryToGetKeyValueStore(client, keyValueStoreNameOrId);
29+
30+
if (!existingKvs) {
31+
error({
32+
message: `Key-value Store with ID or name "${keyValueStoreNameOrId}" not found.`,
33+
});
34+
35+
return;
36+
}
37+
38+
const confirmed = await confirmAction({ type: 'Key-value Store' });
39+
40+
if (!confirmed) {
41+
info({ message: 'Key-value Store deletion has been aborted.' });
42+
return;
43+
}
44+
45+
const { id, name } = existingKvs.keyValueStore;
46+
47+
try {
48+
await existingKvs.keyValueStoreClient.delete();
49+
50+
success({
51+
message: `Key-value Store with ID ${chalk.yellow(id)}${name ? ` (called ${chalk.yellow(name)})` : ''} has been deleted.`,
52+
stdout: true,
53+
});
54+
} catch (err) {
55+
const casted = err as ApifyApiError;
56+
57+
error({
58+
message: `Failed to delete Key-value Store with ID ${chalk.yellow(id)}\n ${casted.message || casted}`,
59+
});
60+
}
61+
}
62+
}

src/lib/commands/confirm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const yesNoConfirmation = ({ type }: { type: string }): DistinctQuestion => ({
1010
const inputValidation = ({
1111
type,
1212
expectedValue,
13-
failureMessage,
14-
}: { type: string; expectedValue: string; failureMessage: string }): DistinctQuestion => ({
13+
failureMessage = 'That is not the correct input!',
14+
}: { type: string; expectedValue: string; failureMessage?: string }): DistinctQuestion => ({
1515
name: 'confirmed',
1616
type: 'input',
1717
message: `Are you sure you want to delete this ${type}? If so, please type in "${expectedValue}":`,
@@ -28,7 +28,7 @@ export async function confirmAction({
2828
expectedValue,
2929
type,
3030
failureMessage,
31-
}: { expectedValue?: string; type: string; failureMessage: string }): Promise<boolean> {
31+
}: { type: string; expectedValue?: string; failureMessage?: string }): Promise<boolean> {
3232
const result = await inquirer.prompt<{ confirmed: boolean | string }>(
3333
expectedValue ? inputValidation({ type, expectedValue, failureMessage }) : yesNoConfirmation({ type }),
3434
);

src/lib/commands/storages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ async function tryToGetStorage<T extends 'dataset' | 'keyValueStore'>(
4545
export async function tryToGetDataset(
4646
client: ApifyClient,
4747
datasetId: string,
48-
): Promise<{ dataset: Dataset | undefined; datasetClient: DatasetClient } | null> {
48+
): Promise<{ dataset: Dataset; datasetClient: DatasetClient } | null> {
4949
return tryToGetStorage(client, datasetId, 'dataset');
5050
}
5151

5252
export async function tryToGetKeyValueStore(
5353
client: ApifyClient,
5454
keyValueStoreId: string,
55-
): Promise<{ keyValueStore: KeyValueStore | undefined; keyValueStoreClient: KeyValueStoreClient } | null> {
55+
): Promise<{ keyValueStore: KeyValueStore; keyValueStoreClient: KeyValueStoreClient } | null> {
5656
return tryToGetStorage(client, keyValueStoreId, 'keyValueStore');
5757
}

0 commit comments

Comments
 (0)