github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/discovery/cmd/cmd.go (about)

     1  /*
     2  Copyright hechain. 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/hechain20/hechain/cmd/common"
    14  	discovery "github.com/hechain20/hechain/discovery/client"
    15  	"gopkg.in/alecthomas/kingpin.v2"
    16  )
    17  
    18  const (
    19  	PeersCommand     = "peers"
    20  	ConfigCommand    = "config"
    21  	EndorsersCommand = "endorsers"
    22  )
    23  
    24  // responseParserWriter defines the stdout
    25  var responseParserWriter = os.Stdout
    26  
    27  const (
    28  	defaultTimeout = time.Second * 10
    29  )
    30  
    31  //go:generate mockery -dir . -name Stub -case underscore -output mocks/
    32  
    33  // Stub represents the remote discovery service
    34  type Stub interface {
    35  	// Send sends the request, and receives a response
    36  	Send(server string, conf common.Config, req *discovery.Request) (ServiceResponse, error)
    37  }
    38  
    39  //go:generate mockery -dir . -name ResponseParser -case underscore -output mocks/
    40  
    41  // ResponseParser parses responses sent from the server
    42  type ResponseParser interface {
    43  	// ParseResponse parses the response and uses the given output when emitting data
    44  	ParseResponse(channel string, response ServiceResponse) error
    45  }
    46  
    47  //go:generate mockery -dir . -name CommandRegistrar -case underscore -output mocks/
    48  
    49  // CommandRegistrar registers commands
    50  type CommandRegistrar interface {
    51  	// Command adds a new top-level command to the CLI
    52  	Command(name, help string, onCommand common.CLICommand) *kingpin.CmdClause
    53  }
    54  
    55  // AddCommands registers the discovery commands to the given CommandRegistrar
    56  func AddCommands(cli CommandRegistrar) {
    57  	peerCmd := NewPeerCmd(&ClientStub{}, &PeerResponseParser{Writer: responseParserWriter})
    58  	peers := cli.Command(PeersCommand, "Discover peers", peerCmd.Execute)
    59  	server := peers.Flag("server", "Sets the endpoint of the server to connect").String()
    60  	channel := peers.Flag("channel", "Sets the channel the query is intended to").String()
    61  	peerCmd.SetServer(server)
    62  	peerCmd.SetChannel(channel)
    63  
    64  	configCmd := NewConfigCmd(&ClientStub{}, &ConfigResponseParser{Writer: responseParserWriter})
    65  	config := cli.Command(ConfigCommand, "Discover channel config", configCmd.Execute)
    66  	server = config.Flag("server", "Sets the endpoint of the server to connect").String()
    67  	channel = config.Flag("channel", "Sets the channel the query is intended to").String()
    68  	configCmd.SetServer(server)
    69  	configCmd.SetChannel(channel)
    70  
    71  	endorserCmd := NewEndorsersCmd(&RawStub{}, &EndorserResponseParser{Writer: responseParserWriter})
    72  	endorsers := cli.Command(EndorsersCommand, "Discover chaincode endorsers", endorserCmd.Execute)
    73  	chaincodes := endorsers.Flag("chaincode", "Specifies the chaincode name(s)").Strings()
    74  	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()
    75  	noPrivReads := endorsers.Flag("noPrivateReads", "Specifies chaincodes that are not expected to be have private data read").PlaceHolder("CHAINCODE").Strings()
    76  
    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  	endorserCmd.SetNoPrivateReads(noPrivReads)
    84  }