|
| 1 | +import { Command, Flags } from '@oclif/core'; |
| 2 | +import { ConfigService } from '../services/config.service'; |
| 3 | +import { CLIUtils } from '../utils/cli.utils'; |
| 4 | +import { MissingCredentialsError, NotValidFileUuidError } from '../types/command.types'; |
| 5 | +import { ValidationService } from '../services/validation.service'; |
| 6 | +import { DriveFileService } from '../services/drive/drive-file.service'; |
| 7 | +import { ErrorUtils } from '../utils/errors.utils'; |
| 8 | +import { TrashService } from '../services/drive/trash.service'; |
| 9 | + |
| 10 | +export default class DeletePermanentlyFile extends Command { |
| 11 | + static readonly args = {}; |
| 12 | + static readonly description = 'Deletes permanently a file. This action cannot be undone.'; |
| 13 | + static readonly aliases = ['delete:permanently:file']; |
| 14 | + static readonly examples = ['<%= config.bin %> <%= command.id %>']; |
| 15 | + static readonly flags = { |
| 16 | + ...CLIUtils.CommonFlags, |
| 17 | + id: Flags.string({ |
| 18 | + char: 'i', |
| 19 | + description: 'The file id to be permanently deleted.', |
| 20 | + required: false, |
| 21 | + }), |
| 22 | + }; |
| 23 | + static readonly enableJsonFlag = true; |
| 24 | + |
| 25 | + public run = async () => { |
| 26 | + const { flags } = await this.parse(DeletePermanentlyFile); |
| 27 | + |
| 28 | + const nonInteractive = flags['non-interactive']; |
| 29 | + |
| 30 | + const userCredentials = await ConfigService.instance.readUser(); |
| 31 | + if (!userCredentials) throw new MissingCredentialsError(); |
| 32 | + |
| 33 | + const fileUuid = await this.getFileUuid(flags['id'], nonInteractive); |
| 34 | + const driveFile = await DriveFileService.instance.getFileMetadata(fileUuid); |
| 35 | + if (!driveFile) { |
| 36 | + throw new Error('File not found'); |
| 37 | + } |
| 38 | + |
| 39 | + await TrashService.instance.deleteFile({ fileId: driveFile.id, folderId: driveFile.folderId }); |
| 40 | + const message = 'File permanently deleted successfully'; |
| 41 | + CLIUtils.success(this.log.bind(this), message); |
| 42 | + return { success: true, message }; |
| 43 | + }; |
| 44 | + |
| 45 | + public catch = async (error: Error) => { |
| 46 | + ErrorUtils.report(this.error.bind(this), error, { command: this.id }); |
| 47 | + CLIUtils.error(this.log.bind(this), error.message); |
| 48 | + this.exit(1); |
| 49 | + }; |
| 50 | + |
| 51 | + private getFileUuid = async (fileUuidFlag: string | undefined, nonInteractive: boolean): Promise<string> => { |
| 52 | + const fileUuid = await CLIUtils.getValueFromFlag( |
| 53 | + { |
| 54 | + value: fileUuidFlag, |
| 55 | + name: DeletePermanentlyFile.flags['id'].name, |
| 56 | + }, |
| 57 | + { |
| 58 | + nonInteractive, |
| 59 | + prompt: { |
| 60 | + message: 'What is the file id you want to permanently delete? (This action cannot be undone)', |
| 61 | + options: { type: 'input' }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + validate: ValidationService.instance.validateUUIDv4, |
| 66 | + error: new NotValidFileUuidError(), |
| 67 | + }, |
| 68 | + this.log.bind(this), |
| 69 | + ); |
| 70 | + return fileUuid; |
| 71 | + }; |
| 72 | +} |
0 commit comments