github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/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/inklabsfoundation/inkchain/common/flogging" 23 "github.com/inklabsfoundation/inkchain/common/util" 24 "github.com/inklabsfoundation/inkchain/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 flags.StringVarP(&priKey, "priKey", "z", "", "Private key for signing Invoke") 44 flags.StringVarP(&feeLimit, "feeLimit", "i", "0", "feeLimit") 45 flags.StringVarP(&msg, "msg", "m", "", "message for chaincode invocation") 46 } 47 48 // Cmd returns the cobra command for Chaincode 49 func Cmd(cf *ChaincodeCmdFactory) *cobra.Command { 50 addFlags(chaincodeCmd) 51 52 chaincodeCmd.AddCommand(installCmd(cf)) 53 chaincodeCmd.AddCommand(instantiateCmd(cf)) 54 chaincodeCmd.AddCommand(invokeCmd(cf)) 55 chaincodeCmd.AddCommand(packageCmd(cf, nil)) 56 chaincodeCmd.AddCommand(queryCmd(cf)) 57 chaincodeCmd.AddCommand(signpackageCmd(cf)) 58 chaincodeCmd.AddCommand(upgradeCmd(cf)) 59 60 return chaincodeCmd 61 } 62 63 // Chaincode-related variables. 64 var ( 65 chaincodeLang string 66 chaincodeCtorJSON string 67 chaincodePath string 68 chaincodeName string 69 chaincodeUsr string // Not used 70 chaincodeQueryRaw bool 71 chaincodeQueryHex bool 72 customIDGenAlg string 73 chainID string 74 chaincodeVersion string 75 policy string 76 escc string 77 vscc string 78 policyMarhsalled []byte 79 orderingEndpoint string 80 tls bool 81 caFile string 82 priKey string 83 feeLimit string 84 msg string 85 ) 86 87 var chaincodeCmd = &cobra.Command{ 88 Use: chainFuncName, 89 Short: fmt.Sprint(shortDes), 90 Long: fmt.Sprint(longDes), 91 } 92 93 var flags *pflag.FlagSet 94 95 func init() { 96 resetFlags() 97 } 98 99 // Explicitly define a method to facilitate tests 100 func resetFlags() { 101 flags = &pflag.FlagSet{} 102 103 flags.StringVarP(&chaincodeLang, "lang", "l", "golang", 104 fmt.Sprintf("Language the %s is written in", chainFuncName)) 105 flags.StringVarP(&chaincodeCtorJSON, "ctor", "c", "{}", 106 fmt.Sprintf("Constructor message for the %s in JSON format", chainFuncName)) 107 flags.StringVarP(&chaincodePath, "path", "p", common.UndefinedParamValue, 108 fmt.Sprintf("Path to %s", chainFuncName)) 109 flags.StringVarP(&chaincodeName, "name", "n", common.UndefinedParamValue, 110 fmt.Sprint("Name of the chaincode")) 111 flags.StringVarP(&chaincodeVersion, "version", "v", common.UndefinedParamValue, 112 fmt.Sprint("Version of the chaincode specified in install/instantiate/upgrade commands")) 113 flags.StringVarP(&chaincodeUsr, "username", "u", common.UndefinedParamValue, 114 fmt.Sprint("Username for chaincode operations when security is enabled")) 115 flags.StringVarP(&customIDGenAlg, "tid", "t", common.UndefinedParamValue, 116 fmt.Sprint("Name of a custom ID generation algorithm (hashing and decoding) e.g. sha256base64")) 117 flags.StringVarP(&chainID, "channelID", "C", util.GetTestChainID(), 118 fmt.Sprint("The channel on which this command should be executed")) 119 flags.StringVarP(&policy, "policy", "P", common.UndefinedParamValue, 120 fmt.Sprint("The endorsement policy associated to this chaincode")) 121 flags.StringVarP(&escc, "escc", "E", common.UndefinedParamValue, 122 fmt.Sprint("The name of the endorsement system chaincode to be used for this chaincode")) 123 flags.StringVarP(&vscc, "vscc", "V", common.UndefinedParamValue, 124 fmt.Sprint("The name of the verification system chaincode to be used for this chaincode")) 125 } 126 127 func attachFlags(cmd *cobra.Command, names []string) { 128 cmdFlags := cmd.Flags() 129 for _, name := range names { 130 if flag := flags.Lookup(name); flag != nil { 131 cmdFlags.AddFlag(flag) 132 } else { 133 logger.Fatalf("Could not find flag '%s' to attach to commond '%s'", name, cmd.Name()) 134 } 135 } 136 }