standardize import command
This commit is contained in:
parent
2a8a8a8ba7
commit
336e4acdd0
2
jslib
2
jslib
|
@ -1 +1 @@
|
||||||
Subproject commit a5476f12aa156f8375d137ca3ac3ddce6d4ca7cc
|
Subproject commit 8b26d90e742966bff3dc9e57ecb104b59e48371b
|
|
@ -1,33 +1,33 @@
|
||||||
import * as program from 'commander';
|
import * as program from 'commander';
|
||||||
import * as inquirer from 'inquirer';
|
import * as inquirer from 'inquirer';
|
||||||
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||||
import { ImportOptions, ImportService } from 'jslib/abstractions/import.service';
|
import { ImportService } from 'jslib/abstractions/import.service';
|
||||||
import { UserService } from 'jslib/abstractions/user.service';
|
import { UserService } from 'jslib/abstractions/user.service';
|
||||||
|
|
||||||
import { Response } from '../models/response';
|
import { Response } from '../models/response';
|
||||||
|
import { MessageResponse } from '../models/response/messageResponse';
|
||||||
|
|
||||||
import { CliUtils } from '../utils';
|
import { CliUtils } from '../utils';
|
||||||
|
|
||||||
const writeLn = CliUtils.writeLn;
|
|
||||||
|
|
||||||
export class ImportCommand {
|
export class ImportCommand {
|
||||||
constructor(private cryptoService: CryptoService, private userService: UserService,
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
||||||
private importService: ImportService) { }
|
private importService: ImportService) { }
|
||||||
|
|
||||||
async list() {
|
async run(format: string, filepath: string, password: string, cmd: program.Command): Promise<Response> {
|
||||||
const options: ImportOptions = this.importService.getOptions().sort((a, b) => {
|
if (cmd.formats || false) {
|
||||||
if (a.id < b.id) { return -1; }
|
return this.list();
|
||||||
if (a.id > b.id) { return 1; }
|
} else {
|
||||||
return 0;
|
return this.import(format, filepath, password);
|
||||||
});
|
}
|
||||||
writeLn('\nSupported input formats:\n');
|
|
||||||
options.forEach((option) => {
|
|
||||||
writeLn(' ' + option.id);
|
|
||||||
});
|
|
||||||
return Response.success();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(type: string, path: string, password: string, cmd: program.Command): Promise<Response> {
|
private async import(format: string, filepath: string, password: string) {
|
||||||
|
if (format == null || format === '') {
|
||||||
|
return Response.badRequest('`format` was not provided.');
|
||||||
|
}
|
||||||
|
if (filepath == null || filepath === '') {
|
||||||
|
return Response.badRequest('`filepath` was not provided.');
|
||||||
|
}
|
||||||
if (password == null || password === '') {
|
if (password == null || password === '') {
|
||||||
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
||||||
type: 'password',
|
type: 'password',
|
||||||
|
@ -45,23 +45,39 @@ export class ImportCommand {
|
||||||
const key = await this.cryptoService.makeKey(password, email);
|
const key = await this.cryptoService.makeKey(password, email);
|
||||||
const keyHash = await this.cryptoService.hashPassword(password, key);
|
const keyHash = await this.cryptoService.hashPassword(password, key);
|
||||||
const storedKeyHash = await this.cryptoService.getKeyHash();
|
const storedKeyHash = await this.cryptoService.getKeyHash();
|
||||||
const importer = await this.importService.getImporter(type);
|
if (storedKeyHash == null || keyHash == null || storedKeyHash !== keyHash) {
|
||||||
|
return Response.badRequest('Invalid master password.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const importer = await this.importService.getImporter(format);
|
||||||
if (importer === null) {
|
if (importer === null) {
|
||||||
return Response.badRequest('Proper importer type required.');
|
return Response.badRequest('Proper importer type required.');
|
||||||
}
|
}
|
||||||
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
|
|
||||||
return CliUtils.readFile(path).then(async (contents) => {
|
try {
|
||||||
const submitResult = await this.importService.submit(importer, contents);
|
const contents = await CliUtils.readFile(filepath);
|
||||||
if (submitResult !== null) {
|
if (contents === null || contents === '') {
|
||||||
return Response.success();
|
return Response.badRequest('Import file was empty.');
|
||||||
} else {
|
}
|
||||||
return Response.badRequest(submitResult.message);
|
|
||||||
}
|
const submitResult = await this.importService.import(importer, contents);
|
||||||
}).catch((err) => {
|
if (submitResult !== null) {
|
||||||
return Response.badRequest(err);
|
const res = new MessageResponse('Imported ' + filepath, null);
|
||||||
});
|
return Response.success(res);
|
||||||
} else {
|
} else {
|
||||||
return Response.badRequest('Invalid master password.');
|
return Response.badRequest(submitResult.message);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
return Response.badRequest(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async list() {
|
||||||
|
const options = this.importService.importOptions.sort((a, b) => {
|
||||||
|
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
||||||
|
}).map((option) => option.id).join('\n');
|
||||||
|
const res = new MessageResponse('Supported input formats:', options);
|
||||||
|
res.raw = options;
|
||||||
|
return Response.success(res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,5 +13,11 @@
|
||||||
},
|
},
|
||||||
"noneFolder": {
|
"noneFolder": {
|
||||||
"message": "No Folder"
|
"message": "No Folder"
|
||||||
|
},
|
||||||
|
"importFormatError": {
|
||||||
|
"message": "Data is not formatted correctly. Please check your import file and try again."
|
||||||
|
},
|
||||||
|
"importNothingError": {
|
||||||
|
"message": "Nothing was imported."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -391,19 +391,13 @@ export class Program {
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('import <format> <filepath> [password]')
|
.command('import [format] [input] [password]')
|
||||||
.description('Import vault data from a file.')
|
.description('Import vault data from a file.')
|
||||||
.option('-l, --list-formats', 'List valid formats')
|
.option('--formats', 'List formats')
|
||||||
.on('option:list-formats', async () => {
|
|
||||||
const command = new ImportCommand(this.main.cryptoService,
|
|
||||||
this.main.userService, this.main.importService);
|
|
||||||
const response = await command.list();
|
|
||||||
this.processResponse(response);
|
|
||||||
})
|
|
||||||
.on('--help', () => {
|
.on('--help', () => {
|
||||||
writeLn('\n Examples:');
|
writeLn('\n Examples:');
|
||||||
writeLn('');
|
writeLn('');
|
||||||
writeLn(' bw import --list-formats');
|
writeLn(' bw import --formats');
|
||||||
writeLn(' bw import bitwardencsv ./from/source.csv');
|
writeLn(' bw import bitwardencsv ./from/source.csv');
|
||||||
writeLn(' bw import keepass2xml keepass_backup.xml myPassword123');
|
writeLn(' bw import keepass2xml keepass_backup.xml myPassword123');
|
||||||
})
|
})
|
||||||
|
|
|
@ -28,7 +28,7 @@ export class CliUtils {
|
||||||
p = osInput;
|
p = osInput;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
reject('you must specify a path');
|
reject('You must specify a file path.');
|
||||||
}
|
}
|
||||||
fs.readFile(p, 'utf8', (err, data) => {
|
fs.readFile(p, 'utf8', (err, data) => {
|
||||||
if (err != null) {
|
if (err != null) {
|
||||||
|
|
Loading…
Reference in New Issue