Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
refactor: minor tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Jun 18, 2022
1 parent e8866b2 commit 5e4bc83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions finding_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

// This example demonstrates how to fetch all findings for a given project.
func Example_getAllFindings() {
func Example_fetchAllFindings() {
client, _ := dtrack.NewClient("https://dtrack.example.com", dtrack.WithAPIKey("..."))
projectUUID := uuid.MustParse("2d16089e-6d3a-437e-b334-f27eb2cbd7f4")

_, err := dtrack.FetchAll[dtrack.Finding](func(po dtrack.PageOptions) (dtrack.Page[dtrack.Finding], error) {
_, err := dtrack.FetchAll(func(po dtrack.PageOptions) (dtrack.Page[dtrack.Finding], error) {
return client.Finding.GetAll(context.TODO(), projectUUID, false, po)
})
if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions notification/notification_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/nscuro/dtrack-client/notification"
)

func ExampleParse() {
// This example demonstrates how to parse and process notifications.
func Example_parse() {
file, err := os.Open("./testdata/new-vulnerability.json")
if err != nil {
panic(err)
Expand All @@ -19,13 +20,17 @@ func ExampleParse() {
panic(err)
}

subject, ok := n.Subject.(*notification.NewVulnerabilitySubject)
if !ok {
panic("unexpected subject type")
switch subject := n.Subject.(type) {
case *notification.NewVulnerabilitySubject:
fmt.Printf("new vulnerability identified: %s\n", subject.Vulnerability.VulnID)
for _, project := range subject.AffectedProjects {
fmt.Printf("=> Project: %s %s\n", project.Name, project.Version)
fmt.Printf(" Component: %s %s\n", subject.Component.Name, subject.Component.Version)
}
}

fmt.Println(subject.Vulnerability.VulnID)

// Output:
// CVE-2012-5784
// new vulnerability identified: CVE-2012-5784
// => Project: Acme Example 1.0.0
// Component: axis 1.4
}
11 changes: 7 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package dtrack
// FetchAll is a convenience function to retrieve all items of a paginated API resource.
func FetchAll[T any](f func(po PageOptions) (Page[T], error)) (items []T, err error) {
const pageSize = 50
pageNumber := 1

var (
page Page[T]
pageNumber = 1
)

for {
page, fErr := f(PageOptions{
page, err = f(PageOptions{
PageNumber: pageNumber,
PageSize: pageSize,
})
if fErr != nil {
err = fErr
if err != nil {
break
}

Expand Down

0 comments on commit 5e4bc83

Please sign in to comment.