github.com/kaituanwang/hyperledger@v2.0.1+incompatible/discovery/cmd/cmd.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package discovery 8 9 import ( 10 "os" 11 "time" 12 13 "github.com/hyperledger/fabric/cmd/common" 14 discovery "github.com/hyperledger/fabric/discovery/client" 15 "gopkg.in/alecthomas/kingpin.v2" 16 ) 17 18 const ( 19 PeersCommand = "peers" 20 ConfigCommand = "config" 21 EndorsersCommand = "endorsers" 22 ) 23 24 var ( 25 // responseParserWriter defines the stdout 26 responseParserWriter = os.Stdout 27 ) 28 29 const ( 30 defaultTimeout = time.Second * 10 31 ) 32 33 //go:generate mockery -dir . -name Stub -case underscore -output mocks/ 34 35 // Stub represents the remote discovery service 36 type Stub interface { 37 // Send sends the request, and receives a response 38 Send(server string, conf common.Config, req *discovery.Request) (ServiceResponse, error) 39 } 40 41 //go:generate mockery -dir . -name ResponseParser -case underscore -output mocks/ 42 43 // ResponseParser parses responses sent from the server 44 type ResponseParser interface { 45 // ParseResponse parses the response and uses the given output when emitting data 46 ParseResponse(channel string, response ServiceResponse) error 47 } 48 49 //go:generate mockery -dir . -name CommandRegistrar -case underscore -output mocks/ 50 51 // CommandRegistrar registers commands 52 type CommandRegistrar interface { 53 // Command adds a new top-level command to the CLI 54 Command(name, help string, onCommand common.CLICommand) *kingpin.CmdClause 55 } 56 57 // AddCommands registers the discovery commands to the given CommandRegistrar 58 func AddCommands(cli CommandRegistrar) { 59 peerCmd := NewPeerCmd(&ClientStub{}, &PeerResponseParser{Writer: responseParserWriter}) 60 peers := cli.Command(PeersCommand, "Discover peers", peerCmd.Execute) 61 server := peers.Flag("server", "Sets the endpoint of the server to connect").String() 62 channel := peers.Flag("channel", "Sets the channel the query is intended to").String() 63 peerCmd.SetServer(server) 64 peerCmd.SetChannel(channel) 65 66 configCmd := NewConfigCmd(&ClientStub{}, &ConfigResponseParser{Writer: responseParserWriter}) 67 config := cli.Command(ConfigCommand, "Discover channel config", configCmd.Execute) 68 server = config.Flag("server", "Sets the endpoint of the server to connect").String() 69 channel = config.Flag("channel", "Sets the channel the query is intended to").String() 70 configCmd.SetServer(server) 71 configCmd.SetChannel(channel) 72 73 endorserCmd := NewEndorsersCmd(&RawStub{}, &EndorserResponseParser{Writer: responseParserWriter}) 74 endorsers := cli.Command(EndorsersCommand, "Discover chaincode endorsers", endorserCmd.Execute) 75 chaincodes := endorsers.Flag("chaincode", "Specifies the chaincode name(s)").Strings() 76 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() 77 server = endorsers.Flag("server", "Sets the endpoint of the server to connect").String() 78 channel = endorsers.Flag("channel", "Sets the channel the query is intended to").String() 79 endorserCmd.SetChannel(channel) 80 endorserCmd.SetServer(server) 81 endorserCmd.SetChaincodes(chaincodes) 82 endorserCmd.SetCollections(collections) 83 }