-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrename.ts
85 lines (68 loc) · 2.28 KB
/
rename.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Args, Flags } from '@oclif/core';
import type { ApifyApiError } from 'apify-client';
import chalk from 'chalk';
import { ApifyCommand } from '../../lib/apify_command.js';
import { tryToGetDataset } from '../../lib/commands/storages.js';
import { error, success } from '../../lib/outputs.js';
import { getLoggedClientOrThrow } from '../../lib/utils.js';
export class DatasetsRenameCommand extends ApifyCommand<typeof DatasetsRenameCommand> {
static override description = 'Change dataset name or removes name with --unname flag.';
static override flags = {
unname: Flags.boolean({
description: 'Removes the unique name of the dataset.',
}),
};
static override args = {
nameOrId: Args.string({
description: 'The dataset ID or name to delete.',
required: true,
}),
newName: Args.string({
description: 'The new name for the dataset.',
}),
};
async run() {
const { unname } = this.flags;
const { newName, nameOrId } = this.args;
if (!newName && !unname) {
error({ message: 'You must provide either a new name or the --unname flag.' });
return;
}
if (newName && unname) {
error({
message: 'You cannot provide a new name and the --unname flag.',
});
return;
}
const client = await getLoggedClientOrThrow();
const existingDataset = await tryToGetDataset(client, nameOrId);
if (!existingDataset) {
error({
message: `Dataset with ID or name "${nameOrId}" not found.`,
});
return;
}
const { id, name } = existingDataset.dataset;
const successMessage = (() => {
if (!name) {
return `The name of the dataset with ID ${chalk.yellow(id)} has been set to: ${chalk.yellow(newName)}`;
}
if (unname) {
return `The name of the dataset with ID ${chalk.yellow(id)} has been removed (was ${chalk.yellow(name)} previously).`;
}
return `The name of the dataset with ID ${chalk.yellow(id)} was changed from ${chalk.yellow(name)} to ${chalk.yellow(newName)}.`;
})();
try {
await existingDataset.datasetClient.update({ name: unname ? (null as never) : newName! });
success({
message: successMessage,
stdout: true,
});
} catch (err) {
const casted = err as ApifyApiError;
error({
message: `Failed to rename dataset with ID ${chalk.yellow(id)}\n ${casted.message || casted}`,
});
}
}
}