From 09eb78941f4ec8b8b4b6903be696bc67a12778bf Mon Sep 17 00:00:00 2001 From: Adnan Gulegulzar Date: Sun, 15 Sep 2024 12:35:02 -0400 Subject: [PATCH] fix: updated controls output --- cmd/get/controls.go | 81 ++++++++++++++++++++++++++----------------- pkg/types/controls.go | 5 +++ 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/cmd/get/controls.go b/cmd/get/controls.go index 28b7703..66b9079 100644 --- a/cmd/get/controls.go +++ b/cmd/get/controls.go @@ -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" @@ -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" { diff --git a/pkg/types/controls.go b/pkg/types/controls.go index 278f330..69cac27 100644 --- a/pkg/types/controls.go +++ b/pkg/types/controls.go @@ -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"`