refactor(core): Delete duplicate code across all commands (#5452)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-02-10 14:59:20 +01:00
committed by GitHub
parent 8494c97821
commit 5194513850
26 changed files with 979 additions and 1345 deletions

View File

@@ -1,21 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable no-console */
import { Command, flags } from '@oclif/command';
import { Credentials, UserSettings } from 'n8n-core';
import type { IDataObject } from 'n8n-workflow';
import { LoggerProxy } from 'n8n-workflow';
import { flags } from '@oclif/command';
import fs from 'fs';
import path from 'path';
import { getLogger } from '@/Logger';
import { Credentials, UserSettings } from 'n8n-core';
import type { IDataObject } from 'n8n-workflow';
import * as Db from '@/Db';
import type { ICredentialsDecryptedDb } from '@/Interfaces';
import { BaseCommand } from '../BaseCommand';
export class ExportCredentialsCommand extends Command {
export class ExportCredentialsCommand extends BaseCommand {
static description = 'Export credentials';
static examples = [
@@ -55,11 +47,7 @@ export class ExportCredentialsCommand extends Command {
}),
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async run() {
const logger = getLogger();
LoggerProxy.init(logger);
// eslint-disable-next-line @typescript-eslint/no-shadow
const { flags } = this.parse(ExportCredentialsCommand);
@@ -70,104 +58,103 @@ export class ExportCredentialsCommand extends Command {
}
if (!flags.all && !flags.id) {
console.info('Either option "--all" or "--id" have to be set!');
this.logger.info('Either option "--all" or "--id" have to be set!');
return;
}
if (flags.all && flags.id) {
console.info('You should either use "--all" or "--id" but never both!');
this.logger.info('You should either use "--all" or "--id" but never both!');
return;
}
if (flags.separate) {
try {
if (!flags.output) {
console.info('You must inform an output directory via --output when using --separate');
this.logger.info(
'You must inform an output directory via --output when using --separate',
);
return;
}
if (fs.existsSync(flags.output)) {
if (!fs.lstatSync(flags.output).isDirectory()) {
console.info('The parameter --output must be a directory');
this.logger.info('The parameter --output must be a directory');
return;
}
} else {
fs.mkdirSync(flags.output, { recursive: true });
}
} catch (e) {
console.error(
this.logger.error(
'Aborting execution as a filesystem error has been encountered while creating the output directory. See log messages for details.',
);
logger.error('\nFILESYSTEM ERROR');
logger.info('====================================');
logger.error(e.message);
logger.error(e.stack);
this.exit(1);
this.logger.error('\nFILESYSTEM ERROR');
if (e instanceof Error) {
this.logger.info('====================================');
this.logger.error(e.message);
this.logger.error(e.stack!);
}
return;
}
} else if (flags.output) {
if (fs.existsSync(flags.output)) {
if (fs.lstatSync(flags.output).isDirectory()) {
console.info('The parameter --output must be a writeable file');
this.logger.info('The parameter --output must be a writeable file');
return;
}
}
}
try {
await Db.init();
const findQuery: IDataObject = {};
if (flags.id) {
findQuery.id = flags.id;
}
const findQuery: IDataObject = {};
if (flags.id) {
findQuery.id = flags.id;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const credentials = await Db.collections.Credentials.find(findQuery);
if (flags.decrypted) {
const encryptionKey = await UserSettings.getEncryptionKey();
for (let i = 0; i < credentials.length; i++) {
const { name, type, nodesAccess, data } = credentials[i];
const id = credentials[i].id;
const credential = new Credentials({ id, name }, type, nodesAccess, data);
const plainData = credential.getData(encryptionKey);
(credentials[i] as ICredentialsDecryptedDb).data = plainData;
}
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const credentials = await Db.collections.Credentials.find(findQuery);
if (credentials.length === 0) {
throw new Error('No credentials found with specified filters.');
}
if (flags.decrypted) {
const encryptionKey = await UserSettings.getEncryptionKey();
for (let i = 0; i < credentials.length; i++) {
const { name, type, nodesAccess, data } = credentials[i];
const id = credentials[i].id;
const credential = new Credentials({ id, name }, type, nodesAccess, data);
const plainData = credential.getData(encryptionKey);
(credentials[i] as ICredentialsDecryptedDb).data = plainData;
}
if (flags.separate) {
let fileContents: string;
let i: number;
for (i = 0; i < credentials.length; i++) {
fileContents = JSON.stringify(credentials[i], null, flags.pretty ? 2 : undefined);
const filename = `${
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/restrict-plus-operands
(flags.output!.endsWith(path.sep) ? flags.output! : flags.output + path.sep) +
credentials[i].id
}.json`;
fs.writeFileSync(filename, fileContents);
}
if (credentials.length === 0) {
throw new Error('No credentials found with specified filters.');
}
if (flags.separate) {
let fileContents: string;
let i: number;
for (i = 0; i < credentials.length; i++) {
fileContents = JSON.stringify(credentials[i], null, flags.pretty ? 2 : undefined);
const filename = `${
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(flags.output!.endsWith(path.sep) ? flags.output! : flags.output + path.sep) +
credentials[i].id
}.json`;
fs.writeFileSync(filename, fileContents);
}
console.info(`Successfully exported ${i} credentials.`);
this.logger.info(`Successfully exported ${i} credentials.`);
} else {
const fileContents = JSON.stringify(credentials, null, flags.pretty ? 2 : undefined);
if (flags.output) {
fs.writeFileSync(flags.output, fileContents);
this.logger.info(`Successfully exported ${credentials.length} credentials.`);
} else {
const fileContents = JSON.stringify(credentials, null, flags.pretty ? 2 : undefined);
if (flags.output) {
fs.writeFileSync(flags.output, fileContents);
console.info(`Successfully exported ${credentials.length} credentials.`);
} else {
console.info(fileContents);
}
this.logger.info(fileContents);
}
// Force exit as process won't exit using MySQL or Postgres.
process.exit(0);
} catch (error) {
console.error('Error exporting credentials. See log messages for details.');
logger.error(error.message);
this.exit(1);
}
}
async catch(error: Error) {
this.logger.error('Error exporting credentials. See log messages for details.');
this.logger.error(error.message);
}
}