snyk-go-plugin
Version:
Snyk CLI Golang plugin
37 lines • 1.42 kB
JavaScript
;
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, importPath) {
let namespace;
let name;
const version = goModule.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 is not exactly matching the module's
// path, we're dealing with a sub-package. This should go under the purl's
// subpath.
if (importPath && importPath !== goModule.Path) {
subpath = importPath.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