Skip to content

Commit

Permalink
Add automatic peer command detection
Browse files Browse the repository at this point in the history
Add logic to determine the peer command being run and initialise
logging accordingly rather than hard coding it in each command.

Change-Id: I685db8fbde1019515b6882fc96b1b25a062df44f
Signed-off-by: Julian Carrivick <cjulian@au1.ibm.com>
  • Loading branch information
juliancarrivick-ibm committed Aug 4, 2016
1 parent ab56c33 commit 3f5b2fa
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 22 deletions.
4 changes: 0 additions & 4 deletions peer/chaincode/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package chaincode
import (
"fmt"

"github.com/hyperledger/fabric/flogging"
"github.com/hyperledger/fabric/peer/common"
"github.com/op/go-logging"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -74,7 +73,4 @@ var chaincodeCmd = &cobra.Command{
Use: chainFuncName,
Short: fmt.Sprintf("%s specific commands.", chainFuncName),
Long: fmt.Sprintf("%s specific commands.", chainFuncName),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
flogging.LoggingInit(chainFuncName)
},
}
41 changes: 35 additions & 6 deletions peer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,18 @@ const cmdRoot = "core"
var mainCmd = &cobra.Command{
Use: "peer",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
peerCommand := getPeerCommandFromCobraCommand(cmd)
flogging.LoggingInit(peerCommand)

return core.CacheConfiguration()
},
PreRun: func(cmd *cobra.Command, args []string) {
flogging.LoggingInit("peer")
},
Run: func(cmd *cobra.Command, args []string) {
if versionFlag {
version.Print()
} else {
cmd.HelpFunc()(cmd, args)
}
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

// Peer command version flag
Expand Down Expand Up @@ -127,3 +124,35 @@ func main() {
}
logger.Info("Exiting.....")
}

// getPeerCommandFromCobraCommand retreives the peer command from the cobra command struct.
// i.e. for a command of `peer node start`, this should return "node"
// For the main/root command this will return the root name (i.e. peer)
// For invalid commands (i.e. nil commands) this will return an empty string
func getPeerCommandFromCobraCommand(command *cobra.Command) string {
var commandName string

if command == nil {
return commandName
}

if peerCommand, ok := findChildOfRootCommand(command); ok {
commandName = peerCommand.Name()
} else {
commandName = command.Name()
}

return commandName
}

func findChildOfRootCommand(command *cobra.Command) (*cobra.Command, bool) {
for command.HasParent() {
if !command.Parent().HasParent() {
return command, true
}

command = command.Parent()
}

return nil, false
}
82 changes: 82 additions & 0 deletions peer/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"testing"

"github.com/spf13/cobra"
)

func TestBuildcommandNameOutputSingleCommand(t *testing.T) {
command := &cobra.Command{Use: "command"}

commandNameOutput := getPeerCommandFromCobraCommand(command)

assertEqual(t, "command", commandNameOutput)
}

func TestBuildcommandNameOutputNilCommand(t *testing.T) {
var command *cobra.Command

commandNameOutput := getPeerCommandFromCobraCommand(command)

assertEqual(t, "", commandNameOutput)
}

func TestBuildcommandNameOutputTwoCommands(t *testing.T) {
rootCommand := &cobra.Command{Use: "rootcommand"}
childCommand := &cobra.Command{Use: "childcommand"}
rootCommand.AddCommand(childCommand)

commandNameOutput := getPeerCommandFromCobraCommand(childCommand)

assertEqual(t, "childcommand", commandNameOutput)
}

func TestBuildcommandNameOutputThreeCommands(t *testing.T) {
rootCommand := &cobra.Command{Use: "rootcommand"}
childCommand := &cobra.Command{Use: "childcommand"}
leafCommand := &cobra.Command{Use: "leafCommand"}
rootCommand.AddCommand(childCommand)
childCommand.AddCommand(leafCommand)

commandNameOutput := getPeerCommandFromCobraCommand(leafCommand)

assertEqual(t, "childcommand", commandNameOutput)
}

func TestBuildcommandNameOutputFourCommands(t *testing.T) {
rootCommand := &cobra.Command{Use: "rootcommand"}
childCommand := &cobra.Command{Use: "childcommand"}
secondChildCommand := &cobra.Command{Use: "secondChildCommand"}
leafCommand := &cobra.Command{Use: "leafCommand"}

rootCommand.AddCommand(childCommand)
childCommand.AddCommand(secondChildCommand)
secondChildCommand.AddCommand(leafCommand)

commandNameOutput := getPeerCommandFromCobraCommand(leafCommand)

assertEqual(t, "childcommand", commandNameOutput)
}

func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if expected != actual {
t.Errorf("Expected %v, got %v", expected, actual)
}
}
4 changes: 0 additions & 4 deletions peer/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package network
import (
"fmt"

"github.com/hyperledger/fabric/flogging"
"github.com/op/go-logging"
"github.com/spf13/cobra"
)
Expand All @@ -40,7 +39,4 @@ var networkCmd = &cobra.Command{
Use: networkFuncName,
Short: fmt.Sprintf("%s specific commands.", networkFuncName),
Long: fmt.Sprintf("%s specific commands.", networkFuncName),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
flogging.LoggingInit(networkFuncName)
},
}
4 changes: 0 additions & 4 deletions peer/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package node
import (
"fmt"

"github.com/hyperledger/fabric/flogging"
"github.com/op/go-logging"
"github.com/spf13/cobra"
)
Expand All @@ -44,7 +43,4 @@ var nodeCmd = &cobra.Command{
Use: nodeFuncName,
Short: fmt.Sprintf("%s specific commands.", nodeFuncName),
Long: fmt.Sprintf("%s specific commands.", nodeFuncName),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
flogging.LoggingInit(nodeFuncName)
},
}
4 changes: 0 additions & 4 deletions peer/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package version
import (
"fmt"

"github.com/hyperledger/fabric/flogging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -33,9 +32,6 @@ var cobraCommand = &cobra.Command{
Use: "version",
Short: "Print fabric peer version.",
Long: `Print current version of fabric peer server.`,
PreRun: func(cmd *cobra.Command, args []string) {
flogging.LoggingInit("version")
},
Run: func(cmd *cobra.Command, args []string) {
Print()
},
Expand Down

0 comments on commit 3f5b2fa

Please sign in to comment.