2018-05-17 16:58:30 +02:00
|
|
|
import * as program from 'commander';
|
2018-05-23 17:16:23 +02:00
|
|
|
import * as inquirer from 'inquirer';
|
2018-05-17 16:58:30 +02:00
|
|
|
|
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
|
|
|
import { ExportService } from 'jslib/abstractions/export.service';
|
|
|
|
|
2019-03-16 03:34:59 +01:00
|
|
|
import { Response } from 'jslib/cli/models/response';
|
2018-05-17 16:58:30 +02:00
|
|
|
|
|
|
|
import { CliUtils } from '../utils';
|
|
|
|
|
2019-10-07 17:01:30 +02:00
|
|
|
import { Utils } from 'jslib/misc/utils';
|
|
|
|
|
2018-05-17 16:58:30 +02:00
|
|
|
export class ExportCommand {
|
2018-08-14 21:13:40 +02:00
|
|
|
constructor(private cryptoService: CryptoService, private exportService: ExportService) { }
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async run(password: string, options: program.OptionValues): Promise<Response> {
|
2020-03-12 20:37:44 +01:00
|
|
|
const canInteract = process.env.BW_NOINTERACTION !== 'true';
|
2019-10-21 22:04:51 +02:00
|
|
|
if ((password == null || password === '') && canInteract) {
|
2018-06-19 04:07:45 +02:00
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
2018-05-23 17:16:23 +02:00
|
|
|
type: 'password',
|
|
|
|
name: 'password',
|
|
|
|
message: 'Master password:',
|
2018-05-17 16:58:30 +02:00
|
|
|
});
|
2018-05-23 17:16:23 +02:00
|
|
|
password = answer.password;
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
if (password == null || password === '') {
|
|
|
|
return Response.badRequest('Master password is required.');
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:13:40 +02:00
|
|
|
const keyHash = await this.cryptoService.hashPassword(password, null);
|
2018-05-17 16:58:30 +02:00
|
|
|
const storedKeyHash = await this.cryptoService.getKeyHash();
|
|
|
|
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
|
2021-02-03 18:44:33 +01:00
|
|
|
let format = options.format;
|
2020-12-08 18:35:37 +01:00
|
|
|
if (format !== 'encrypted_json' && format !== 'json') {
|
|
|
|
format = 'csv';
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
if (options.organizationid != null && !Utils.isGuid(options.organizationid)) {
|
|
|
|
return Response.error('`' + options.organizationid + '` is not a GUID.');
|
2019-10-07 17:01:30 +02:00
|
|
|
}
|
2020-08-25 20:24:43 +02:00
|
|
|
let exportContent: string = null;
|
2019-10-07 17:01:30 +02:00
|
|
|
try {
|
2021-02-03 18:44:33 +01:00
|
|
|
exportContent = options.organizationid != null ?
|
|
|
|
await this.exportService.getOrganizationExport(options.organizationid, format) :
|
2019-10-07 17:01:30 +02:00
|
|
|
await this.exportService.getExport(format);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
return await this.saveFile(exportContent, options, format);
|
2018-05-17 16:58:30 +02:00
|
|
|
} else {
|
|
|
|
return Response.error('Invalid master password.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async saveFile(exportContent: string, options: program.OptionValues, format: string): Promise<Response> {
|
2018-05-17 19:28:22 +02:00
|
|
|
try {
|
2021-05-27 22:47:39 +02:00
|
|
|
const fileName = this.getFileName(format, options.organizationid != null ? 'org' : null);
|
2021-02-03 18:44:33 +01:00
|
|
|
return await CliUtils.saveResultToFile(exportContent, options.output, fileName);
|
2018-05-17 19:28:22 +02:00
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e.toString());
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:47:39 +02:00
|
|
|
|
|
|
|
private getFileName(format: string, prefix?: string) {
|
|
|
|
if (format === 'encrypted_json') {
|
|
|
|
if (prefix == null) {
|
|
|
|
prefix = 'encrypted';
|
|
|
|
} else {
|
|
|
|
prefix = 'encrypted_' + prefix;
|
|
|
|
}
|
|
|
|
format = 'json';
|
|
|
|
}
|
|
|
|
return this.exportService.getFileName(prefix, format);
|
|
|
|
}
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|