diff --git a/src/commands/create.command.ts b/src/commands/create.command.ts index 904202f128..e3eaeeb430 100644 --- a/src/commands/create.command.ts +++ b/src/commands/create.command.ts @@ -9,13 +9,23 @@ import { StringResponse } from '../models/response/stringResponse'; import { Cipher } from '../models/cipher'; import { Folder } from '../models/folder'; +import { CliUtils } from '../utils'; + export class CreateCommand { constructor(private cipherService: CipherService, private folderService: FolderService) { } - async run(object: string, requestData: string, cmd: program.Command): Promise { + async run(object: string, requestJson: string, cmd: program.Command): Promise { + if (requestJson == null || requestJson === '') { + requestJson = await CliUtils.readStdin(); + } + + if (requestJson == null || requestJson === '') { + return Response.badRequest('`requestJson` was not provided.'); + } + let req: any = null; try { - const reqJson = new Buffer(requestData, 'base64').toString(); + const reqJson = new Buffer(requestJson, 'base64').toString(); req = JSON.parse(reqJson); } catch (e) { return Response.badRequest('Error parsing the encoded request data.'); diff --git a/src/commands/edit.command.ts b/src/commands/edit.command.ts index 557b8ff153..5550a392b6 100644 --- a/src/commands/edit.command.ts +++ b/src/commands/edit.command.ts @@ -8,13 +8,23 @@ import { Response } from '../models/response'; import { Cipher } from '../models/cipher'; import { Folder } from '../models/folder'; +import { CliUtils } from 'src/utils'; + export class EditCommand { constructor(private cipherService: CipherService, private folderService: FolderService) { } - async run(object: string, id: string, requestData: string, cmd: program.Command): Promise { + async run(object: string, id: string, requestJson: string, cmd: program.Command): Promise { + if (requestJson == null || requestJson === '') { + requestJson = await CliUtils.readStdin(); + } + + if (requestJson == null || requestJson === '') { + return Response.badRequest('`requestJson` was not provided.'); + } + let req: any = null; try { - const reqJson = new Buffer(requestData, 'base64').toString(); + const reqJson = new Buffer(requestJson, 'base64').toString(); req = JSON.parse(reqJson); } catch (e) { return Response.badRequest('Error parsing the encoded request data.'); diff --git a/src/commands/encode.command.ts b/src/commands/encode.command.ts index 3f52708b93..1453036ad4 100644 --- a/src/commands/encode.command.ts +++ b/src/commands/encode.command.ts @@ -3,29 +3,16 @@ import * as program from 'commander'; import { Response } from '../models/response'; import { StringResponse } from '../models/response/stringResponse'; -export class EncodeCommand { - run(cmd: program.Command): Promise { - if (process.stdin == null || process.stdin.isTTY) { - return Promise.resolve(Response.badRequest('No stdin was piped in.')); - } +import { CliUtils } from '../utils'; - return new Promise((resolve, reject) => { - let input: string = ''; - process.stdin.setEncoding('utf8'); - process.stdin.on('readable', () => { - while (true) { - const chunk = process.stdin.read(); - if (chunk == null) { - break; - } - input += chunk; - } - }); - process.stdin.on('end', () => { - const b64 = new Buffer(input, 'utf8').toString('base64'); - const res = new StringResponse(b64); - resolve(Response.success(res)); - }); - }); +export class EncodeCommand { + async run(cmd: program.Command): Promise { + if (process.stdin.isTTY) { + return Response.badRequest('No stdin was piped in.'); + } + const input = await CliUtils.readStdin(); + const b64 = new Buffer(input, 'utf8').toString('base64'); + const res = new StringResponse(b64); + return Response.success(res); } } diff --git a/src/program.ts b/src/program.ts index 3d03ed681a..bae5ab25c3 100644 --- a/src/program.ts +++ b/src/program.ts @@ -161,22 +161,22 @@ export class Program { }); program - .command('create ') + .command('create [encodedJson]') .description('Create an object.') - .action(async (object, encodedData, cmd) => { + .action(async (object, encodedJson, cmd) => { await this.exitIfLocked(); const command = new CreateCommand(this.main.cipherService, this.main.folderService); - const response = await command.run(object, encodedData, cmd); + const response = await command.run(object, encodedJson, cmd); this.processResponse(response); }); program - .command('edit ') + .command('edit [encodedJson]') .description('Edit an object.') - .action(async (object, id, encodedData, cmd) => { + .action(async (object, id, encodedJson, cmd) => { await this.exitIfLocked(); const command = new EditCommand(this.main.cipherService, this.main.folderService); - const response = await command.run(object, id, encodedData, cmd); + const response = await command.run(object, id, encodedJson, cmd); this.processResponse(response); }); diff --git a/src/utils.ts b/src/utils.ts index 9ffe241b05..63cfd3aac7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,32 @@ import { CollectionView } from 'jslib/models/view/collectionView'; import { FolderView } from 'jslib/models/view/folderView'; export class CliUtils { + static readStdin(): Promise { + return new Promise((resolve, reject) => { + let input: string = ''; + + if (process.stdin.isTTY) { + resolve(input); + return; + } + + process.stdin.setEncoding('utf8'); + process.stdin.on('readable', () => { + while (true) { + const chunk = process.stdin.read(); + if (chunk == null) { + break; + } + input += chunk; + } + }); + + process.stdin.on('end', () => { + resolve(input); + }); + }); + } + static searchCiphers(ciphers: CipherView[], search: string) { search = search.toLowerCase(); return ciphers.filter((c) => {