diff --git a/cmd/expand/root.go b/cmd/expand/root.go index fcf20ad73..5ceacafba 100644 --- a/cmd/expand/root.go +++ b/cmd/expand/root.go @@ -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 @@ -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 diff --git a/cmd/expand/root_test.go b/cmd/expand/root_test.go index c7c2aca8a..4e87ab6a3 100644 --- a/cmd/expand/root_test.go +++ b/cmd/expand/root_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ory/x/cmdx" + "github.com/stretchr/testify/assert" "github.com/ory/keto/cmd/client" @@ -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") + }) + }) } diff --git a/contrib/docs-code-samples/expand-api-display-access/99-cleanup/main.go b/contrib/docs-code-samples/expand-api-display-access/99-cleanup/main.go index 4c8253fec..493d1401a 100644 --- a/contrib/docs-code-samples/expand-api-display-access/99-cleanup/main.go +++ b/contrib/docs-code-samples/expand-api-display-access/99-cleanup/main.go @@ -1,3 +1,4 @@ +//go:build docscodesamples // +build docscodesamples package main diff --git a/contrib/docs-code-samples/list-api-display-objects/99-cleanup/main.go b/contrib/docs-code-samples/list-api-display-objects/99-cleanup/main.go index f2423d5fb..e81ce2bb7 100644 --- a/contrib/docs-code-samples/list-api-display-objects/99-cleanup/main.go +++ b/contrib/docs-code-samples/list-api-display-objects/99-cleanup/main.go @@ -1,3 +1,4 @@ +//go:build docscodesamples // +build docscodesamples package main diff --git a/contrib/docs-code-samples/simple-access-check-guide/99-cleanup/main.go b/contrib/docs-code-samples/simple-access-check-guide/99-cleanup/main.go index d96e67f9b..b0679115c 100644 --- a/contrib/docs-code-samples/simple-access-check-guide/99-cleanup/main.go +++ b/contrib/docs-code-samples/simple-access-check-guide/99-cleanup/main.go @@ -1,3 +1,4 @@ +//go:build docscodesamples // +build docscodesamples package main diff --git a/docs/docs/cli/keto-expand.md b/docs/docs/cli/keto-expand.md index 52231ef3a..8f9ac06c1 100644 --- a/docs/docs/cli/keto-expand.md +++ b/docs/docs/cli/keto-expand.md @@ -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") ``` diff --git a/go_mod_indirect_pins.go b/go_mod_indirect_pins.go index 5442245c7..494237496 100644 --- a/go_mod_indirect_pins.go +++ b/go_mod_indirect_pins.go @@ -1,3 +1,4 @@ +//go:build go_mod_indirect_pins // +build go_mod_indirect_pins package main diff --git a/internal/expand/tree.go b/internal/expand/tree.go index 5a93a747c..63375705b 100644 --- a/internal/expand/tree.go +++ b/internal/expand/tree.go @@ -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 {