Skip to content

Commit 7af9d5c

Browse files
committed
added delete items commands
1 parent 9b6b7a8 commit 7af9d5c

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Command, Flags } from '@oclif/core';
2+
import { ConfigService } from '../services/config.service';
3+
import { CLIUtils } from '../utils/cli.utils';
4+
import { MissingCredentialsError, NotValidFolderUuidError } from '../types/command.types';
5+
import { ValidationService } from '../services/validation.service';
6+
import { ErrorUtils } from '../utils/errors.utils';
7+
import { TrashService } from '../services/drive/trash.service';
8+
import { DriveFolderService } from '../services/drive/drive-folder.service';
9+
10+
export default class DeletePermanentlyFolder extends Command {
11+
static readonly args = {};
12+
static readonly description = 'Deletes permanently a folder. This action cannot be undone.';
13+
static readonly aliases = ['delete:permanently:folder'];
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 folder 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(DeletePermanentlyFolder);
27+
28+
const nonInteractive = flags['non-interactive'];
29+
30+
const userCredentials = await ConfigService.instance.readUser();
31+
if (!userCredentials) throw new MissingCredentialsError();
32+
33+
const folderUuid = await this.getFolderUuid(flags['id'], nonInteractive);
34+
const driveFolder = await DriveFolderService.instance.getFolderMetaByUuid(folderUuid);
35+
if (!driveFolder) {
36+
throw new Error('Folder not found');
37+
}
38+
39+
await TrashService.instance.deleteFolder(driveFolder.id);
40+
const message = 'Folder 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 getFolderUuid = async (folderUuidFlag: string | undefined, nonInteractive: boolean): Promise<string> => {
52+
const folderUuid = await CLIUtils.getValueFromFlag(
53+
{
54+
value: folderUuidFlag,
55+
name: DeletePermanentlyFolder.flags['id'].name,
56+
},
57+
{
58+
nonInteractive,
59+
prompt: {
60+
message: 'What is the folder 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 NotValidFolderUuidError(),
67+
},
68+
this.log.bind(this),
69+
);
70+
return folderUuid;
71+
};
72+
}

src/services/drive/trash.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ export class TrashService {
1010
return storageClient.addItemsToTrash(payload);
1111
};
1212

13+
public deleteFile = (payload: StorageTypes.DeleteFilePayload) => {
14+
const storageClient = SdkManager.instance.getStorage(false);
15+
return storageClient.deleteFile(payload);
16+
};
17+
18+
public deleteFolder = (folderId: number) => {
19+
const storageClient = SdkManager.instance.getStorage(false);
20+
return storageClient.deleteFolder(folderId);
21+
};
22+
1323
public clearTrash = () => {
1424
const storageClient = SdkManager.instance.getTrash();
1525
return storageClient.clearTrash();

0 commit comments

Comments
 (0)