snyk-go-plugin
Version:
Snyk CLI Golang plugin
68 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsonParse = exports.buildGraph = exports.buildDepGraphFromImportsAndModules = exports.inspect = void 0;
const lookpath_1 = require("lookpath");
const sub_process_1 = require("./sub-process");
const helpers_1 = require("./helpers");
Object.defineProperty(exports, "jsonParse", { enumerable: true, get: function () { return helpers_1.jsonParse; } });
const pkg_manager_1 = require("./pkg-manager");
const dep_tree_1 = require("./dep-tree");
const dep_graph_1 = require("./dep-graph");
Object.defineProperty(exports, "buildDepGraphFromImportsAndModules", { enumerable: true, get: function () { return dep_graph_1.buildDepGraphFromImportsAndModules; } });
Object.defineProperty(exports, "buildGraph", { enumerable: true, get: function () { return dep_graph_1.buildGraph; } });
const debug = require("./debug");
async function inspect(root, targetFile, options = {}) {
if (options.debug) {
debug.enable();
}
else {
debug.disable();
}
const hasGoBinary = Boolean(await (0, lookpath_1.lookpath)('go'));
if (!hasGoBinary) {
throw new Error('The "go" command is not available on your system. To scan your dependencies in the CLI, you must ensure you have first installed the relevant package manager.');
}
const [metadata, deps] = await Promise.all([
getMetadata(root, targetFile),
getDependencies(root, targetFile, options),
]);
if (deps.dependencyGraph) {
return {
plugin: metadata,
dependencyGraph: deps.dependencyGraph,
};
}
// TODO @boost: get rid of the rest of depTree and fully convert this plugin to use depGraph
if (deps.dependencyTree) {
return {
plugin: metadata,
package: deps.dependencyTree,
};
}
// TODO @boost: remove me
throw new Error('Failed to scan this go project.');
}
exports.inspect = inspect;
async function getMetadata(root, targetFile) {
const output = await (0, sub_process_1.execute)('go', ['version'], { cwd: root });
const versionMatch = /(go\d+\.?\d+?\.?\d*)/.exec(output);
const runtime = versionMatch ? versionMatch[0] : undefined;
return {
name: 'snyk-go-plugin',
runtime,
targetFile: (0, helpers_1.pathToPosix)(targetFile),
};
}
async function getDependencies(root, targetFile, options = {}) {
switch ((0, pkg_manager_1.pkgManagerByTarget)(targetFile)) {
case 'gomodules':
return {
dependencyGraph: await (0, dep_graph_1.getDepGraph)(root, targetFile, options),
};
default:
return {
dependencyTree: await (0, dep_tree_1.getDepTree)(root, targetFile),
};
}
}
//# sourceMappingURL=index.js.map