Skip to content

Commit

Permalink
fix: added page_number flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ADorigi committed Sep 16, 2024
1 parent 44e4b69 commit 1286349
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 48 deletions.
9 changes: 8 additions & 1 deletion cmd/get/benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ to quickly create a Cobra application.`,
}

requestPayload := types.GetBenchmarkPayload{
Cursor: 1,
Cursor: int(utils.ReadIntFlag(cmd, "page_number")),
PerPage: int(utils.ReadIntFlag(cmd, "page_size")),
OnlyRootBenchmark: utils.ReadBoolFlag(cmd, "show_only_root"),
IncludeFindingsSummary: utils.ReadBoolFlag(cmd, "include_findings_summary"),
Expand Down Expand Up @@ -92,6 +92,13 @@ to quickly create a Cobra application.`,
fmt.Print(string(js))
}

fmt.Printf(
"\n\n\n\nNext Page: \n\tcheckctl get benchmarks --page_size %d --page_number %d --output %s\n",
utils.ReadIntFlag(cmd, "page_size"),
utils.ReadIntFlag(cmd, "page_number")+1,
outputFormat,
)

return nil
},
}
Expand Down
87 changes: 40 additions & 47 deletions cmd/get/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,70 +40,63 @@ to quickly create a Cobra application.`,
outputFormat = configuration.OutputFormat
}

var cursor = 1
var controls []types.Control

for {

requestPayload := types.RequestPayload{
Cursor: cursor,
PerPage: int(utils.ReadIntFlag(cmd, "page_size")),
}

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

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

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
requestPayload := types.RequestPayload{
Cursor: int(utils.ReadIntFlag(cmd, "page_number")),
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
}

var getControlsResponse types.GetControlsResponse
err = json.Unmarshal(body, &getControlsResponse)
if err != nil {
return err
}
request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"POST",
"main/compliance/api/v3/controls",
payload,
)
if err != nil {
return err
}

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

controls = append(controls, getControlsResponse.Items...)
cursor += 1
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 outputFormat == "table" {
rows := utils.GenerateControlRows(controls)
rows := utils.GenerateControlRows(getControlsResponse.Items)

tables.PrintControlsTable(rows)
} else {
js, err := json.MarshalIndent(controls, "", " ")
js, err := json.MarshalIndent(getControlsResponse.Items, "", " ")
if err != nil {
return err
}
fmt.Print(string(js))
}

fmt.Printf(
"\n\n\n\nNext Page: \n\tcheckctl get controls --page_size %d --page_number %d --output %s\n",
utils.ReadIntFlag(cmd, "page_size"),
utils.ReadIntFlag(cmd, "page_number")+1,
outputFormat,
)

return nil
},
}
1 change: 1 addition & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ func init() {
GetCmd.AddCommand(benchmarksCmd)

GetCmd.PersistentFlags().Int("page_size", 25, "Defines page size of response")
GetCmd.PersistentFlags().Int("page_number", 1, "Defines page number of response")

}

0 comments on commit 1286349

Please sign in to comment.