import path from 'path' import { PackageURL } from 'packageurl-js' export function parseGoPackage (pkg: string): PackageURL { const [qualifiedPackage, version] = pkg.split('@') let namespace: string | null = null let name: string if (qualifiedPackage.indexOf('/') !== -1) { namespace = path.dirname(qualifiedPackage) name = path.basename(qualifiedPackage) } else { name = qualifiedPackage } return new PackageURL('golang', namespace, name, version ?? null, null, null) } /** * parseGoList parses a list of Go packages (one per line) matching the format * "${GO_PACKAGE}@v{VERSION}" into Package URLs. This expects the output of 'go * list -deps' as input. * * @param {string} contents * @returns {Array} */ export function parseGoList (contents: string): Array { // split the input by newlines, sort, and dedup const packages: string[] = Array.from( new Set(contents.split('\n').map((p) => p.trim())) ) const purls: Array = [] packages.forEach((pkg: string) => { if (!pkg.trim()) return purls.push(parseGoPackage(pkg)) }) return purls } /** * parseGoModGraph parses an *associative list* of Go packages into tuples into * an associative list of PackageURLs. This expects the output of 'go mod * graph' as input */ export function parseGoModGraph ( contents: string ): Array<[PackageURL, PackageURL]> { const pkgAssocList: Array<[PackageURL, PackageURL]> = [] contents.split('\n').forEach((line) => { if (!line.trim()) return const [parentPkg, childPkg] = line.split(' ') pkgAssocList.push([parseGoPackage(parentPkg), parseGoPackage(childPkg)]) }) return pkgAssocList }