mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
included additional support for older npm spec.
Signed-off-by: Toure <tdunnon@gmail.com>
This commit is contained in:
parent
94e448a818
commit
48c7dee9da
@ -37,16 +37,15 @@ type Author struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type" mapstructure:"type"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url" mapstructure:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// match example: "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)"
|
// match example: "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)"
|
||||||
// ---> name: "Isaac Z. Schlueter" email: "i@izs.me" url: "http://blog.izs.me"
|
// ---> name: "Isaac Z. Schlueter" email: "i@izs.me" url: "http://blog.izs.me"
|
||||||
var authorPattern = regexp.MustCompile(`^\s*(?P<name>[^<(]*)(\s+<(?P<email>.*)>)?(\s\((?P<url>.*)\))?\s*$`)
|
var authorPattern = regexp.MustCompile(`^\s*(?P<name>[^<(]*)(\s+<(?P<email>.*)>)?(\s\((?P<url>.*)\))?\s*$`)
|
||||||
|
|
||||||
// This method implements the UnmarshalJSON interface to help normalize
|
// Exports Author.UnmarshalJSON interface to help normalize the json structure.
|
||||||
// the json structure.
|
|
||||||
func (a *Author) UnmarshalJSON(b []byte) error {
|
func (a *Author) UnmarshalJSON(b []byte) error {
|
||||||
var authorStr string
|
var authorStr string
|
||||||
var fields map[string]string
|
var fields map[string]string
|
||||||
@ -73,7 +72,7 @@ func (a *Author) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Author) String() string {
|
func (a *Author) AuthorString() string {
|
||||||
result := a.Name
|
result := a.Name
|
||||||
if a.Email != "" {
|
if a.Email != "" {
|
||||||
result += fmt.Sprintf(" <%s>", a.Email)
|
result += fmt.Sprintf(" <%s>", a.Email)
|
||||||
@ -84,6 +83,30 @@ func (a *Author) String() string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repository) UnmarshalJSON(b []byte) error {
|
||||||
|
var repositoryStr string
|
||||||
|
var fields map[string]string
|
||||||
|
var repository Repository
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, &repositoryStr); err != nil {
|
||||||
|
// string parsing did not work, assume a map was given
|
||||||
|
// for more information: https://docs.npmjs.com/files/package.json#people-fields-author-contributors
|
||||||
|
if err := json.Unmarshal(b, &fields); err != nil {
|
||||||
|
return fmt.Errorf("unable to parse package.json author: %w", err)
|
||||||
|
}
|
||||||
|
// translate the map into a structure
|
||||||
|
if err := mapstructure.Decode(fields, &repository); err != nil {
|
||||||
|
return fmt.Errorf("unable to decode package.json author: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
*r = repository
|
||||||
|
} else {
|
||||||
|
r.URL = repositoryStr
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// parsePackageJson parses a package.json and returns the discovered JavaScript packages.
|
// parsePackageJson parses a package.json and returns the discovered JavaScript packages.
|
||||||
func parsePackageJSON(_ string, reader io.Reader) ([]pkg.Package, error) {
|
func parsePackageJSON(_ string, reader io.Reader) ([]pkg.Package, error) {
|
||||||
packages := make([]pkg.Package, 0)
|
packages := make([]pkg.Package, 0)
|
||||||
@ -104,7 +127,7 @@ func parsePackageJSON(_ string, reader io.Reader) ([]pkg.Package, error) {
|
|||||||
Language: pkg.JavaScript,
|
Language: pkg.JavaScript,
|
||||||
Type: pkg.NpmPkg,
|
Type: pkg.NpmPkg,
|
||||||
Metadata: pkg.NpmMetadata{
|
Metadata: pkg.NpmMetadata{
|
||||||
Author: p.Author.String(),
|
Author: p.Author.AuthorString(),
|
||||||
Homepage: p.Homepage,
|
Homepage: p.Homepage,
|
||||||
URL: p.Repository.URL,
|
URL: p.Repository.URL,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -43,6 +43,21 @@ func TestParsePackageJSON(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Fixture: "test-fixtures/pkg-json/package-repo-string.json",
|
||||||
|
ExpectedPkg: pkg.Package{
|
||||||
|
Name: "function-bind",
|
||||||
|
Version: "1.1.1",
|
||||||
|
Type: pkg.NpmPkg,
|
||||||
|
Licenses: []string{"MIT"},
|
||||||
|
Language: pkg.JavaScript,
|
||||||
|
Metadata: pkg.NpmMetadata{
|
||||||
|
Author: "Raynos <raynos2@gmail.com>",
|
||||||
|
Homepage: "https://github.com/Raynos/function-bind",
|
||||||
|
URL: "git://github.com/Raynos/function-bind.git",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
@ -0,0 +1,66 @@
|
|||||||
|
{
|
||||||
|
"name": "function-bind",
|
||||||
|
"version": "1.1.1",
|
||||||
|
"description": "Implementation of Function.prototype.bind",
|
||||||
|
"keywords": [
|
||||||
|
"function",
|
||||||
|
"bind",
|
||||||
|
"shim",
|
||||||
|
"es5"
|
||||||
|
],
|
||||||
|
"author": "Raynos <raynos2@gmail.com>",
|
||||||
|
"repository": "git://github.com/Raynos/function-bind.git",
|
||||||
|
"main": "index",
|
||||||
|
"homepage": "https://github.com/Raynos/function-bind",
|
||||||
|
"contributors": [
|
||||||
|
{
|
||||||
|
"name": "Raynos"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Jordan Harband",
|
||||||
|
"url": "https://github.com/ljharb"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/Raynos/function-bind/issues",
|
||||||
|
"email": "raynos2@gmail.com"
|
||||||
|
},
|
||||||
|
"dependencies": {},
|
||||||
|
"devDependencies": {
|
||||||
|
"@ljharb/eslint-config": "^12.2.1",
|
||||||
|
"covert": "^1.1.0",
|
||||||
|
"eslint": "^4.5.0",
|
||||||
|
"jscs": "^3.0.7",
|
||||||
|
"tape": "^4.8.0"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"pretest": "npm run lint",
|
||||||
|
"test": "npm run tests-only",
|
||||||
|
"posttest": "npm run coverage -- --quiet",
|
||||||
|
"tests-only": "node test",
|
||||||
|
"coverage": "covert test/*.js",
|
||||||
|
"lint": "npm run jscs && npm run eslint",
|
||||||
|
"jscs": "jscs *.js */*.js",
|
||||||
|
"eslint": "eslint *.js */*.js"
|
||||||
|
},
|
||||||
|
"testling": {
|
||||||
|
"files": "test/index.js",
|
||||||
|
"browsers": [
|
||||||
|
"ie/8..latest",
|
||||||
|
"firefox/16..latest",
|
||||||
|
"firefox/nightly",
|
||||||
|
"chrome/22..latest",
|
||||||
|
"chrome/canary",
|
||||||
|
"opera/12..latest",
|
||||||
|
"opera/next",
|
||||||
|
"safari/5.1..latest",
|
||||||
|
"ipad/6.0..latest",
|
||||||
|
"iphone/6.0..latest",
|
||||||
|
"android-browser/4.2..latest"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
,"_resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"
|
||||||
|
,"_integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
|
||||||
|
,"_from": "function-bind@1.1.1"
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user