/
config.go
88 lines (73 loc) · 2 KB
/
config.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
//此源码被清华学神尹成大魔王专业翻译分析并修改
//尹成QQ77025077
//尹成微信18510341407
//尹成所在QQ群721929980
//尹成邮箱 yinc13@mails.tsinghua.edu.cn
//尹成毕业于清华大学,微软区块链领域全球最有价值专家
//https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
/*
版权所有IBM公司。保留所有权利。
SPDX许可证标识符:Apache-2.0
**/
package discovery
import (
"encoding/json"
"fmt"
"io"
"github.com/hyperledger/fabric/cmd/common"
"github.com/hyperledger/fabric/discovery/client"
"github.com/pkg/errors"
)
//newconfigCmd创建新的configCmd
func NewConfigCmd(stub Stub, parser ResponseParser) *ConfigCmd {
return &ConfigCmd{
stub: stub,
parser: parser,
}
}
//configCmd执行检索config的命令
type ConfigCmd struct {
stub Stub
server *string
channel *string
parser ResponseParser
}
//setserver设置configCmd的服务器
func (pc *ConfigCmd) SetServer(server *string) {
pc.server = server
}
//setchannel设置configCmd的通道
func (pc *ConfigCmd) SetChannel(channel *string) {
pc.channel = channel
}
//执行执行命令
func (pc *ConfigCmd) Execute(conf common.Config) error {
if pc.server == nil || *pc.server == "" {
return errors.New("no server specified")
}
if pc.channel == nil || *pc.channel == "" {
return errors.New("no channel specified")
}
server := *pc.server
channel := *pc.channel
req := discovery.NewRequest().OfChannel(channel).AddConfigQuery()
res, err := pc.stub.Send(server, conf, req)
if err != nil {
return err
}
return pc.parser.ParseResponse(channel, res)
}
//configresponseparser分析配置响应
type ConfigResponseParser struct {
io.Writer
}
//ParseResponse解析给定通道的给定响应
func (parser *ConfigResponseParser) ParseResponse(channel string, res ServiceResponse) error {
chanConf, err := res.ForChannel(channel).Config()
if err != nil {
return err
}
jsonBytes, _ := json.MarshalIndent(chanConf, "", "\t")
fmt.Fprintln(parser.Writer, string(jsonBytes))
return nil
}