github.com/yimialmonte/fabric@v2.1.1+incompatible/discovery/cmd/config.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 "encoding/json" 11 "fmt" 12 "io" 13 14 "github.com/hyperledger/fabric/cmd/common" 15 discovery "github.com/hyperledger/fabric/discovery/client" 16 "github.com/pkg/errors" 17 ) 18 19 // NewConfigCmd creates a new ConfigCmd 20 func NewConfigCmd(stub Stub, parser ResponseParser) *ConfigCmd { 21 return &ConfigCmd{ 22 stub: stub, 23 parser: parser, 24 } 25 } 26 27 // ConfigCmd executes a command that retrieves config 28 type ConfigCmd struct { 29 stub Stub 30 server *string 31 channel *string 32 parser ResponseParser 33 } 34 35 // SetServer sets the server of the ConfigCmd 36 func (pc *ConfigCmd) SetServer(server *string) { 37 pc.server = server 38 } 39 40 // SetChannel sets the channel of the ConfigCmd 41 func (pc *ConfigCmd) SetChannel(channel *string) { 42 pc.channel = channel 43 } 44 45 // Execute executes the command 46 func (pc *ConfigCmd) Execute(conf common.Config) error { 47 if pc.server == nil || *pc.server == "" { 48 return errors.New("no server specified") 49 } 50 if pc.channel == nil || *pc.channel == "" { 51 return errors.New("no channel specified") 52 } 53 54 server := *pc.server 55 channel := *pc.channel 56 57 req := discovery.NewRequest().OfChannel(channel).AddConfigQuery() 58 res, err := pc.stub.Send(server, conf, req) 59 if err != nil { 60 return err 61 } 62 return pc.parser.ParseResponse(channel, res) 63 } 64 65 // ConfigResponseParser parses config responses 66 type ConfigResponseParser struct { 67 io.Writer 68 } 69 70 // ParseResponse parses the given response for the given channel 71 func (parser *ConfigResponseParser) ParseResponse(channel string, res ServiceResponse) error { 72 chanConf, err := res.ForChannel(channel).Config() 73 if err != nil { 74 return err 75 } 76 jsonBytes, _ := json.MarshalIndent(chanConf, "", "\t") 77 fmt.Fprintln(parser.Writer, string(jsonBytes)) 78 return nil 79 }