UNPKG

snyk-go-plugin

Version:
37 lines 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGoPurl = void 0; const packageurl_js_1 = require("packageurl-js"); const PURL_TYPE_GOLANG = 'golang'; /** * Construct a PackageURL string for a given Go module and import path. * * For a definition of golang type purls, see: * https://github.com/package-url/purl-spec/blob/9041aa74236686b23153652f8cd3862eef8c33a9/types-doc/golang-definition.md */ function createGoPurl(goModule, snykPkg) { let namespace; let name; const version = goModule.Version || (snykPkg === null || snykPkg === void 0 ? void 0 : snykPkg.version); let subpath; // Split the module path into a name and namespace. // If the module path does not include any /, the path is // the purl name. const idx = goModule.Path.lastIndexOf('/'); if (idx >= 0) { namespace = goModule.Path.slice(0, idx); name = goModule.Path.slice(idx + 1); } else { name = goModule.Path; } // If an import path was given, and it contains more parts than the module's path, // we're dealing with a sub-package. This should go under the purl's subpath. if ((snykPkg === null || snykPkg === void 0 ? void 0 : snykPkg.name.startsWith(goModule.Path)) && snykPkg.name.length > goModule.Path.length) { subpath = snykPkg.name.replace(`${goModule.Path}/`, ''); } return new packageurl_js_1.PackageURL(PURL_TYPE_GOLANG, namespace, name, version, null, subpath).toString(); } exports.createGoPurl = createGoPurl; //# sourceMappingURL=package-url.js.map