Skip to content

Commit

Permalink
refactor javascript cataloger to use configuration options when creat…
Browse files Browse the repository at this point in the history
…ing packages (anchore#2438)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman authored Dec 15, 2023
1 parent eb31efd commit 42cbd08
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions syft/pkg/cataloger/javascript/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/javascript/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/javascript/parse_yarn_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 42cbd08

Please sign in to comment.