mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
refactor javascript cataloger to use configuration options when creating packages (#2438)
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
parent
05660da8d7
commit
4eace4b141
@ -3,25 +3,25 @@ package javascript
|
||||
const npmBaseURL = "https://registry.npmjs.org"
|
||||
|
||||
type CatalogerConfig struct {
|
||||
searchRemoteLicenses bool
|
||||
npmBaseURL string
|
||||
SearchRemoteLicenses bool `json:"search-remote-licenses" yaml:"search-remote-licenses" mapstructure:"search-remote-licenses"`
|
||||
NPMBaseURL string `json:"npm-base-url" yaml:"npm-base-url" mapstructure:"npm-base-url"`
|
||||
}
|
||||
|
||||
func DefaultCatalogerConfig() CatalogerConfig {
|
||||
return CatalogerConfig{
|
||||
searchRemoteLicenses: false,
|
||||
npmBaseURL: npmBaseURL,
|
||||
SearchRemoteLicenses: false,
|
||||
NPMBaseURL: npmBaseURL,
|
||||
}
|
||||
}
|
||||
|
||||
func (j CatalogerConfig) WithSearchRemoteLicenses(input bool) CatalogerConfig {
|
||||
j.searchRemoteLicenses = input
|
||||
j.SearchRemoteLicenses = input
|
||||
return j
|
||||
}
|
||||
|
||||
func (j CatalogerConfig) WithNpmBaseURL(input string) CatalogerConfig {
|
||||
if input != "" {
|
||||
j.npmBaseURL = input
|
||||
j.NPMBaseURL = input
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
@ -113,8 +113,8 @@ func newPnpmPackage(resolver file.Resolver, location file.Location, name, versio
|
||||
func newYarnLockPackage(cfg CatalogerConfig, resolver file.Resolver, location file.Location, name, version string) pkg.Package {
|
||||
var licenseSet pkg.LicenseSet
|
||||
|
||||
if cfg.searchRemoteLicenses {
|
||||
license, err := getLicenseFromNpmRegistry(cfg.npmBaseURL, name, version)
|
||||
if cfg.SearchRemoteLicenses {
|
||||
license, err := getLicenseFromNpmRegistry(cfg.NPMBaseURL, name, version)
|
||||
if err == nil && license != "" {
|
||||
licenses := pkg.NewLicensesFromValues(license)
|
||||
licenseSet = pkg.NewLicenseSet(licenses...)
|
||||
|
||||
@ -204,7 +204,7 @@ func TestSearchYarnForLicenses(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "search remote licenses returns the expected licenses when search is set to true",
|
||||
config: CatalogerConfig{searchRemoteLicenses: true},
|
||||
config: CatalogerConfig{SearchRemoteLicenses: true},
|
||||
requestHandlers: []handlerPath{
|
||||
{
|
||||
// https://registry.yarnpkg.com/@babel/code-frame/7.10.4
|
||||
@ -232,7 +232,7 @@ func TestSearchYarnForLicenses(t *testing.T) {
|
||||
for _, handler := range tc.requestHandlers {
|
||||
mux.HandleFunc(handler.path, handler.handler)
|
||||
}
|
||||
tc.config.npmBaseURL = url
|
||||
tc.config.NPMBaseURL = url
|
||||
adapter := newGenericYarnLockAdapter(tc.config)
|
||||
pkgtest.TestFileParser(t, fixture, adapter.parseYarnLock, tc.expectedPackages, nil)
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user