diff --git a/README.md b/README.md index 4e37a0f..c8e4843 100644 --- a/README.md +++ b/README.md @@ -656,7 +656,7 @@ fga tuple **delete** --store-id= If you want to delete all the tuples in a store, you can use the following code: ``` -fga tuple read --simple-json > tuples.json +fga tuple read --simple-output > tuples.json fga tuple delete --file tuples.json ``` @@ -671,7 +671,7 @@ fga tuple **read** [--user=] [--relation=] [--object=] * `--relation`: Relation * `--object`: Object * `--max-pages`: Max number of pages to get. (default 20) -* `--simple-json`: Output simpler JSON version. (It can be used by write and delete commands) +* `--simple-output`: Output simpler JSON version. (It can be used by write and delete commands) ###### Example `fga tuple read --store-id=01H0H015178Y2V4CX10C2KGHF4 --user user:anne --relation can_view --object document:roadmap` @@ -691,7 +691,7 @@ fga tuple **read** [--user=] [--relation=] [--object=] ] } ``` -###### Response (--simple-json) +###### Response (--simple-output) ```json5 [ { @@ -706,7 +706,7 @@ fga tuple **read** [--user=] [--relation=] [--object=] If you want to transform this output in a way that can be then imported using the `fga tuple import` you can run ``` -fga tuple read --simple-json > tuples.json +fga tuple read --simple-output --max-pages 0 > tuples.json fga tuple import --file tuples.json ``` diff --git a/cmd/tuple/read.go b/cmd/tuple/read.go index fb6cd33..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 == "" || 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 } @@ -112,8 +125,8 @@ var readCmd = &cobra.Command{ return err } - simpleJSON, _ := cmd.Flags().GetBool("simple-json") - if simpleJSON { + simpleOutput, _ := cmd.Flags().GetBool("simple-output") + if simpleOutput { return output.Display(response.simple) //nolint:wrapcheck } @@ -125,6 +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().Bool("simple-json", false, "Output simpler JSON version. (It can be used by write and delete commands)") //nolint:lll + 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 } diff --git a/cmd/tuple/write.go b/cmd/tuple/write.go index 3139f68..4ca80ce 100644 --- a/cmd/tuple/write.go +++ b/cmd/tuple/write.go @@ -30,11 +30,14 @@ import ( // writeCmd represents the write command. var writeCmd = &cobra.Command{ - Use: "write", - Short: "Create Relationship Tuples", - Long: "Add relationship tuples to the store.", - Args: ExactArgsOrFlag(3, "file"), //nolint:gomnd - Example: "fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 user:anne can_view document:roadmap", + Use: "write", + Short: "Create Relationship Tuples", + Long: "Add relationship tuples to the store.", + Args: ExactArgsOrFlag(3, "file"), //nolint:gomnd + Example: `fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 user:anne can_view document:roadmap +fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 --file tuples.json +fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 --file tuples.yaml +`, RunE: func(cmd *cobra.Command, args []string) error { clientConfig := cmdutils.GetClientConfig(cmd) fgaClient, err := clientConfig.GetFgaClient()