From 8b2488c1ca8f0c0de6af84e098a0fd2f6df0f3b8 Mon Sep 17 00:00:00 2001 From: treilik Date: Fri, 19 Aug 2022 10:19:32 +0200 Subject: [PATCH] chore: move Subject creation into helper function --- cmd/check/root.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/cmd/check/root.go b/cmd/check/root.go index f97e8f7e8..806a71f9f 100644 --- a/cmd/check/root.go +++ b/cmd/check/root.go @@ -49,19 +49,9 @@ func newCheckCmd() *cobra.Command { namespace := args[2] object := args[3] - var s *rts.Subject - - // distinguish between subjectSet and subjectID - if strings.Contains(subject, ":") { - su := &ketoapi.SubjectSet{} - su, err := su.FromString(subject) - if err != nil { - return err - } - - s = rts.NewSubjectSet(su.Namespace, su.Object, su.Relation) - } else { - s = rts.NewSubjectID(subject) + s, err := genSubject(subject) + if err != nil { + return err } cl := rts.NewCheckServiceClient(conn) @@ -91,6 +81,25 @@ func newCheckCmd() *cobra.Command { return cmd } +// generate from given string a SubjectID or a SubjectSet embedded into a Subject +func genSubject(subject string) (*rts.Subject, error) { + var s *rts.Subject + + // distinguish between subjectSet and subjectID + if strings.Contains(subject, ":") { + su := &ketoapi.SubjectSet{} + su, err := su.FromString(subject) + if err != nil { + return nil, err + } + + s = rts.NewSubjectSet(su.Namespace, su.Object, su.Relation) + } else { + s = rts.NewSubjectID(subject) + } + return s, nil +} + func RegisterCommandsRecursive(parent *cobra.Command) { parent.AddCommand(newCheckCmd()) }