Skip to content

Commit

Permalink
Merge pull request #269 from authzed/args-take-precedence-over-stdin
Browse files Browse the repository at this point in the history
fixes when STDIN input and CLI args are provided
  • Loading branch information
vroldanbet committed Jul 24, 2023
2 parents 7568737 + 1a8fdc0 commit fe3bb03
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
7 changes: 2 additions & 5 deletions internal/commands/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ var bulkDeleteCmd = &cobra.Command{
}

func writeRelationshipsFromArgsOrStdin(cmd *cobra.Command, args []string) error {
if ok := isArgsViaFile(os.Stdin); ok {
if len(args) > 0 {
return fmt.Errorf("cannot provide input both via command-line args and stdin")
}
if ok := isArgsViaFile(os.Stdin) && len(args) == 0; ok {
return nil
}
return cobra.ExactArgs(3)(cmd, args)
Expand Down Expand Up @@ -402,7 +399,7 @@ var ErrExhaustedRelationships = errors.New("exhausted all relationships")
func writeRelationshipCmdFunc(operation v1.RelationshipUpdate_Operation, input *os.File) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
parser := SliceRelationshipParser(args)
if isArgsViaFile(input) {
if isArgsViaFile(input) && len(args) == 0 {
parser = FileRelationshipParser(input)
}

Expand Down
65 changes: 57 additions & 8 deletions internal/commands/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,23 @@ func TestWriteRelationshipsArgs(t *testing.T) {
_ = os.Remove(f.Name())
})

// returns accepts anything if input file is not a terminal
require.Nil(t, writeRelationshipsFromArgsOrStdin(&cobra.Command{}, nil))

// does not accept both file input and arguments
require.ErrorContains(t, writeRelationshipsFromArgsOrStdin(&cobra.Command{}, []string{"a", "b"}), "cannot provide input both via command-line args and stdin")

// checks there is 3 input arguments in case of tty
isTerm := false
originalFunc := isFileTerminal
isFileTerminal = func(f *os.File) bool {
return true
return isTerm
}
defer func() {
isFileTerminal = originalFunc
}()

// returns accepts anything if input file is not a terminal
require.Nil(t, writeRelationshipsFromArgsOrStdin(&cobra.Command{}, nil))

// if both STDIN and CLI args are provided, CLI args take precedence
require.ErrorContains(t, writeRelationshipsFromArgsOrStdin(&cobra.Command{}, []string{"a", "b"}), "accepts 3 arg(s), received 2")

isTerm = true
// checks there is 3 input arguments in case of tty
require.ErrorContains(t, writeRelationshipsFromArgsOrStdin(&cobra.Command{}, nil), "accepts 3 arg(s), received 0")
require.Nil(t, writeRelationshipsFromArgsOrStdin(&cobra.Command{}, []string{"a", "b", "c"}))
}
Expand Down Expand Up @@ -261,6 +264,52 @@ func TestWriteRelationshipCmdFuncFromTTY(t *testing.T) {
require.NoError(t, err)
}

func TestWriteRelationshipCmdFuncArgsTakePrecedence(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: tuple.ParseRel("resource:1#viewer@user:1"),
},
},
}}}, nil
}

originalFunc := isFileTerminal
isFileTerminal = func(f *os.File) bool {
return false
}
defer func() {
isFileTerminal = originalFunc
}()

fi := fileFromStrings(t, []string{
"resource:1 viewer user:3",
})
defer func() {
require.NoError(t, fi.Close())
}()
t.Cleanup(func() {
_ = os.Remove(fi.Name())
})

originalClient := client.NewClient
client.NewClient = mock
defer func() {
client.NewClient = originalClient
}()

f := writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_TOUCH, fi)
cmd := &cobra.Command{}
cmd.Flags().Int("batch-size", 100, "")
cmd.Flags().Bool("json", true, "")
cmd.Flags().String("caveat", "", "")

err := f(cmd, []string{"resource:1", "viewer", "user:1"})
require.NoError(t, err)
}

func TestWriteRelationshipCmdFuncFromStdin(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{{
Expand Down

0 comments on commit fe3bb03

Please sign in to comment.