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';
|
|
|
|
|
|
|
|
import { Response } from '../models/response';
|
|
|
|
import { MessageResponse } from '../models/response/messageResponse';
|
|
|
|
|
|
|
|
import { CliUtils } from '../utils';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
async run(password: string, cmd: program.Command): Promise<Response> {
|
|
|
|
if (password == null || password === '') {
|
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) {
|
2018-07-05 21:29:40 +02:00
|
|
|
const csv = await this.exportService.getExport('csv');
|
2018-05-17 16:58:30 +02:00
|
|
|
return await this.saveFile(csv, cmd);
|
|
|
|
} else {
|
|
|
|
return Response.error('Invalid master password.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveFile(csv: string, cmd: program.Command): Promise<Response> {
|
2018-05-17 19:28:22 +02:00
|
|
|
try {
|
|
|
|
const filePath = await CliUtils.saveFile(csv, cmd.output, this.exportService.getFileName());
|
|
|
|
const res = new MessageResponse('Saved ' + filePath, null);
|
|
|
|
res.raw = filePath;
|
|
|
|
return Response.success(res);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e.toString());
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|