github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/channel/fetchconfig.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 channel
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"strconv"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	cb "github.com/hyperledger/fabric/protos/common"
    26  	"github.com/hyperledger/fabric/protos/utils"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  func fetchCmd(cf *ChannelCmdFactory) *cobra.Command {
    31  	fetchCmd := &cobra.Command{
    32  		Use:   "fetch <newest|oldest|config|(number)> [outputfile]",
    33  		Short: "Fetch a block",
    34  		Long:  "Fetch a specified block, writing it to a file.",
    35  		RunE: func(cmd *cobra.Command, args []string) error {
    36  			return fetch(cmd, args, cf)
    37  		},
    38  	}
    39  	flagList := []string{
    40  		"channelID",
    41  	}
    42  	attachFlags(fetchCmd, flagList)
    43  
    44  	return fetchCmd
    45  }
    46  
    47  func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
    48  	var err error
    49  	if cf == nil {
    50  		cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	if len(args) == 0 {
    57  		return fmt.Errorf("fetch target required, oldest, newest, config, or a number")
    58  	}
    59  
    60  	if len(args) > 2 {
    61  		return fmt.Errorf("trailing args detected")
    62  	}
    63  
    64  	var block *cb.Block
    65  
    66  	switch args[0] {
    67  	case "oldest":
    68  		block, err = cf.DeliverClient.getOldestBlock()
    69  	case "newest":
    70  		block, err = cf.DeliverClient.getNewestBlock()
    71  	case "config":
    72  		iBlock, err := cf.DeliverClient.getNewestBlock()
    73  		if err != nil {
    74  			return err
    75  		}
    76  		lc, err := utils.GetLastConfigIndexFromBlock(iBlock)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		block, err = cf.DeliverClient.getSpecifiedBlock(lc)
    81  	default:
    82  		num, err := strconv.Atoi(args[0])
    83  		if err != nil {
    84  			return fmt.Errorf("fetch target illegal: %s", args[0])
    85  		}
    86  		block, err = cf.DeliverClient.getSpecifiedBlock(uint64(num))
    87  	}
    88  
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	b, err := proto.Marshal(block)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	var file string
    99  	if len(args) == 1 {
   100  		file = chainID + "_" + args[0] + ".block"
   101  	} else {
   102  		file = args[1]
   103  	}
   104  
   105  	if err = ioutil.WriteFile(file, b, 0644); err != nil {
   106  		return err
   107  	}
   108  
   109  	return nil
   110  }