forked from DependencyTrack/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bom_example_test.go
70 lines (61 loc) · 1.39 KB
/
bom_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package dtrack_test
import (
"context"
"encoding/base64"
"fmt"
"os"
"time"
"github.com/futurice/dependency-track-client-go"
)
// This example demonstrates how to upload a Bill of Materials and wait for its processing to complete.
func Example_uploadBOM() {
client, _ := dtrack.NewClient("https://dtrack.example.com", dtrack.WithAPIKey("..."))
bomContent, err := os.ReadFile("bom.xml")
if err != nil {
panic(err)
}
uploadToken, err := client.BOM.Upload(context.TODO(), dtrack.BOMUploadRequest{
ProjectName: "acme-app",
ProjectVersion: "1.0.0",
AutoCreate: true,
BOM: base64.StdEncoding.EncodeToString(bomContent),
})
if err != nil {
panic(err)
}
var (
doneChan = make(chan struct{})
errChan = make(chan error)
ticker = time.NewTicker(1 * time.Second)
timeout = time.After(30 * time.Second)
)
go func() {
defer func() {
close(doneChan)
close(errChan)
}()
for {
select {
case <-ticker.C:
processing, err := client.BOM.IsBeingProcessed(context.TODO(), uploadToken)
if err != nil {
errChan <- err
return
}
if !processing {
doneChan <- struct{}{}
return
}
case <-timeout:
errChan <- fmt.Errorf("timeout exceeded")
return
}
}
}()
select {
case <-doneChan:
fmt.Println("bom processing completed")
case <-errChan:
fmt.Printf("failed to wait for bom processing: %v\n", err)
}
}