From 920ce30dd07e5a691a55e2130417994e7d9924d0 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 17 Dec 2018 10:36:40 -0500 Subject: [PATCH] export file format option --- jslib | 2 +- src/commands/export.command.ts | 9 +++++---- src/program.ts | 12 +++++++++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/jslib b/jslib index 94f103c474..e7b5868aad 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 94f103c474655a81f43ad1f108b4408e4ffdcc17 +Subproject commit e7b5868aadf8412f49bfaba378d05c692124e265 diff --git a/src/commands/export.command.ts b/src/commands/export.command.ts index b1cdfebca6..37a02a6f9a 100644 --- a/src/commands/export.command.ts +++ b/src/commands/export.command.ts @@ -28,16 +28,17 @@ export class ExportCommand { const keyHash = await this.cryptoService.hashPassword(password, null); const storedKeyHash = await this.cryptoService.getKeyHash(); if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) { - const csv = await this.exportService.getExport('csv'); - return await this.saveFile(csv, cmd); + const format = cmd.format !== 'json' ? 'csv' : 'json'; + const csv = await this.exportService.getExport(format); + return await this.saveFile(csv, cmd, format); } else { return Response.error('Invalid master password.'); } } - async saveFile(csv: string, cmd: program.Command): Promise { + async saveFile(csv: string, cmd: program.Command, format: string): Promise { try { - const filePath = await CliUtils.saveFile(csv, cmd.output, this.exportService.getFileName()); + const filePath = await CliUtils.saveFile(csv, cmd.output, this.exportService.getFileName(null, format)); const res = new MessageResponse('Saved ' + filePath, null); res.raw = filePath; return Response.success(res); diff --git a/src/program.ts b/src/program.ts index 44023755cd..b284f3a1eb 100644 --- a/src/program.ts +++ b/src/program.ts @@ -429,15 +429,21 @@ export class Program { program .command('export [password]') - .description('Export vault data to a CSV file.') + .description('Export vault data to a CSV or JSON file.') .option('--output ', 'Output directory or filename.') + .option('--format ', 'Export file format.') .on('--help', () => { - writeLn('\n Examples:'); + writeLn('\n Notes:'); + writeLn(''); + writeLn(' Valid formats are `csv` and `json`. Default format is `csv`.'); + writeLn(''); + writeLn(' Examples:'); writeLn(''); writeLn(' bw export'); writeLn(' bw export myPassword321'); + writeLn(' bw export myPassword321 --format json'); writeLn(' bw export --output ./exp/bw.csv'); - writeLn(' bw export myPassword321 --output bw.csv'); + writeLn(' bw export myPassword321 --output bw.json --format json'); writeLn('', true); }) .action(async (password, cmd) => {