1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-25 16:59:17 +01:00

sub out create command

This commit is contained in:
Kyle Spearrin 2018-05-14 21:19:49 -04:00
parent 428f46af06
commit b5507813ab
4 changed files with 74 additions and 1 deletions

View File

@ -0,0 +1,46 @@
import * as program from 'commander';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { FolderService } from 'jslib/services/folder.service';
import { Response } from '../models/response';
import { CipherRequest } from '../models/request/cipherRequest';
import { FolderRequest } from '../models/request/folderRequest';
export class CreateCommand {
constructor(private cipherService: CipherService, private folderService: FolderService) { }
async run(object: string, requestData: string, cmd: program.Command): Promise<Response> {
const reqJson = new Buffer(requestData, 'base64').toString();
const req = JSON.parse(reqJson);
switch (object.toLowerCase()) {
case 'item':
return await this.createCipher(req);
case 'folder':
return await this.createFolder(req);
default:
return Response.badRequest('Unknown object.');
}
}
private async createCipher(req: CipherRequest) {
const cipher = await this.cipherService.encrypt(CipherRequest.toView(req));
try {
await this.cipherService.saveWithServer(cipher);
return Response.success();
} catch (e) {
return Response.error(e.toString());
}
}
private async createFolder(req: FolderRequest) {
const folder = await this.folderService.encrypt(FolderRequest.toView(req));
try {
await this.folderService.saveWithServer(folder);
return Response.success();
} catch (e) {
return Response.error(e.toString());
}
}
}

View File

@ -1,10 +1,13 @@
import { CipherType } from 'jslib/enums/cipherType'; import { CipherType } from 'jslib/enums/cipherType';
import { LoginRequest } from './loginRequest'; import { LoginRequest } from './loginRequest';
import { SecureNoteRequest } from './secureNoteRequest'; import { SecureNoteRequest } from './secureNoteRequest';
import { CardRequest } from './cardRequest'; import { CardRequest } from './cardRequest';
import { IdentityRequest } from './identityRequest'; import { IdentityRequest } from './identityRequest';
import { FieldRequest } from './fieldRequest'; import { FieldRequest } from './fieldRequest';
import { CipherView } from 'jslib/models/view/cipherView';
export class CipherRequest { export class CipherRequest {
static template(): CipherRequest { static template(): CipherRequest {
var req = new CipherRequest(); var req = new CipherRequest();
@ -22,6 +25,12 @@ export class CipherRequest {
return req; return req;
} }
static toView(req: CipherRequest) {
const view = new CipherView();
view.name = req.name;
return view;
}
type: CipherType; type: CipherType;
folderId: string; folderId: string;
organizationId: string; organizationId: string;

View File

@ -1,3 +1,11 @@
import { FolderView } from 'jslib/models/view/folderView';
export class FolderRequest { export class FolderRequest {
static toView(req: FolderRequest) {
const view = new FolderView();
view.name = req.name;
return view;
}
name: string; name: string;
} }

View File

@ -9,10 +9,11 @@ import { SyncCommand } from './commands/sync.command';
import { Main } from './main'; import { Main } from './main';
import { Response } from './models/response'; import { Response } from './models/response';
import { CreateCommand } from './commands/create.command';
import { EncodeCommand } from './commands/encode.command';
import { ListResponse } from './models/response/listResponse'; import { ListResponse } from './models/response/listResponse';
import { StringResponse } from './models/response/stringResponse'; import { StringResponse } from './models/response/stringResponse';
import { TemplateResponse } from './models/response/templateResponse'; import { TemplateResponse } from './models/response/templateResponse';
import { EncodeCommand } from './commands/encode.command';
export class Program { export class Program {
constructor(private main: Main) { } constructor(private main: Main) { }
@ -70,6 +71,15 @@ export class Program {
this.processResponse(response); this.processResponse(response);
}); });
program
.command('create <object> <encodedData>')
.description('Create an object.')
.action(async (object, encodedData, cmd) => {
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, encodedData, cmd);
this.processResponse(response);
});
program program
.command('edit <object> <id>') .command('edit <object> <id>')
.description('Edit an object.') .description('Edit an object.')