github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/chaincode/chaincode.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package chaincode 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/common/flogging" 23 "github.com/hyperledger/fabric/common/util" 24 "github.com/hyperledger/fabric/peer/common" 25 "github.com/spf13/cobra" 26 "github.com/spf13/pflag" 27 ) 28 29 const ( 30 chainFuncName = "chaincode" 31 shortDes = "Operate a chaincode: install|instantiate|invoke|package|query|signpackage|upgrade." 32 longDes = "Operate a chaincode: install|instantiate|invoke|package|query|signpackage|upgrade." 33 ) 34 35 var logger = flogging.MustGetLogger("chaincodeCmd") 36 37 func addFlags(cmd *cobra.Command) { 38 flags := cmd.PersistentFlags() 39 40 flags.StringVarP(&orderingEndpoint, "orderer", "o", "", "Ordering service endpoint") 41 flags.BoolVarP(&tls, "tls", "", false, "Use TLS when communicating with the orderer endpoint") 42 flags.StringVarP(&caFile, "cafile", "", "", "Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint") 43 } 44 45 // Cmd returns the cobra command for Chaincode 46 func Cmd(cf *ChaincodeCmdFactory) *cobra.Command { 47 addFlags(chaincodeCmd) 48 49 chaincodeCmd.AddCommand(installCmd(cf)) 50 chaincodeCmd.AddCommand(instantiateCmd(cf)) 51 chaincodeCmd.AddCommand(invokeCmd(cf)) 52 chaincodeCmd.AddCommand(packageCmd(cf, nil)) 53 chaincodeCmd.AddCommand(queryCmd(cf)) 54 chaincodeCmd.AddCommand(signpackageCmd(cf)) 55 chaincodeCmd.AddCommand(upgradeCmd(cf)) 56 57 return chaincodeCmd 58 } 59 60 // Chaincode-related variables. 61 var ( 62 chaincodeLang string 63 chaincodeCtorJSON string 64 chaincodePath string 65 chaincodeName string 66 chaincodeUsr string // Not used 67 chaincodeQueryRaw bool 68 chaincodeQueryHex bool 69 customIDGenAlg string 70 chainID string 71 chaincodeVersion string 72 policy string 73 escc string 74 vscc string 75 policyMarhsalled []byte 76 orderingEndpoint string 77 tls bool 78 caFile string 79 ) 80 81 var chaincodeCmd = &cobra.Command{ 82 Use: chainFuncName, 83 Short: fmt.Sprint(shortDes), 84 Long: fmt.Sprint(longDes), 85 } 86 87 var flags *pflag.FlagSet 88 89 func init() { 90 resetFlags() 91 } 92 93 // Explicitly define a method to facilitate tests 94 func resetFlags() { 95 flags = &pflag.FlagSet{} 96 97 flags.StringVarP(&chaincodeLang, "lang", "l", "golang", 98 fmt.Sprintf("Language the %s is written in", chainFuncName)) 99 flags.StringVarP(&chaincodeCtorJSON, "ctor", "c", "{}", 100 fmt.Sprintf("Constructor message for the %s in JSON format", chainFuncName)) 101 flags.StringVarP(&chaincodePath, "path", "p", common.UndefinedParamValue, 102 fmt.Sprintf("Path to %s", chainFuncName)) 103 flags.StringVarP(&chaincodeName, "name", "n", common.UndefinedParamValue, 104 fmt.Sprint("Name of the chaincode")) 105 flags.StringVarP(&chaincodeVersion, "version", "v", common.UndefinedParamValue, 106 fmt.Sprint("Version of the chaincode specified in install/instantiate/upgrade commands")) 107 flags.StringVarP(&chaincodeUsr, "username", "u", common.UndefinedParamValue, 108 fmt.Sprint("Username for chaincode operations when security is enabled")) 109 flags.StringVarP(&customIDGenAlg, "tid", "t", common.UndefinedParamValue, 110 fmt.Sprint("Name of a custom ID generation algorithm (hashing and decoding) e.g. sha256base64")) 111 flags.StringVarP(&chainID, "channelID", "C", util.GetTestChainID(), 112 fmt.Sprint("The channel on which this command should be executed")) 113 flags.StringVarP(&policy, "policy", "P", common.UndefinedParamValue, 114 fmt.Sprint("The endorsement policy associated to this chaincode")) 115 flags.StringVarP(&escc, "escc", "E", common.UndefinedParamValue, 116 fmt.Sprint("The name of the endorsement system chaincode to be used for this chaincode")) 117 flags.StringVarP(&vscc, "vscc", "V", common.UndefinedParamValue, 118 fmt.Sprint("The name of the verification system chaincode to be used for this chaincode")) 119 } 120 121 func attachFlags(cmd *cobra.Command, names []string) { 122 cmdFlags := cmd.Flags() 123 for _, name := range names { 124 if flag := flags.Lookup(name); flag != nil { 125 cmdFlags.AddFlag(flag) 126 } else { 127 logger.Fatalf("Could not find flag '%s' to attach to commond '%s'", name, cmd.Name()) 128 } 129 } 130 }