-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (72 loc) · 2.26 KB
/
Copy pathmain.go
File metadata and controls
89 lines (72 loc) · 2.26 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/samber/lo"
openlane "github.com/theopenlane/go-client"
"github.com/theopenlane/go-client/graphclient"
)
func main() {
apiToken := os.Getenv("OPENLANE_API_TOKEN")
if apiToken == "" {
log.Fatal("OPENLANE_API_TOKEN environment variable is required")
}
// Initialize the client
client, err := openlane.New(
openlane.WithAPIToken(apiToken),
)
ctx := context.Background()
// Query all controls with pagination
controls, err := getAllControls(client, ctx)
if err != nil {
log.Fatalf("Error fetching controls: %v", err)
}
// Display results
fmt.Printf("Total controls fetched: %d\n\n", len(controls))
for i, control := range controls {
fmt.Printf("%d. [%s] %s\n", i+1, control.RefCode, *control.Title)
fmt.Printf(" Category: %s\n",
*control.Category)
if control.Description != nil {
fmt.Printf(" Description: %s\n", *control.Description)
}
fmt.Println()
}
}
// getAllControls fetches all controls using pagination and returns them as a slice
func getAllControls(client *openlane.Client, ctx context.Context) ([]*graphclient.GetControls_Controls_Edges_Node, error) {
var allControls []*graphclient.GetControls_Controls_Edges_Node
// Set page size
pageSize := int64(10)
where := &graphclient.ControlWhereInput{
ReferenceFramework: lo.ToPtr("SOC 2"),
SystemOwned: lo.ToPtr(true),
}
orderBy := &graphclient.ControlOrder{
Field: graphclient.ControlOrderFieldRefCode,
Direction: graphclient.OrderDirectionAsc,
}
after := (*string)(nil)
// Iterate through all pages
for {
response, err := client.GetControls(ctx, &pageSize, nil, after, nil, where, []*graphclient.ControlOrder{orderBy})
if err != nil {
return nil, fmt.Errorf("failed to fetch controls: %w", err)
}
// Collect results from current page
for _, edge := range response.Controls.Edges {
allControls = append(allControls, edge.Node)
}
fmt.Printf("Fetched page with %d controls (total so far: %d of %d)\n",
len(response.Controls.Edges), len(allControls), response.Controls.TotalCount)
// Check if there are more pages
if !response.Controls.PageInfo.HasNextPage {
break
}
// Update cursor for next page
after = response.Controls.PageInfo.EndCursor
}
return allControls, nil
}