forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
83 lines (67 loc) · 3.04 KB
/
cmd.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package discovery
import (
"os"
"time"
"github.com/hyperledger/fabric/cmd/common"
"github.com/hyperledger/fabric/discovery/client"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
PeersCommand = "peers"
ConfigCommand = "config"
EndorsersCommand = "endorsers"
)
var (
// responseParserWriter defines the stdout
responseParserWriter = os.Stdout
)
const (
defaultTimeout = time.Second * 10
)
//go:generate mockery -dir . -name Stub -case underscore -output mocks/
// Stub represents the remote discovery service
type Stub interface {
// Send sends the request, and receives a response
Send(server string, conf common.Config, req *discovery.Request) (ServiceResponse, error)
}
//go:generate mockery -dir . -name ResponseParser -case underscore -output mocks/
// ResponseParser parses responses sent from the server
type ResponseParser interface {
// ParseResponse parses the response and uses the given output when emitting data
ParseResponse(channel string, response ServiceResponse) error
}
//go:generate mockery -dir . -name CommandRegistrar -case underscore -output mocks/
// CommandRegistrar registers commands
type CommandRegistrar interface {
// Command adds a new top-level command to the CLI
Command(name, help string, onCommand common.CLICommand) *kingpin.CmdClause
}
// AddCommands registers the discovery commands to the given CommandRegistrar
func AddCommands(cli CommandRegistrar) {
peerCmd := NewPeerCmd(&ClientStub{}, &PeerResponseParser{Writer: responseParserWriter})
peers := cli.Command(PeersCommand, "Discover peers", peerCmd.Execute)
server := peers.Flag("server", "Sets the endpoint of the server to connect").String()
channel := peers.Flag("channel", "Sets the channel the query is intended to").String()
peerCmd.SetServer(server)
peerCmd.SetChannel(channel)
configCmd := NewConfigCmd(&ClientStub{}, &ConfigResponseParser{Writer: responseParserWriter})
config := cli.Command(ConfigCommand, "Discover channel config", configCmd.Execute)
server = config.Flag("server", "Sets the endpoint of the server to connect").String()
channel = config.Flag("channel", "Sets the channel the query is intended to").String()
configCmd.SetServer(server)
configCmd.SetChannel(channel)
endorserCmd := NewEndorsersCmd(&RawStub{}, &EndorserResponseParser{Writer: responseParserWriter})
endorsers := cli.Command(EndorsersCommand, "Discover chaincode endorsers", endorserCmd.Execute)
chaincodes := endorsers.Flag("chaincode", "Specifies the chaincode name(s)").Strings()
collections := endorsers.Flag("collection", "Specifies the collection name(s) as a mapping from chaincode to a comma separated list of collections").PlaceHolder("CC:C1,C2").StringMap()
server = endorsers.Flag("server", "Sets the endpoint of the server to connect").String()
channel = endorsers.Flag("channel", "Sets the channel the query is intended to").String()
endorserCmd.SetChannel(channel)
endorserCmd.SetServer(server)
endorserCmd.SetChaincodes(chaincodes)
endorserCmd.SetCollections(collections)
}