From 6b77c539a5bb67f5f984de88947f3ab6f8f993b8 Mon Sep 17 00:00:00 2001 From: Artem Barger Date: Fri, 24 Mar 2017 00:43:05 +0200 Subject: [PATCH] Cleanup peer cli channel commands, messages. Remove unused variable and fix print out warning, do not use formatting where no need for this and stop using '\n' in errors messages. Change-Id: I8b094f2a358010b7258bb6a4546131f581d791bc Signed-off-by: Artem Barger --- peer/channel/create.go | 10 +++++++--- peer/channel/fetchconfig.go | 6 ++++-- peer/channel/fetchconfig_test.go | 13 ++++--------- peer/channel/join.go | 24 ++++++++++++------------ peer/channel/join_test.go | 10 +++++----- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/peer/channel/create.go b/peer/channel/create.go index 16733be0b00..97efc1fd1ac 100644 --- a/peer/channel/create.go +++ b/peer/channel/create.go @@ -21,6 +21,8 @@ import ( "io/ioutil" "time" + "errors" + "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric/common/configtx" configtxtest "github.com/hyperledger/fabric/common/configtx/test" @@ -35,6 +37,8 @@ import ( //ConfigTxFileNotFound channel create configuration tx file not found type ConfigTxFileNotFound string +const createCmdDescription = "Create a channel" + func (e ConfigTxFileNotFound) Error() string { return fmt.Sprintf("channel create configuration tx file not found %s", string(e)) } @@ -49,8 +53,8 @@ func (e InvalidCreateTx) Error() string { func createCmd(cf *ChannelCmdFactory) *cobra.Command { createCmd := &cobra.Command{ Use: "create", - Short: "Create a chain.", - Long: `Create a chain.`, + Short: createCmdDescription, + Long: createCmdDescription, RunE: func(cmd *cobra.Command, args []string) error { return create(cmd, args, cf) }, @@ -165,7 +169,7 @@ func executeCreate(cf *ChannelCmdFactory) error { func create(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error { //the global chainID filled by the "-c" command if chainID == common.UndefinedParamValue { - return fmt.Errorf("Must supply channel ID .\n") + return errors.New("Must supply channel ID") } var err error diff --git a/peer/channel/fetchconfig.go b/peer/channel/fetchconfig.go index 28d96b48fa7..b1f9fec3959 100644 --- a/peer/channel/fetchconfig.go +++ b/peer/channel/fetchconfig.go @@ -24,11 +24,13 @@ import ( "github.com/spf13/cobra" ) +const fetchCmdDescription = "Fetch configuration block." + func fetchCmd(cf *ChannelCmdFactory) *cobra.Command { createCmd := &cobra.Command{ Use: "fetch", - Short: "Fetch configuration block.", - Long: `Fetch configuration block.`, + Short: fetchCmdDescription, + Long: fetchCmdDescription, RunE: func(cmd *cobra.Command, args []string) error { return fetch(cmd, args, cf) }, diff --git a/peer/channel/fetchconfig_test.go b/peer/channel/fetchconfig_test.go index 6b154fe3f57..6e5540bb68b 100644 --- a/peer/channel/fetchconfig_test.go +++ b/peer/channel/fetchconfig_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/hyperledger/fabric/peer/common" + "github.com/stretchr/testify/assert" ) func TestFetchChain(t *testing.T) { @@ -46,10 +47,7 @@ func TestFetchChain(t *testing.T) { args := []string{"-c", mockchain} cmd.SetArgs(args) - if err := cmd.Execute(); err != nil { - t.Fail() - t.Errorf("expected join command to succeed") - } + assert.NoError(t, cmd.Execute(), "Join command expected to succeed") os.Remove(mockchain + ".block") @@ -61,14 +59,11 @@ func TestFetchChain(t *testing.T) { args = []string{"-c", mockchain} cmd.SetArgs(args) - if err := cmd.Execute(); err != nil { - t.Fail() - t.Errorf("expected join command to succeed") - } + assert.NoError(t, cmd.Execute(), "Join command expected to succeed") if _, err := os.Stat(mockchain + ".block"); os.IsNotExist(err) { // path/to/whatever does not exist - t.Fail() t.Error("expected configuration block to be fetched") + t.Fail() } } diff --git a/peer/channel/join.go b/peer/channel/join.go index 1d1653ac3b1..846f105b8bc 100644 --- a/peer/channel/join.go +++ b/peer/channel/join.go @@ -17,9 +17,11 @@ limitations under the License. package channel import ( + "errors" "fmt" "io/ioutil" + "github.com/hyperledger/fabric/core/scc/cscc" "github.com/hyperledger/fabric/peer/common" pcommon "github.com/hyperledger/fabric/protos/common" pb "github.com/hyperledger/fabric/protos/peer" @@ -28,20 +30,18 @@ import ( "golang.org/x/net/context" ) -const joinFuncName = "join" +const commandDescription = "Joins the peer to a chain." func joinCmd(cf *ChannelCmdFactory) *cobra.Command { // Set the flags on the channel start command. - - channelJoinCmd := &cobra.Command{ + return &cobra.Command{ Use: "join", - Short: "Joins the peer to a chain.", - Long: `Joins the peer to a chain.`, + Short: commandDescription, + Long: commandDescription, RunE: func(cmd *cobra.Command, args []string) error { return join(cmd, args, cf) }, } - return channelJoinCmd } //GBFileNotFoundErr genesis block file not found @@ -60,7 +60,7 @@ func (e ProposalFailedErr) Error() string { func getJoinCCSpec() (*pb.ChaincodeSpec, error) { if genesisBlockPath == common.UndefinedParamValue { - return nil, fmt.Errorf("Must supply genesis block file.\n") + return nil, errors.New("Must supply genesis block file.") } gb, err := ioutil.ReadFile(genesisBlockPath) @@ -71,7 +71,7 @@ func getJoinCCSpec() (*pb.ChaincodeSpec, error) { spec := &pb.ChaincodeSpec{} // Build the spec - input := &pb.ChaincodeInput{Args: [][]byte{[]byte("JoinChain"), gb}} + input := &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.JoinChain), gb}} spec = &pb.ChaincodeSpec{ Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), @@ -93,19 +93,19 @@ func executeJoin(cf *ChannelCmdFactory) (err error) { creator, err := cf.Signer.Serialize() if err != nil { - return fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err) + return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) } var prop *pb.Proposal prop, _, err = putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator) if err != nil { - return fmt.Errorf("Error creating proposal for join %s\n", err) + return fmt.Errorf("Error creating proposal for join %s", err) } var signedProp *pb.SignedProposal signedProp, err = putils.GetSignedProposal(prop, cf.Signer) if err != nil { - return fmt.Errorf("Error creating signed proposal %s\n", err) + return fmt.Errorf("Error creating signed proposal %s", err) } var proposalResp *pb.ProposalResponse @@ -122,7 +122,7 @@ func executeJoin(cf *ChannelCmdFactory) (err error) { return ProposalFailedErr(fmt.Sprintf("bad proposal response %d", proposalResp.Response.Status)) } - fmt.Printf("Join Result: %s\n", string(proposalResp.Response.Payload)) + fmt.Println("Peer joined the channel!") return nil } diff --git a/peer/channel/join_test.go b/peer/channel/join_test.go index 20896992e14..84f145e88d4 100644 --- a/peer/channel/join_test.go +++ b/peer/channel/join_test.go @@ -58,7 +58,7 @@ func TestJoin(t *testing.T) { if err := cmd.Execute(); err != nil { t.Fail() - t.Errorf("expected join command to succeed") + t.Error("expected join command to succeed") } } @@ -92,10 +92,10 @@ func TestJoinNonExistentBlock(t *testing.T) { if err := cmd.Execute(); err == nil { t.Fail() - t.Errorf("expected join command to fail") + t.Error("expected join command to fail") } else if err, _ = err.(GBFileNotFoundErr); err == nil { t.Fail() - t.Errorf("expected file not found error") + t.Error("expected file not found error") } } @@ -132,9 +132,9 @@ func TestBadProposalResponse(t *testing.T) { if err := cmd.Execute(); err == nil { t.Fail() - t.Errorf("expected join command to fail") + t.Error("expected join command to fail") } else if err, _ = err.(ProposalFailedErr); err == nil { t.Fail() - t.Errorf("expected proposal failure error") + t.Error("expected proposal failure error") } }