github.com/defanghe/fabric@v2.1.1+incompatible/internal/peer/chaincode/query.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "errors" 11 "fmt" 12 13 "github.com/hyperledger/fabric/bccsp" 14 "github.com/spf13/cobra" 15 ) 16 17 var chaincodeQueryCmd *cobra.Command 18 19 // queryCmd returns the cobra command for Chaincode Query 20 func queryCmd(cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) *cobra.Command { 21 chaincodeQueryCmd = &cobra.Command{ 22 Use: "query", 23 Short: fmt.Sprintf("Query using the specified %s.", chainFuncName), 24 Long: fmt.Sprintf("Get endorsed result of %s function call and print it. It won't generate transaction.", chainFuncName), 25 ValidArgs: []string{"1"}, 26 RunE: func(cmd *cobra.Command, args []string) error { 27 return chaincodeQuery(cmd, cf, cryptoProvider) 28 }, 29 } 30 flagList := []string{ 31 "ctor", 32 "name", 33 "channelID", 34 "peerAddresses", 35 "tlsRootCertFiles", 36 "connectionProfile", 37 } 38 attachFlags(chaincodeQueryCmd, flagList) 39 40 chaincodeQueryCmd.Flags().BoolVarP(&chaincodeQueryRaw, "raw", "r", false, 41 "If true, output the query value as raw bytes, otherwise format as a printable string") 42 chaincodeQueryCmd.Flags().BoolVarP(&chaincodeQueryHex, "hex", "x", false, 43 "If true, output the query value byte array in hexadecimal. Incompatible with --raw") 44 45 return chaincodeQueryCmd 46 } 47 48 func chaincodeQuery(cmd *cobra.Command, cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) error { 49 if channelID == "" { 50 return errors.New("The required parameter 'channelID' is empty. Rerun the command with -C flag") 51 } 52 // Parsing of the command line is done so silence cmd usage 53 cmd.SilenceUsage = true 54 55 var err error 56 if cf == nil { 57 cf, err = InitCmdFactory(cmd.Name(), true, false, cryptoProvider) 58 if err != nil { 59 return err 60 } 61 } 62 63 return chaincodeInvokeOrQuery(cmd, false, cf) 64 }