Skip to content

Commit dd9921c

Browse files
committed
[FAB-5602] add channel create outputBlock flag
By default, peer channel create will create a file in the current directory named $CHANNEL.block. If a different output file is desired, the --outputBlock flag can be used to specify the path. Change-Id: I8b0f2ced26c62a21e3139688c588c10763ed533c Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 79c2df7 commit dd9921c

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

peer/channel/channel.go

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
// create related variables
4343
channelID string
4444
channelTxFile string
45+
outputBlock string
4546
timeout int
4647
)
4748

@@ -78,6 +79,7 @@ func resetFlags() {
7879
flags.StringVarP(&genesisBlockPath, "blockpath", "b", common.UndefinedParamValue, "Path to file containing genesis block")
7980
flags.StringVarP(&channelID, "channelID", "c", common.UndefinedParamValue, "In case of a newChain command, the channel ID to create.")
8081
flags.StringVarP(&channelTxFile, "file", "f", "", "Configuration transaction file generated by a tool such as configtxgen for submitting to orderer")
82+
flags.StringVarP(&outputBlock, "outputBlock", "", common.UndefinedParamValue, `The path to write the genesis block for the channel. (default ./<channelID>.block)`)
8183
flags.IntVarP(&timeout, "timeout", "t", 5, "Channel creation timeout")
8284
}
8385

peer/channel/create.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import (
3737
//ConfigTxFileNotFound channel create configuration tx file not found
3838
type ConfigTxFileNotFound string
3939

40-
const createCmdDescription = "Create a channel"
41-
4240
func (e ConfigTxFileNotFound) Error() string {
4341
return fmt.Sprintf("channel create configuration tx file not found %s", string(e))
4442
}
@@ -53,15 +51,16 @@ func (e InvalidCreateTx) Error() string {
5351
func createCmd(cf *ChannelCmdFactory) *cobra.Command {
5452
createCmd := &cobra.Command{
5553
Use: "create",
56-
Short: createCmdDescription,
57-
Long: createCmdDescription,
54+
Short: "Create a channel",
55+
Long: "Create a channel and write the genesis block to a file.",
5856
RunE: func(cmd *cobra.Command, args []string) error {
5957
return create(cmd, args, cf)
6058
},
6159
}
6260
flagList := []string{
6361
"channelID",
6462
"file",
63+
"outputBlock",
6564
"timeout",
6665
}
6766
attachFlags(createCmd, flagList)
@@ -173,14 +172,13 @@ func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
173172
}
174173

175174
func executeCreate(cf *ChannelCmdFactory) error {
176-
var err error
177-
178-
if err = sendCreateChainTransaction(cf); err != nil {
175+
err := sendCreateChainTransaction(cf)
176+
if err != nil {
179177
return err
180178
}
181179

182-
var block *cb.Block
183-
if block, err = getGenesisBlock(cf); err != nil {
180+
block, err := getGenesisBlock(cf)
181+
if err != nil {
184182
return err
185183
}
186184

@@ -190,7 +188,11 @@ func executeCreate(cf *ChannelCmdFactory) error {
190188
}
191189

192190
file := channelID + ".block"
193-
if err = ioutil.WriteFile(file, b, 0644); err != nil {
191+
if outputBlock != common.UndefinedParamValue {
192+
file = outputBlock
193+
}
194+
err = ioutil.WriteFile(file, b, 0644)
195+
if err != nil {
194196
return err
195197
}
196198

@@ -202,6 +204,7 @@ func create(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
202204
if channelID == common.UndefinedParamValue {
203205
return errors.New("Must supply channel ID")
204206
}
207+
205208
// Parsing of the command line is done so silence cmd usage
206209
cmd.SilenceUsage = true
207210

peer/channel/create_test.go

+48-3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,51 @@ func TestCreateChain(t *testing.T) {
178178
t.Fail()
179179
t.Errorf("expected create command to succeed")
180180
}
181+
182+
filename := mockchain + ".block"
183+
if _, err := os.Stat(filename); err != nil {
184+
t.Fail()
185+
t.Errorf("expected %s to exist", filename)
186+
}
187+
}
188+
189+
func TestCreateChainWithOutputBlock(t *testing.T) {
190+
InitMSP()
191+
cleanup := configtest.SetDevFabricConfigPath(t)
192+
defer cleanup()
193+
194+
mockchain := "mockchain"
195+
196+
signer, err := common.GetDefaultSigner()
197+
if err != nil {
198+
t.Fatalf("Get default signer error: %v", err)
199+
}
200+
201+
mockCF := &ChannelCmdFactory{
202+
BroadcastFactory: mockBroadcastClientFactory,
203+
Signer: signer,
204+
DeliverClient: &mockDeliverClient{},
205+
}
206+
207+
cmd := createCmd(mockCF)
208+
AddFlags(cmd)
209+
210+
tempDir, err := ioutil.TempDir("", "create-output")
211+
if err != nil {
212+
t.Fatalf("failed to create temporary directory")
213+
}
214+
defer os.RemoveAll(tempDir)
215+
216+
outputBlockPath := filepath.Join(tempDir, "output.block")
217+
args := []string{"-c", mockchain, "-o", "localhost:7050", "--outputBlock", outputBlockPath}
218+
cmd.SetArgs(args)
219+
defer func() { outputBlock = "" }()
220+
221+
err = cmd.Execute()
222+
assert.NoError(t, err, "execute should succeed")
223+
224+
_, err = os.Stat(outputBlockPath)
225+
assert.NoErrorf(t, err, "expected %s to exist", outputBlockPath)
181226
}
182227

183228
func TestCreateChainWithDefaultAnchorPeers(t *testing.T) {
@@ -398,7 +443,7 @@ func TestCreateChainFromTx(t *testing.T) {
398443
defer cleanup()
399444

400445
mockchannel := "mockchannel"
401-
dir, err := ioutil.TempDir("/tmp", "createtestfromtx-")
446+
dir, err := ioutil.TempDir("", "createtestfromtx-")
402447
if err != nil {
403448
t.Fatalf("couldn't create temp dir")
404449
}
@@ -459,7 +504,7 @@ func TestCreateChainInvalidTx(t *testing.T) {
459504

460505
mockchannel := "mockchannel"
461506

462-
dir, err := ioutil.TempDir("/tmp", "createinvaltest-")
507+
dir, err := ioutil.TempDir("", "createinvaltest-")
463508
if err != nil {
464509
t.Fatalf("couldn't create temp dir")
465510
}
@@ -531,7 +576,7 @@ func TestCreateChainNilCF(t *testing.T) {
531576
defer cleanup()
532577

533578
mockchannel := "mockchannel"
534-
dir, err := ioutil.TempDir("/tmp", "createinvaltest-")
579+
dir, err := ioutil.TempDir("", "createinvaltest-")
535580
assert.NoError(t, err, "Couldn't create temp dir")
536581
defer os.RemoveAll(dir) // clean up
537582

0 commit comments

Comments
 (0)