github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/chaincode/invoke.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  	"fmt"
    11  
    12  	"github.com/hyperledger/fabric/bccsp"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var chaincodeInvokeCmd *cobra.Command
    18  
    19  // invokeCmd returns the cobra command for Chaincode Invoke
    20  func invokeCmd(cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) *cobra.Command {
    21  	chaincodeInvokeCmd = &cobra.Command{
    22  		Use:       "invoke",
    23  		Short:     fmt.Sprintf("Invoke the specified %s.", chainFuncName),
    24  		Long:      fmt.Sprintf("Invoke the specified %s. It will try to commit the endorsed transaction to the network.", chainFuncName),
    25  		ValidArgs: []string{"1"},
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			return chaincodeInvoke(cmd, cf, cryptoProvider)
    28  		},
    29  	}
    30  	flagList := []string{
    31  		"name",
    32  		"ctor",
    33  		"isInit",
    34  		"channelID",
    35  		"peerAddresses",
    36  		"tlsRootCertFiles",
    37  		"connectionProfile",
    38  		"waitForEvent",
    39  		"waitForEventTimeout",
    40  	}
    41  	attachFlags(chaincodeInvokeCmd, flagList)
    42  
    43  	return chaincodeInvokeCmd
    44  }
    45  
    46  func chaincodeInvoke(cmd *cobra.Command, cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) error {
    47  	if channelID == "" {
    48  		return errors.New("The required parameter 'channelID' is empty. Rerun the command with -C flag")
    49  	}
    50  	// Parsing of the command line is done so silence cmd usage
    51  	cmd.SilenceUsage = true
    52  
    53  	var err error
    54  	if cf == nil {
    55  		cf, err = InitCmdFactory(cmd.Name(), true, true, cryptoProvider)
    56  		if err != nil {
    57  			return err
    58  		}
    59  	}
    60  	defer cf.BroadcastClient.Close()
    61  
    62  	return chaincodeInvokeOrQuery(cmd, true, cf)
    63  }