|
| 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