From 5e4bc83e61fef28176b6aaac6784fcf7f4316527 Mon Sep 17 00:00:00 2001 From: nscuro Date: Sat, 18 Jun 2022 14:41:34 +0200 Subject: [PATCH] refactor: minor tweaks Signed-off-by: nscuro --- finding_example_test.go | 4 ++-- notification/notification_example_test.go | 19 ++++++++++++------- util.go | 11 +++++++---- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/finding_example_test.go b/finding_example_test.go index 5744bda..a0c7a61 100644 --- a/finding_example_test.go +++ b/finding_example_test.go @@ -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 { diff --git a/notification/notification_example_test.go b/notification/notification_example_test.go index 23ca013..5ed6162 100644 --- a/notification/notification_example_test.go +++ b/notification/notification_example_test.go @@ -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) @@ -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 } diff --git a/util.go b/util.go index 794c5ae..23c7556 100644 --- a/util.go +++ b/util.go @@ -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 }