Skip to content

Commit 6b77c53

Browse files
committed
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 <[email protected]>
1 parent 397f5de commit 6b77c53

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

peer/channel/create.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"io/ioutil"
2222
"time"
2323

24+
"errors"
25+
2426
"github.com/golang/protobuf/proto"
2527
"github.com/hyperledger/fabric/common/configtx"
2628
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
@@ -35,6 +37,8 @@ import (
3537
//ConfigTxFileNotFound channel create configuration tx file not found
3638
type ConfigTxFileNotFound string
3739

40+
const createCmdDescription = "Create a channel"
41+
3842
func (e ConfigTxFileNotFound) Error() string {
3943
return fmt.Sprintf("channel create configuration tx file not found %s", string(e))
4044
}
@@ -49,8 +53,8 @@ func (e InvalidCreateTx) Error() string {
4953
func createCmd(cf *ChannelCmdFactory) *cobra.Command {
5054
createCmd := &cobra.Command{
5155
Use: "create",
52-
Short: "Create a chain.",
53-
Long: `Create a chain.`,
56+
Short: createCmdDescription,
57+
Long: createCmdDescription,
5458
RunE: func(cmd *cobra.Command, args []string) error {
5559
return create(cmd, args, cf)
5660
},
@@ -165,7 +169,7 @@ func executeCreate(cf *ChannelCmdFactory) error {
165169
func create(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
166170
//the global chainID filled by the "-c" command
167171
if chainID == common.UndefinedParamValue {
168-
return fmt.Errorf("Must supply channel ID .\n")
172+
return errors.New("Must supply channel ID")
169173
}
170174

171175
var err error

peer/channel/fetchconfig.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ import (
2424
"github.com/spf13/cobra"
2525
)
2626

27+
const fetchCmdDescription = "Fetch configuration block."
28+
2729
func fetchCmd(cf *ChannelCmdFactory) *cobra.Command {
2830
createCmd := &cobra.Command{
2931
Use: "fetch",
30-
Short: "Fetch configuration block.",
31-
Long: `Fetch configuration block.`,
32+
Short: fetchCmdDescription,
33+
Long: fetchCmdDescription,
3234
RunE: func(cmd *cobra.Command, args []string) error {
3335
return fetch(cmd, args, cf)
3436
},

peer/channel/fetchconfig_test.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/hyperledger/fabric/peer/common"
24+
"github.com/stretchr/testify/assert"
2425
)
2526

2627
func TestFetchChain(t *testing.T) {
@@ -46,10 +47,7 @@ func TestFetchChain(t *testing.T) {
4647
args := []string{"-c", mockchain}
4748
cmd.SetArgs(args)
4849

49-
if err := cmd.Execute(); err != nil {
50-
t.Fail()
51-
t.Errorf("expected join command to succeed")
52-
}
50+
assert.NoError(t, cmd.Execute(), "Join command expected to succeed")
5351

5452
os.Remove(mockchain + ".block")
5553

@@ -61,14 +59,11 @@ func TestFetchChain(t *testing.T) {
6159
args = []string{"-c", mockchain}
6260
cmd.SetArgs(args)
6361

64-
if err := cmd.Execute(); err != nil {
65-
t.Fail()
66-
t.Errorf("expected join command to succeed")
67-
}
62+
assert.NoError(t, cmd.Execute(), "Join command expected to succeed")
6863

6964
if _, err := os.Stat(mockchain + ".block"); os.IsNotExist(err) {
7065
// path/to/whatever does not exist
71-
t.Fail()
7266
t.Error("expected configuration block to be fetched")
67+
t.Fail()
7368
}
7469
}

peer/channel/join.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ limitations under the License.
1717
package channel
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"io/ioutil"
2223

24+
"github.com/hyperledger/fabric/core/scc/cscc"
2325
"github.com/hyperledger/fabric/peer/common"
2426
pcommon "github.com/hyperledger/fabric/protos/common"
2527
pb "github.com/hyperledger/fabric/protos/peer"
@@ -28,20 +30,18 @@ import (
2830
"golang.org/x/net/context"
2931
)
3032

31-
const joinFuncName = "join"
33+
const commandDescription = "Joins the peer to a chain."
3234

3335
func joinCmd(cf *ChannelCmdFactory) *cobra.Command {
3436
// Set the flags on the channel start command.
35-
36-
channelJoinCmd := &cobra.Command{
37+
return &cobra.Command{
3738
Use: "join",
38-
Short: "Joins the peer to a chain.",
39-
Long: `Joins the peer to a chain.`,
39+
Short: commandDescription,
40+
Long: commandDescription,
4041
RunE: func(cmd *cobra.Command, args []string) error {
4142
return join(cmd, args, cf)
4243
},
4344
}
44-
return channelJoinCmd
4545
}
4646

4747
//GBFileNotFoundErr genesis block file not found
@@ -60,7 +60,7 @@ func (e ProposalFailedErr) Error() string {
6060

6161
func getJoinCCSpec() (*pb.ChaincodeSpec, error) {
6262
if genesisBlockPath == common.UndefinedParamValue {
63-
return nil, fmt.Errorf("Must supply genesis block file.\n")
63+
return nil, errors.New("Must supply genesis block file.")
6464
}
6565

6666
gb, err := ioutil.ReadFile(genesisBlockPath)
@@ -71,7 +71,7 @@ func getJoinCCSpec() (*pb.ChaincodeSpec, error) {
7171
spec := &pb.ChaincodeSpec{}
7272

7373
// Build the spec
74-
input := &pb.ChaincodeInput{Args: [][]byte{[]byte("JoinChain"), gb}}
74+
input := &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.JoinChain), gb}}
7575

7676
spec = &pb.ChaincodeSpec{
7777
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
@@ -93,19 +93,19 @@ func executeJoin(cf *ChannelCmdFactory) (err error) {
9393

9494
creator, err := cf.Signer.Serialize()
9595
if err != nil {
96-
return fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err)
96+
return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
9797
}
9898

9999
var prop *pb.Proposal
100100
prop, _, err = putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator)
101101
if err != nil {
102-
return fmt.Errorf("Error creating proposal for join %s\n", err)
102+
return fmt.Errorf("Error creating proposal for join %s", err)
103103
}
104104

105105
var signedProp *pb.SignedProposal
106106
signedProp, err = putils.GetSignedProposal(prop, cf.Signer)
107107
if err != nil {
108-
return fmt.Errorf("Error creating signed proposal %s\n", err)
108+
return fmt.Errorf("Error creating signed proposal %s", err)
109109
}
110110

111111
var proposalResp *pb.ProposalResponse
@@ -122,7 +122,7 @@ func executeJoin(cf *ChannelCmdFactory) (err error) {
122122
return ProposalFailedErr(fmt.Sprintf("bad proposal response %d", proposalResp.Response.Status))
123123
}
124124

125-
fmt.Printf("Join Result: %s\n", string(proposalResp.Response.Payload))
125+
fmt.Println("Peer joined the channel!")
126126

127127
return nil
128128
}

peer/channel/join_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestJoin(t *testing.T) {
5858

5959
if err := cmd.Execute(); err != nil {
6060
t.Fail()
61-
t.Errorf("expected join command to succeed")
61+
t.Error("expected join command to succeed")
6262
}
6363
}
6464

@@ -92,10 +92,10 @@ func TestJoinNonExistentBlock(t *testing.T) {
9292

9393
if err := cmd.Execute(); err == nil {
9494
t.Fail()
95-
t.Errorf("expected join command to fail")
95+
t.Error("expected join command to fail")
9696
} else if err, _ = err.(GBFileNotFoundErr); err == nil {
9797
t.Fail()
98-
t.Errorf("expected file not found error")
98+
t.Error("expected file not found error")
9999
}
100100
}
101101

@@ -132,9 +132,9 @@ func TestBadProposalResponse(t *testing.T) {
132132

133133
if err := cmd.Execute(); err == nil {
134134
t.Fail()
135-
t.Errorf("expected join command to fail")
135+
t.Error("expected join command to fail")
136136
} else if err, _ = err.(ProposalFailedErr); err == nil {
137137
t.Fail()
138-
t.Errorf("expected proposal failure error")
138+
t.Error("expected proposal failure error")
139139
}
140140
}

0 commit comments

Comments
 (0)