-
Notifications
You must be signed in to change notification settings - Fork 24
/
check.go
75 lines (64 loc) · 2.02 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package directory
import (
"github.com/aserto-dev/go-directory/aserto/directory/reader/v3"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/clients"
"github.com/aserto-dev/topaz/pkg/cli/edit"
"github.com/aserto-dev/topaz/pkg/cli/fflag"
"github.com/aserto-dev/topaz/pkg/cli/jsonx"
"github.com/aserto-dev/topaz/pkg/cli/prompter"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
type CheckCmd struct {
Request string `arg:"" type:"string" name:"request" optional:"" help:"json request or file path to check permission request or '-' to read from stdin"`
Template bool `name:"template" short:"t" help:"prints a check permission request template on stdout"`
Editor bool `name:"edit" short:"e" help:"edit config file" hidden:"" type:"fflag.Editor"`
clients.DirectoryConfig
}
func (cmd *CheckCmd) Run(c *cc.CommonCtx) error {
if cmd.Template {
return jsonx.OutputJSONPB(c.UI.Output(), cmd.template())
}
client, err := clients.NewDirectoryClient(c, &cmd.DirectoryConfig)
if err != nil {
return errors.Wrap(err, "failed to get directory client")
}
if cmd.Request == "" && cmd.Editor && fflag.Enabled(fflag.Editor) {
req, err := edit.Msg(cmd.template())
if err != nil {
return err
}
cmd.Request = req
}
if cmd.Request == "" && fflag.Enabled(fflag.Prompter) {
p := prompter.New(cmd.template())
if err := p.Show(); err != nil {
return err
}
cmd.Request = jsonx.MaskedMarshalOpts().Format(p.Req())
}
if cmd.Request == "" {
return errors.New("request argument is required")
}
var req reader.CheckRequest
err = clients.UnmarshalRequest(cmd.Request, &req)
if err != nil {
return err
}
resp, err := client.V3.Reader.Check(c.Context, &req)
if err != nil {
return errors.Wrap(err, "check permission call failed")
}
return jsonx.OutputJSONPB(c.UI.Output(), resp)
}
func (cmd *CheckCmd) template() proto.Message {
return &reader.CheckRequest{
ObjectType: "",
ObjectId: "",
Relation: "",
SubjectType: "",
SubjectId: "",
Trace: false,
}
}