Skip to content

Commit

Permalink
feat: enhance debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Sep 28, 2020
1 parent 3da3f9f commit 4caa901
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/cmds/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export const builder = {
outputFormat: {
default: OutputFormat.HTML,
desc: 'Report format',
// TODO: add also PDF when ready
options: [OutputFormat.HTML]
},
view: {
// TODO: add also dependency based view when ready
default: SupportedViews.ORG_LICENSES,
desc: 'How should the data be represented. Defaults to a license based view.',
},
Expand All @@ -47,7 +49,7 @@ export async function handler(argv: {
try {
const { orgPublicId, outputFormat, template, view } = argv;
debug(
'Options: ' +
'ℹ️ Options: ' +
JSON.stringify({ orgPublicId, outputFormat, template, view }),
);
getApiToken();
Expand Down
20 changes: 18 additions & 2 deletions src/cmds/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as debugLib from 'debug';

import { getApiToken } from '../lib/get-api-token';
import { generateLicenseData } from '../lib/generate-org-license-report';
import { SupportedViews } from '../lib/generate-output';

const debug = debugLib('snyk-licenses:json');

export const command = 'json';
export const desc = 'Generate org licenses & dependencies data in JSON format';
Expand All @@ -8,16 +13,27 @@ export const builder = {
required: true,
default: undefined,
},
view: {
// TODO: add also dependency based view when ready
default: SupportedViews.ORG_LICENSES,
desc:
'How should the data be represented. Defaults to a license based view.',
},
};
export const aliases = ['j'];

export async function handler(argv: { orgPublicId: string }) {
export async function handler(argv: {
orgPublicId: string;
view: SupportedViews;
}) {
try {
const { orgPublicId, view } = argv;
debug('ℹ️ Options: ' + JSON.stringify({ orgPublicId, view }));
// check SNYK_TOKEN is set as the sdk uses it
getApiToken();
// TODO: define and pass options to help filter the response
// based on filters available in API
const data = await generateLicenseData(argv.orgPublicId, {});
const data = await generateLicenseData(orgPublicId, {});
console.log(JSON.stringify(data));
} catch (e) {
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/org/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function getDependenciesDataForOrg(
);
return dependenciesData;
} catch (e) {
debug('Failed to fetch dependencies' + e);
debug('Failed to fetch dependencies' + e);
throw e;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/org/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getLicenseDataForOrg(
const licenseData = await getAllLicensesData(snykApiClient, body, sortBy, order);
return licenseData;
} catch (e) {
debug('Failed to fetch licenses' + e);
debug('Failed to fetch licenses' + e);
throw e;
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/lib/generate-org-license-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ export async function generateLicenseData(
orgPublicId: string,
options?,
): Promise<LicenseReportData> {
debug(`Generating license data for Org:${orgPublicId}`);
debug(`ℹ️ Generating license data for Org:${orgPublicId}`);

try {
const licenseData = await getLicenseDataForOrg(orgPublicId, options);
debug(`Got license API data for Org:${orgPublicId}`);
debug(`Got license API data for Org:${orgPublicId}`);
const dependenciesDataRaw = await getDependenciesDataForOrg(
orgPublicId,
options,
);
debug(`Got dependencies API data for Org:${orgPublicId}`);
debug(`Got dependencies API data for Org:${orgPublicId}`);
const licenseReportData: LicenseReportData = {};
const dependenciesData = _.groupBy(dependenciesDataRaw.results, 'id');
// TODO: what if 0?
debug(`Processing ${licenseData.total} licenses`);
debug(`Processing ${licenseData.total} licenses`);

const dependenciesAll = [];
for (const license of licenseData.results) {
Expand All @@ -55,9 +55,11 @@ export async function generateLicenseData(
licenseUrl: licenseData?.licenseUrl,
};
}
debug(`✅ Done processing ${licenseData.total} licenses`);

return licenseReportData;
} catch (e) {
debug('Failed to generate report data', e);
debug('Failed to generate report data', e);
throw e;
}
}
Expand Down Expand Up @@ -89,12 +91,12 @@ async function getLicenseTextAndUrl(
try {
return await fetchSpdxLicenseTextAndUrl(id);
} catch (e) {
debug(`Failed to get license data for as SPDX, trying non-SPDX: ${id}`);
debug(`Failed to get license data for as SPDX, trying non-SPDX: ${id}`);
}
try {
return await fetchNonSpdxLicenseTextAndUrl(id);
} catch (e) {
debug(`Failed to get license data as non-SPDX: ${id}`);
debug(`Failed to get license data as non-SPDX: ${id}`);
}

return undefined;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/generate-output/html/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as Handlebars from 'handlebars';
import * as path from 'path';
import * as fs from 'fs';
import * as debugLib from 'debug';

import { LicenseReportData } from '../../generate-org-license-report';

const debug = debugLib('snyk-licenses:generateHtmlReport');

export const enum SupportedViews {
ORG_LICENSES = 'org-licenses',
// TODO: support later
Expand Down
4 changes: 2 additions & 2 deletions test/system/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ describe('`snyk-licenses-report json <...>`', () => {
});
});
it('Generated JSON data with correct --orgPublicId', async (done) => {
exec(`node ${main} json --orgPublicId=${ORG_ID}`, (err, stdout) => {
exec(`DEBUG=snyk-license* node ${main} json --orgPublicId=${ORG_ID}`, (err, stdout) => {
expect(err).toBeNull();
console.log({err, stdout})
console.log({err, stdout, ORG_ID})
expect(stdout.trim()).toMatch("BSD-2-Clause");
done();
});
Expand Down

0 comments on commit 4caa901

Please sign in to comment.