github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/peer/chaincode/package.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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  	"io/ioutil"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  const package_desc = "Package the specified chaincode into a deployment spec."
    29  
    30  // deployCmd returns the cobra command for Chaincode Deploy
    31  func packageCmd(cf *ChaincodeCmdFactory) *cobra.Command {
    32  	chaincodeInstantiateCmd = &cobra.Command{
    33  		Use:       "package",
    34  		Short:     package_desc,
    35  		Long:      package_desc,
    36  		ValidArgs: []string{"1"},
    37  		RunE: func(cmd *cobra.Command, args []string) error {
    38  			return chaincodePackage(cmd, args, cf)
    39  		},
    40  	}
    41  
    42  	return chaincodeInstantiateCmd
    43  }
    44  
    45  // chaincodeDeploy deploys the chaincode. On success, the chaincode name
    46  // (hash) is printed to STDOUT for use by subsequent chaincode-related CLI
    47  // commands.
    48  func chaincodePackage(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error {
    49  	var err error
    50  	spec, err := getChaincodeSpecification(cmd)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	cds, err := getChaincodeBytes(spec, true)
    56  	if err != nil {
    57  		return fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err)
    58  	}
    59  
    60  	cdsBytes, err := proto.Marshal(cds)
    61  	if err != nil {
    62  		return fmt.Errorf("Error marshalling chaincode deployment spec : %s", err)
    63  	}
    64  	logger.Debugf("Packaged chaincode into deployment spec of size <%d>, with args = %v", len(cdsBytes), args)
    65  	fileToWrite := args[0]
    66  	err = ioutil.WriteFile(fileToWrite, cdsBytes, 0700)
    67  	if err != nil {
    68  		logger.Errorf("Failed writing deployment spec to file [%s]: [%s]", fileToWrite, err)
    69  		return err
    70  	}
    71  
    72  	return err
    73  }