fetch.go (1027B)
1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package vulncheck 6 7 import ( 8 "context" 9 "fmt" 10 11 "golang.org/x/tools/go/packages" 12 "golang.org/x/vuln/internal/client" 13 ) 14 15 // FetchVulnerabilities fetches vulnerabilities that affect the supplied modules. 16 func FetchVulnerabilities(ctx context.Context, c *client.Client, modules []*packages.Module) ([]*ModVulns, error) { 17 mreqs := make([]*client.ModuleRequest, len(modules)) 18 for i, mod := range modules { 19 modPath := mod.Path 20 if mod.Replace != nil { 21 modPath = mod.Replace.Path 22 } 23 mreqs[i] = &client.ModuleRequest{ 24 Path: modPath, 25 } 26 } 27 resps, err := c.ByModules(ctx, mreqs) 28 if err != nil { 29 return nil, fmt.Errorf("fetching vulnerabilities: %v", err) 30 } 31 var mv []*ModVulns 32 for i, resp := range resps { 33 if len(resp.Entries) == 0 { 34 continue 35 } 36 mv = append(mv, &ModVulns{ 37 Module: modules[i], 38 Vulns: resp.Entries, 39 }) 40 } 41 return mv, nil 42 }