Skip to content

Commit 3761d5e

Browse files
committed
feat: datasets rename / key-value-stores rename
1 parent 6cc92e7 commit 3761d5e

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

src/commands/datasets/rename.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Args, Flags } 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 { tryToGetDataset } from '../../lib/commands/storages.js';
7+
import { error, success } from '../../lib/outputs.js';
8+
import { getLoggedClientOrThrow } from '../../lib/utils.js';
9+
10+
export class DatasetsRenameCommand extends ApifyCommand<typeof DatasetsRenameCommand> {
11+
static override description = 'Renames a Dataset, or removes its unique name';
12+
13+
static override flags = {
14+
unname: Flags.boolean({
15+
description: 'Removes the unique name of the Dataset',
16+
}),
17+
};
18+
19+
static override args = {
20+
datasetNameOrId: Args.string({
21+
description: 'The Dataset ID or name to delete',
22+
required: true,
23+
}),
24+
newName: Args.string({
25+
description: 'The new name for the Dataset',
26+
}),
27+
};
28+
29+
async run() {
30+
const { unname } = this.flags;
31+
const { newName, datasetNameOrId } = this.args;
32+
33+
if (!newName && !unname) {
34+
error({ message: 'You must provide either a new name or the --unname flag.' });
35+
return;
36+
}
37+
38+
if (newName && unname) {
39+
error({
40+
message: 'You cannot provide a new name and the --unname flag.',
41+
});
42+
return;
43+
}
44+
45+
const client = await getLoggedClientOrThrow();
46+
const existingDataset = await tryToGetDataset(client, datasetNameOrId);
47+
48+
if (!existingDataset) {
49+
error({
50+
message: `Dataset with ID or name "${datasetNameOrId}" not found.`,
51+
});
52+
53+
return;
54+
}
55+
56+
const { id, name } = existingDataset.dataset;
57+
58+
const successMessage = (() => {
59+
if (!name) {
60+
return `The name of the Dataset with ID ${chalk.yellow(id)} has been set to: ${chalk.yellow(newName)}`;
61+
}
62+
63+
if (unname) {
64+
return `The name of the Dataset with ID ${chalk.yellow(id)} has been removed (was ${chalk.yellow(name)} previously).`;
65+
}
66+
67+
return `The name of the Dataset with ID ${chalk.yellow(id)} was changed from ${chalk.yellow(name)} to ${chalk.yellow(newName)}.`;
68+
})();
69+
70+
try {
71+
await existingDataset.datasetClient.update({ name: unname ? (null as never) : newName! });
72+
73+
success({
74+
message: successMessage,
75+
stdout: true,
76+
});
77+
} catch (err) {
78+
const casted = err as ApifyApiError;
79+
80+
error({
81+
message: `Failed to rename Dataset with ID ${chalk.yellow(id)}\n ${casted.message || casted}`,
82+
});
83+
}
84+
}
85+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Args, Flags } 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 { tryToGetKeyValueStore } from '../../lib/commands/storages.js';
7+
import { error, success } from '../../lib/outputs.js';
8+
import { getLoggedClientOrThrow } from '../../lib/utils.js';
9+
10+
export class KeyValueStoresRenameCommand extends ApifyCommand<typeof KeyValueStoresRenameCommand> {
11+
static override description = 'Renames a Key-value Store, or removes its unique name';
12+
13+
static override hiddenAliases = ['kvs:rename'];
14+
15+
static override flags = {
16+
unname: Flags.boolean({
17+
description: 'Removes the unique name of the Key-value Store',
18+
}),
19+
};
20+
21+
static override args = {
22+
keyValueStoreNameOrId: Args.string({
23+
description: 'The Key-value Store ID or name to delete',
24+
required: true,
25+
}),
26+
newName: Args.string({
27+
description: 'The new name for the Key-value Store',
28+
}),
29+
};
30+
31+
async run() {
32+
const { unname } = this.flags;
33+
const { newName, keyValueStoreNameOrId } = this.args;
34+
35+
if (!newName && !unname) {
36+
error({ message: 'You must provide either a new name or the --unname flag.' });
37+
return;
38+
}
39+
40+
if (newName && unname) {
41+
error({
42+
message: 'You cannot provide a new name and the --unname flag.',
43+
});
44+
return;
45+
}
46+
47+
const client = await getLoggedClientOrThrow();
48+
const existingDataset = await tryToGetKeyValueStore(client, keyValueStoreNameOrId);
49+
50+
if (!existingDataset) {
51+
error({
52+
message: `Key-value Store with ID or name "${keyValueStoreNameOrId}" not found.`,
53+
});
54+
55+
return;
56+
}
57+
58+
const { id, name } = existingDataset.keyValueStore;
59+
60+
const successMessage = (() => {
61+
if (!name) {
62+
return `The name of the Key-value Store with ID ${chalk.yellow(id)} has been set to: ${chalk.yellow(newName)}`;
63+
}
64+
65+
if (unname) {
66+
return `The name of the Key-value Store with ID ${chalk.yellow(id)} has been removed (was ${chalk.yellow(name)} previously).`;
67+
}
68+
69+
return `The name of the Key-value Store with ID ${chalk.yellow(id)} was changed from ${chalk.yellow(name)} to ${chalk.yellow(newName)}.`;
70+
})();
71+
72+
try {
73+
await existingDataset.keyValueStoreClient.update({ name: unname ? (null as never) : newName! });
74+
75+
success({
76+
message: successMessage,
77+
stdout: true,
78+
});
79+
} catch (err) {
80+
const casted = err as ApifyApiError;
81+
82+
error({
83+
message: `Failed to rename Key-value Store with ID ${chalk.yellow(id)}\n ${casted.message || casted}`,
84+
});
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)