Skip to content

Commit

Permalink
fix: in read, rename --simple-json to --simple-output (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Oct 12, 2023
2 parents ee44f51 + 0327d23 commit fce955d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ fga tuple **delete** <user> <relation> <object> --store-id=<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
```

Expand All @@ -671,7 +671,7 @@ fga tuple **read** [--user=<user>] [--relation=<relation>] [--object=<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`
Expand All @@ -691,7 +691,7 @@ fga tuple **read** [--user=<user>] [--relation=<relation>] [--object=<object>]
]
}
```
###### Response (--simple-json)
###### Response (--simple-output)
```json5
[
{
Expand All @@ -706,7 +706,7 @@ fga tuple **read** [--user=<user>] [--relation=<relation>] [--object=<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
```
Expand Down
57 changes: 35 additions & 22 deletions cmd/tuple/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
13 changes: 8 additions & 5 deletions cmd/tuple/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fce955d

Please sign in to comment.