forked from anchore/syft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port php cataloger to new generic cataloger pattern (anchore#1315)
Signed-off-by: Alex Goodman <alex.goodman@anchore.com> Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
- Loading branch information
Showing
8 changed files
with
263 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package php | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/anchore/packageurl-go" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/source" | ||
) | ||
|
||
func newComposerLockPackage(m pkg.PhpComposerJSONMetadata, location ...source.Location) pkg.Package { | ||
p := pkg.Package{ | ||
Name: m.Name, | ||
Version: m.Version, | ||
Locations: source.NewLocationSet(location...), | ||
PURL: packageURL(m), | ||
Language: pkg.PHP, | ||
Type: pkg.PhpComposerPkg, | ||
MetadataType: pkg.PhpComposerJSONMetadataType, | ||
Metadata: m, | ||
} | ||
|
||
p.SetID() | ||
return p | ||
} | ||
|
||
func packageURL(m pkg.PhpComposerJSONMetadata) string { | ||
var name, vendor string | ||
fields := strings.Split(m.Name, "/") | ||
switch len(fields) { | ||
case 0: | ||
return "" | ||
case 1: | ||
name = m.Name | ||
case 2: | ||
vendor = fields[0] | ||
name = fields[1] | ||
default: | ||
vendor = fields[0] | ||
name = strings.Join(fields[1:], "-") | ||
} | ||
|
||
pURL := packageurl.NewPackageURL( | ||
packageurl.TypeComposer, | ||
vendor, | ||
name, | ||
m.Version, | ||
nil, | ||
"") | ||
return pURL.ToString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package php | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sergi/go-diff/diffmatchpatch" | ||
|
||
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
func Test_packageURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metadata pkg.PhpComposerJSONMetadata | ||
expected string | ||
}{ | ||
{ | ||
name: "with extractable vendor", | ||
metadata: pkg.PhpComposerJSONMetadata{ | ||
Name: "ven/name", | ||
Version: "1.0.1", | ||
}, | ||
expected: "pkg:composer/ven/name@1.0.1", | ||
}, | ||
{ | ||
name: "name with slashes (invalid)", | ||
metadata: pkg.PhpComposerJSONMetadata{ | ||
Name: "ven/name/component", | ||
Version: "1.0.1", | ||
}, | ||
expected: "pkg:composer/ven/name-component@1.0.1", | ||
}, | ||
{ | ||
name: "unknown vendor", | ||
metadata: pkg.PhpComposerJSONMetadata{ | ||
Name: "name", | ||
Version: "1.0.1", | ||
}, | ||
expected: "pkg:composer/name@1.0.1", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
actual := packageURL(test.metadata) | ||
if actual != test.expected { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(test.expected, actual, true) | ||
t.Errorf("diff: %s", dmp.DiffPrettyText(diffs)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.