Skip to content

Commit

Permalink
Cleanup peer cli channel commands, messages.
Browse files Browse the repository at this point in the history
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 <bartem@il.ibm.com>
  • Loading branch information
C0rWin committed Mar 27, 2017
1 parent 397f5de commit 6b77c53
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
10 changes: 7 additions & 3 deletions peer/channel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}
Expand All @@ -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)
},
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions peer/channel/fetchconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
13 changes: 4 additions & 9 deletions peer/channel/fetchconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/hyperledger/fabric/peer/common"
"github.com/stretchr/testify/assert"
)

func TestFetchChain(t *testing.T) {
Expand All @@ -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")

Expand All @@ -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()
}
}
24 changes: 12 additions & 12 deletions peer/channel/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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"]),
Expand All @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions peer/channel/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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")
}
}

0 comments on commit 6b77c53

Please sign in to comment.