Skip to content

Commit

Permalink
Merge pull request #325 from snyk-tech-services/feat/RefactorListImpo…
Browse files Browse the repository at this point in the history
…rtedCmd

feat: refactor list:imported command
  • Loading branch information
mathild3r authored May 18, 2022
2 parents 494ff57 + bd1c9c7 commit de40b4a
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/cmds/list:imported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import * as debugLib from 'debug';
import * as _ from 'lodash';
import * as yargs from 'yargs';
import { getLoggingPath } from '../lib/get-logging-path';
import { SupportedIntegrationTypesToListSnykTargets } from '../lib/types';
import {
CommandResult,
SupportedIntegrationTypesToListSnykTargets,
} from '../lib/types';
const debug = debugLib('snyk:generate-data-script');

import { generateSnykImportedTargets } from '../scripts/generate-imported-targets-from-snyk';
Expand Down Expand Up @@ -44,26 +47,25 @@ const entityName: {
'bitbucket-server': 'repo',
};

export async function handler(argv: {
groupId?: string;
orgId?: string;
integrationType: SupportedIntegrationTypesToListSnykTargets;
}): Promise<void> {
getLoggingPath();
const { groupId, integrationType, orgId } = argv;
export async function createListImported(
integrationType: SupportedIntegrationTypesToListSnykTargets,
groupId?: string,
orgId?: string,
): Promise<CommandResult> {
try {

if (!(groupId || orgId)) {
throw new Error(
'Missing required parameters: orgId or groupId must be provided.',
);
}

if (groupId && orgId) {
throw new Error(
'Too many parameters: orgId or groupId must be provided, not both.',
);
}
debug('ℹ️ Options: ' + JSON.stringify(argv));

const integrationTypes = _.castArray(integrationType);
const { targets, fileName, failedOrgs } = await generateSnykImportedTargets(
{ groupId, orgId },
Expand All @@ -87,12 +89,43 @@ export async function handler(argv: {
.join(',')}`,
);
}
console.log(targetsMessage);

return {
fileName: fileName,
exitCode: 0,
message: targetsMessage,
};

} catch (e) {
const errorMessage = `ERROR! Failed to list imported targets in Snyk. Try running with \`DEBUG=snyk* <command> for more info\`.\nERROR: ${e.message}`;

return {
fileName: undefined,
exitCode: 1,
message: errorMessage,
};
}
}

export async function handler(argv: {
groupId?: string;
orgId?: string;
integrationType: SupportedIntegrationTypesToListSnykTargets;
}): Promise<void> {
getLoggingPath();
const { groupId, integrationType, orgId } = argv;

debug('ℹ️ Options: ' + JSON.stringify(argv));

const res = await createListImported(integrationType, groupId, orgId)

if (res.exitCode === 1) {
debug('Failed to create organizations.\n' + res.message);

debug('Failed to list all imported targets in Snyk.\n' + e);
console.error(errorMessage);
yargs.exit(1, new Error(errorMessage));
console.error(res.message);
setTimeout(() => yargs.exit(1, new Error(res.message)), 3000);
} else {
console.log(res.message);
}

}

0 comments on commit de40b4a

Please sign in to comment.