Skip to content

Commit

Permalink
fix: updated controls output
Browse files Browse the repository at this point in the history
  • Loading branch information
ADorigi committed Sep 15, 2024
1 parent 38fc98c commit 09eb789
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
81 changes: 49 additions & 32 deletions cmd/get/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package get
import (
"encoding/json"
"fmt"
"github.com/adorigi/opengovernance/pkg/output/tables"
"io"
"net/http"

"github.com/adorigi/opengovernance/pkg/output/tables"

"github.com/adorigi/opengovernance/pkg/config"
"github.com/adorigi/opengovernance/pkg/request"
"github.com/adorigi/opengovernance/pkg/types"
Expand Down Expand Up @@ -39,42 +40,58 @@ to quickly create a Cobra application.`,
outputFormat = configuration.OutputFormat
}

requestPayload := types.RequestPayload{
Cursor: 1,
PerPage: int(utils.ReadIntFlag(cmd, "page-size")),
}
var cursor = 1
var controls []types.Control

payload, err := json.Marshal(requestPayload)
if err != nil {
return err
}
for {

request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"POST",
"main/compliance/api/v2/controls",
payload,
)
if err != nil {
return err
}
fmt.Println(cursor)

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
requestPayload := types.RequestPayload{
Cursor: cursor,
PerPage: int(utils.ReadIntFlag(cmd, "page-size")),
}

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
payload, err := json.Marshal(requestPayload)
if err != nil {
return err
}

request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"POST",
"main/compliance/api/v2/controls",
payload,
)
if err != nil {
return err
}

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

var getControlsResponse types.GetControlsResponse
err = json.Unmarshal(body, &getControlsResponse)
if err != nil {
return err
}

if len(getControlsResponse.Items) == 0 {
break
}

controls = append(controls, getControlsResponse.Items...)
cursor += 1

var controls []types.Control
err = json.Unmarshal(body, &controls)
if err != nil {
return err
}

if outputFormat == "table" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type Control struct {
FindingsSummary *string `json:"findings_summary"`
}

type GetControlsResponse struct {
Items []Control `json:"items"`
TotalCount int64 `json:"total_count"`
}

type RequestPayload struct {
Cursor int `json:"cursor"`
PerPage int `json:"per_page"`
Expand Down

0 comments on commit 09eb789

Please sign in to comment.