/
acl_query.go
90 lines (80 loc) · 2.06 KB
/
acl_query.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
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (c) 2021. Baidu Inc. All Rights Reserved.
*/
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/xuperchain/xupercore/lib/utils"
"github.com/xuperchain/xuperos/common/xupospb/pb"
)
// ACLQueryCommand query acl struct
type ACLQueryCommand struct {
cli *Cli
cmd *cobra.Command
accountName string
contractName string
methodName string
}
// NewACLQueryCommand new acl query cmd
func NewACLQueryCommand(cli *Cli) *cobra.Command {
t := new(ACLQueryCommand)
t.cli = cli
t.cmd = &cobra.Command{
Use: "query [OPTIONS] account contract method",
Short: "query an access control list(ACL) for an account or contract method.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
return t.queryACL(ctx)
},
}
t.addFlags()
return t.cmd
}
func (t *ACLQueryCommand) addFlags() {
t.cmd.Flags().StringVar(&t.accountName, "account", "", "contract account name")
t.cmd.Flags().StringVar(&t.contractName, "contract", "", "contract name")
t.cmd.Flags().StringVar(&t.methodName, "method", "", "method name")
}
func (t *ACLQueryCommand) queryACL(ctx context.Context) error {
client := t.cli.XchainClient()
aclStatus := &pb.AclStatus{
Header: &pb.Header{
Logid: utils.GenLogId(),
},
Bcname: t.cli.RootOptions.Name,
AccountName: t.accountName,
ContractName: t.contractName,
MethodName: t.methodName,
}
if len(t.accountName) == 0 && len(t.contractName) == 0 {
return errors.New("param error")
}
reply, err := client.QueryACL(ctx, aclStatus)
if err != nil {
return err
}
if reply.Header.Error != pb.XChainErrorEnum_SUCCESS {
return errors.New(reply.Header.Error.String())
}
if reply != nil {
acl := reply.GetAcl()
output, err := json.MarshalIndent(acl, "", " ")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(output))
if string(output) != "{}" && output != nil {
confirmed := reply.GetConfirmed()
if !confirmed {
fmt.Println("unconfirmed")
} else {
fmt.Println("confirmed")
}
}
}
return nil
}