-
Notifications
You must be signed in to change notification settings - Fork 26
/
get.go
78 lines (68 loc) · 2.15 KB
/
get.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
76
77
78
package authorizer
import (
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2"
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api"
"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"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
type GetPolicyCmd struct {
Request string `arg:"" type:"string" name:"request" optional:"" help:"json request or file path to get policy 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.AuthorizerConfig
}
func (cmd *GetPolicyCmd) Run(c *cc.CommonCtx) error {
if cmd.Template {
return jsonx.OutputJSONPB(c.UI.Output(), cmd.template())
}
client, err := clients.NewAuthorizerClient(c, &cmd.AuthorizerConfig)
if err != nil {
return errors.Wrap(err, "failed to get authorizer 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 authorizer.GetPolicyRequest
err = clients.UnmarshalRequest(cmd.Request, &req)
if err != nil {
return err
}
resp, err := client.GetPolicy(c.Context, &req)
if err != nil {
return err
}
return jsonx.OutputJSONPB(c.UI.Output(), resp)
}
func (cmd *GetPolicyCmd) template() proto.Message {
return &authorizer.GetPolicyRequest{
Id: "",
FieldMask: &fieldmaskpb.FieldMask{
Paths: []string{},
},
PolicyInstance: &api.PolicyInstance{
Name: "",
InstanceLabel: "",
},
}
}