2018-05-14 19:37:52 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib/services/collection.service';
|
|
|
|
import { FolderService } from 'jslib/services/folder.service';
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
import { Response } from '../models/response';
|
|
|
|
import { CipherResponse } from '../models/response/cipherResponse';
|
|
|
|
import { CollectionResponse } from '../models/response/collectionResponse';
|
|
|
|
import { FolderResponse } from '../models/response/folderResponse';
|
|
|
|
import { ListResponse } from '../models/response/listResponse';
|
|
|
|
|
2018-05-14 19:37:52 +02:00
|
|
|
export class ListCommand {
|
|
|
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
|
|
|
private collectionService: CollectionService) { }
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
async run(object: string, cmd: program.Command): Promise<Response> {
|
2018-05-14 19:37:52 +02:00
|
|
|
switch (object) {
|
|
|
|
case 'items':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.listCiphers();
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'folders':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.listFolders();
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'collections':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.listCollections();
|
2018-05-14 19:37:52 +02:00
|
|
|
default:
|
2018-05-14 20:54:19 +02:00
|
|
|
return Response.badRequest('Unknown object.');
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async listCiphers() {
|
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new ListResponse(ciphers.map((o) => new CipherResponse(o)));
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async listFolders() {
|
|
|
|
const folders = await this.folderService.getAllDecrypted();
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new ListResponse(folders.map((o) => new FolderResponse(o)));
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async listCollections() {
|
|
|
|
const collections = await this.collectionService.getAllDecrypted();
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new ListResponse(collections.map((o) => new CollectionResponse(o)));
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
}
|