From 9b99c299e433616b6ee3e300d45c72772746d98c Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 14 May 2018 17:13:57 -0400 Subject: [PATCH] encode command --- src/commands/encode.command.ts | 34 ++++++++++++++++++++++++++++++++++ src/commands/get.command.ts | 2 +- src/program.ts | 10 ++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/commands/encode.command.ts diff --git a/src/commands/encode.command.ts b/src/commands/encode.command.ts new file mode 100644 index 0000000000..ea04166129 --- /dev/null +++ b/src/commands/encode.command.ts @@ -0,0 +1,34 @@ +import * as program from 'commander'; + +import { CipherService } from 'jslib/abstractions/cipher.service'; +import { FolderService } from 'jslib/abstractions/folder.service'; + +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.')); + } + + 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'); + var res = new StringResponse(b64); + resolve(Response.success(res)); + }); + }); + } +} diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 5aebe8930b..b235019aab 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -125,7 +125,7 @@ export class GetCommand { template = SecureNoteRequest.template(); break; default: - return Response.badRequest('Unknown object.'); + return Response.badRequest('Unknown template object.'); } const res = new TemplateResponse(template); diff --git a/src/program.ts b/src/program.ts index 42c8f6cc57..b05724e651 100644 --- a/src/program.ts +++ b/src/program.ts @@ -12,6 +12,7 @@ import { Response } from './models/response'; import { ListResponse } from './models/response/listResponse'; import { StringResponse } from './models/response/stringResponse'; import { TemplateResponse } from './models/response/templateResponse'; +import { EncodeCommand } from './commands/encode.command'; export class Program { constructor(private main: Main) { } @@ -87,6 +88,15 @@ export class Program { this.processResponse(response); }); + program + .command('encode') + .description('Base64 encode stdin.') + .action(async (object, id, cmd) => { + const command = new EncodeCommand(); + const response = await command.run(cmd); + this.processResponse(response); + }); + program .parse(process.argv); }