Skip to content

Commit

Permalink
Added tuple delete support for CSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlyturnips authored and kindlyturnips committed Feb 15, 2024
1 parent f8d2c52 commit 4f6b139
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
13 changes: 4 additions & 9 deletions cmd/tuple/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package tuple
import (
"context"
"fmt"
"os"

"github.com/openfga/go-sdk/client"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/openfga/cli/internal/cmdutils"
"github.com/openfga/cli/internal/output"
"github.com/openfga/cli/internal/tuplefile"
)

// deleteCmd represents the delete command.
Expand All @@ -46,19 +45,15 @@ var deleteCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("failed to parse file name due to %w", err)
}

if fileName != "" {
var tuples []client.ClientTupleKeyWithoutCondition

data, err := os.ReadFile(fileName)
tuplesWithCondition, err := tuplefile.ReadTupleFile(fileName)
if err != nil {
return fmt.Errorf("failed to read file %s due to %w", fileName, err)
}

err = yaml.Unmarshal(data, &tuples)
if err != nil {
return fmt.Errorf("failed to parse input tuples due to %w", err)
}

var tuples = tuplefile.ClientTupleKeyToTupleKeyWithoutCondition(tuplesWithCondition)
maxTuplesPerWrite, err := cmd.Flags().GetInt("max-tuples-per-write")
if err != nil {
return fmt.Errorf("failed to parse max tuples per write due to %w", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/tuple/testdata/tuples.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context
user,anne,,owner,folder,product,inOfficeIP,
user,chris,,owner,folder,product,inOfficeIP,
folder,product,,parent,folder,product-2021,inOfficeIP,"{""ip_addr"":""10.0.0.1""}"
team,fga,member,viewer,folder,product-2021,,
team,fga,member,viewer,folder,product-2021,,
14 changes: 14 additions & 0 deletions internal/tuplefile/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"

openfga "github.com/openfga/go-sdk"
"github.com/openfga/go-sdk/client"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -32,3 +33,16 @@ func ReadTupleFile(fileName string) ([]client.ClientTupleKey, error) {

return tuples, nil
}

func ClientTupleKeyToTupleKeyWithoutCondition(clientTupleKey []client.ClientTupleKey) []openfga.TupleKeyWithoutCondition {
var tuples []openfga.TupleKeyWithoutCondition
for _, tuple := range clientTupleKey {
convertedTuple := openfga.TupleKeyWithoutCondition{
User: tuple.User,
Relation: tuple.Relation,
Object: tuple.Object,
}
tuples = append(tuples, convertedTuple)
}
return tuples
}

0 comments on commit 4f6b139

Please sign in to comment.