2018-08-06 15:38:17 +02:00
|
|
|
import * as program from 'commander';
|
2021-06-07 19:25:55 +02:00
|
|
|
import { ImportService } from 'jslib-common/abstractions/import.service';
|
2021-06-16 15:50:29 +02:00
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
2018-08-06 15:38:17 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Response } from 'jslib-node/cli/models/response';
|
|
|
|
import { MessageResponse } from 'jslib-node/cli/models/response/messageResponse';
|
2018-08-06 15:38:17 +02:00
|
|
|
|
|
|
|
import { CliUtils } from '../utils';
|
|
|
|
|
|
|
|
export class ImportCommand {
|
2021-06-16 15:50:29 +02:00
|
|
|
constructor(private importService: ImportService, private userService: UserService) { }
|
2018-08-06 15:38:17 +02:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async run(format: string, filepath: string, options: program.OptionValues): Promise<Response> {
|
2021-06-16 15:50:29 +02:00
|
|
|
const organizationId = options.organizationid;
|
|
|
|
if (organizationId != null) {
|
|
|
|
const organization = await this.userService.getOrganization(organizationId);
|
|
|
|
|
|
|
|
if (organization == null) {
|
|
|
|
return Response.badRequest(`You do not belong to an organization with the ID of ${organizationId}. Check the organization ID and sync your vault.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!organization.canAccessImportExport) {
|
|
|
|
return Response.badRequest('You are not authorized to import into the provided organization.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
if (options.formats || false) {
|
2021-06-16 15:50:29 +02:00
|
|
|
return await this.list();
|
2018-08-06 16:38:32 +02:00
|
|
|
} else {
|
2021-06-16 15:50:29 +02:00
|
|
|
return await this.import(format, filepath, organizationId);
|
2018-08-06 16:38:32 +02:00
|
|
|
}
|
2018-08-06 15:38:17 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 15:50:29 +02:00
|
|
|
private async import(format: string, filepath: string, organizationId: string) {
|
2018-08-06 16:38:32 +02:00
|
|
|
if (format == null || format === '') {
|
|
|
|
return Response.badRequest('`format` was not provided.');
|
|
|
|
}
|
|
|
|
if (filepath == null || filepath === '') {
|
|
|
|
return Response.badRequest('`filepath` was not provided.');
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:50:29 +02:00
|
|
|
const importer = await this.importService.getImporter(format, organizationId);
|
2018-08-06 15:38:17 +02:00
|
|
|
if (importer === null) {
|
|
|
|
return Response.badRequest('Proper importer type required.');
|
|
|
|
}
|
2018-08-06 16:38:32 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const contents = await CliUtils.readFile(filepath);
|
|
|
|
if (contents === null || contents === '') {
|
|
|
|
return Response.badRequest('Import file was empty.');
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:50:29 +02:00
|
|
|
const err = await this.importService.import(importer, contents, organizationId);
|
2018-10-11 22:46:04 +02:00
|
|
|
if (err != null) {
|
2018-08-06 17:43:07 +02:00
|
|
|
return Response.badRequest(err.message);
|
2018-08-06 16:38:32 +02:00
|
|
|
}
|
2018-08-06 17:43:07 +02:00
|
|
|
const res = new MessageResponse('Imported ' + filepath, null);
|
|
|
|
return Response.success(res);
|
2018-08-06 16:38:32 +02:00
|
|
|
} catch (err) {
|
|
|
|
return Response.badRequest(err);
|
2018-08-06 15:38:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 16:38:32 +02:00
|
|
|
|
|
|
|
private async list() {
|
2018-08-06 17:43:07 +02:00
|
|
|
const options = this.importService.getImportOptions().sort((a, b) => {
|
2018-08-06 16:38:32 +02:00
|
|
|
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
2021-02-04 05:51:59 +01:00
|
|
|
}).map(option => option.id).join('\n');
|
2018-08-06 16:38:32 +02:00
|
|
|
const res = new MessageResponse('Supported input formats:', options);
|
|
|
|
res.raw = options;
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
2018-08-06 15:38:17 +02:00
|
|
|
}
|