Skip to content

Commit

Permalink
chore: move Subject creation into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
treilik committed Aug 19, 2022
1 parent 01c1452 commit 8b2488c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions cmd/check/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
}

0 comments on commit 8b2488c

Please sign in to comment.