From 0327d23d62c24673c8962370b2d3a761045335ed Mon Sep 17 00:00:00 2001 From: Raghd Hamzeh Date: Thu, 12 Oct 2023 00:32:47 -0400 Subject: [PATCH] fix: lint --- cmd/tuple/read.go | 51 ++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/cmd/tuple/read.go b/cmd/tuple/read.go index 8d8b5df..b584eea 100644 --- a/cmd/tuple/read.go +++ b/cmd/tuple/read.go @@ -35,22 +35,9 @@ type readResponse struct { simple []openfga.TupleKey } -func read(fgaClient client.SdkClient, user string, relation string, object string, maxPages int) ( - *readResponse, error, +func baseRead(fgaClient client.SdkClient, body *client.ClientReadRequest, maxPages int) ( + *openfga.ReadResponse, error, ) { - body := &client.ClientReadRequest{} - if user != "" { - body.User = &user - } - - if relation != "" { - body.Relation = &relation - } - - if object != "" { - body.Object = &object - } - tuples := make([]openfga.Tuple, 0) continuationToken := "" pageIndex := 0 @@ -67,19 +54,45 @@ func read(fgaClient client.SdkClient, user string, relation string, object strin tuples = append(tuples, *response.Tuples...) pageIndex++ - if response.ContinuationToken == nil || *response.ContinuationToken == "" || (maxPages != 0 && pageIndex >= maxPages) { + if response.ContinuationToken == nil || + *response.ContinuationToken == "" || + (maxPages != 0 && pageIndex >= maxPages) { break } continuationToken = *response.ContinuationToken } + return &openfga.ReadResponse{Tuples: &tuples}, nil +} + +func read(fgaClient client.SdkClient, user string, relation string, object string, maxPages int) ( + *readResponse, error, +) { + body := &client.ClientReadRequest{} + if user != "" { + body.User = &user + } + + if relation != "" { + body.Relation = &relation + } + + if object != "" { + body.Object = &object + } + + response, err := baseRead(fgaClient, body, maxPages) + if err != nil { + return nil, err + } + justKeys := make([]openfga.TupleKey, 0) - for _, tuple := range tuples { + for _, tuple := range response.GetTuples() { justKeys = append(justKeys, *tuple.Key) } - res := readResponse{complete: &openfga.ReadResponse{Tuples: &tuples}, simple: justKeys} + res := readResponse{complete: &openfga.ReadResponse{Tuples: response.Tuples}, simple: justKeys} return &res, nil } @@ -125,8 +138,6 @@ func init() { readCmd.Flags().String("user", "", "User") readCmd.Flags().String("relation", "", "Relation") readCmd.Flags().String("object", "", "Object") - readCmd.Flags().Int("max-pages", MaxReadPagesLength, "Max number of pages to get.") - readCmd.Flags().Int("max-pages", MaxReadPagesLength, "Max number of pages to get. Set to 0 to get all pages.") readCmd.Flags().Bool("simple-output", false, "Output simpler JSON version. (It can be used by write and delete commands)") //nolint:lll }