Skip to content

Commit

Permalink
fix(cli): panic when printing empty expand trees (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Aug 31, 2021
1 parent adf7d4c commit 7956dec
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/expand/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func NewExpandCmd() *cobra.Command {
cmdx.PrintJSONAble(cmd, tree)
switch flagx.MustGetString(cmd, cmdx.FlagFormat) {
case string(cmdx.FormatDefault), "":
if tree == nil && !flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
_, _ = fmt.Fprint(cmd.OutOrStdout(), "Got an empty tree. This probably means that the requested relation tuple is not present in Keto.")
}
_, _ = fmt.Fprintln(cmd.OutOrStdout())
}
return nil
Expand All @@ -68,6 +71,7 @@ func NewExpandCmd() *cobra.Command {

client.RegisterRemoteURLFlags(cmd.Flags())
cmdx.RegisterJSONFormatFlags(cmd.Flags())
cmdx.RegisterNoiseFlags(cmd.Flags())
cmd.Flags().Int32P(FlagMaxDepth, "d", 100, "maximum depth of the tree")

return cmd
Expand Down
15 changes: 12 additions & 3 deletions cmd/expand/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/ory/x/cmdx"

"github.com/stretchr/testify/assert"

"github.com/ory/keto/cmd/client"
Expand All @@ -15,7 +16,15 @@ func TestExpandCommand(t *testing.T) {
ts := client.NewTestServer(t, client.ReadServer, []*namespace.Namespace{nspace}, NewExpandCmd)
defer ts.Shutdown(t)

ts.Cmd.PersistentArgs = append(ts.Cmd.PersistentArgs, "--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
stdOut := ts.Cmd.ExecNoErr(t, "access", nspace.Name, "object")
assert.Equal(t, "null\n", stdOut)
t.Run("case=unknown tuple", func(t *testing.T) {
t.Run("format=JSON", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "access", nspace.Name, "object", "--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
assert.Equal(t, "null\n", stdOut)
})

t.Run("format=default", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "access", nspace.Name, "object", "--"+cmdx.FlagFormat, string(cmdx.FormatDefault))
assert.Contains(t, stdOut, "empty tree")
})
})
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build docscodesamples
// +build docscodesamples

package main
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build docscodesamples
// +build docscodesamples

package main
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build docscodesamples
// +build docscodesamples

package main
Expand Down
1 change: 1 addition & 0 deletions docs/docs/cli/keto-expand.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ keto expand <relation> <namespace> <object> [flags]
-f, --format string Set the output format. One of default, json, and json-pretty. (default "default")
-h, --help help for expand
-d, --max-depth int32 maximum depth of the tree (default 100)
-q, --quiet Be quiet with output printing.
--read-remote string Remote URL of the read API endpoint. (default "127.0.0.1:4466")
--write-remote string Remote URL of the write API endpoint. (default "127.0.0.1:4467")
```
Expand Down
1 change: 1 addition & 0 deletions go_mod_indirect_pins.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go_mod_indirect_pins
// +build go_mod_indirect_pins

package main
4 changes: 4 additions & 0 deletions internal/expand/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func TreeFromProto(t *acl.SubjectTree) (*Tree, error) {
}

func (t *Tree) String() string {
if t == nil {
return ""
}

sub := t.Subject.String()

if t.Type == Leaf {
Expand Down

0 comments on commit 7956dec

Please sign in to comment.