diff --git a/cmd/tuple/delete.go b/cmd/tuple/delete.go index ff5ab8e..3d08182 100644 --- a/cmd/tuple/delete.go +++ b/cmd/tuple/delete.go @@ -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. @@ -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) diff --git a/cmd/tuple/testdata/tuples.csv b/cmd/tuple/testdata/tuples.csv index b2f7810..4e2a621 100644 --- a/cmd/tuple/testdata/tuples.csv +++ b/cmd/tuple/testdata/tuples.csv @@ -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,, \ No newline at end of file diff --git a/internal/tuplefile/read.go b/internal/tuplefile/read.go index 041d43c..565d9a3 100644 --- a/internal/tuplefile/read.go +++ b/internal/tuplefile/read.go @@ -5,6 +5,7 @@ import ( "os" "path" + openfga "github.com/openfga/go-sdk" "github.com/openfga/go-sdk/client" "gopkg.in/yaml.v3" ) @@ -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 +}