From 3f5b2fab1f4b254068e036d64c16bff436c0966c Mon Sep 17 00:00:00 2001 From: Julian Carrivick Date: Wed, 27 Jul 2016 11:10:16 +0800 Subject: [PATCH] Add automatic peer command detection 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 --- peer/chaincode/chaincode.go | 4 -- peer/main.go | 41 ++++++++++++++++--- peer/main_test.go | 82 +++++++++++++++++++++++++++++++++++++ peer/network/network.go | 4 -- peer/node/node.go | 4 -- peer/version/version.go | 4 -- 6 files changed, 117 insertions(+), 22 deletions(-) create mode 100755 peer/main_test.go diff --git a/peer/chaincode/chaincode.go b/peer/chaincode/chaincode.go index 453e0eba844..8e420c5b835 100755 --- a/peer/chaincode/chaincode.go +++ b/peer/chaincode/chaincode.go @@ -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" @@ -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) - }, } diff --git a/peer/main.go b/peer/main.go index d0b66f5b137..36d7769e0b0 100644 --- a/peer/main.go +++ b/peer/main.go @@ -49,11 +49,11 @@ 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() @@ -61,9 +61,6 @@ var mainCmd = &cobra.Command{ cmd.HelpFunc()(cmd, args) } }, - PersistentPostRunE: func(cmd *cobra.Command, args []string) error { - return nil - }, } // Peer command version flag @@ -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 +} diff --git a/peer/main_test.go b/peer/main_test.go new file mode 100755 index 00000000000..17b48ee3094 --- /dev/null +++ b/peer/main_test.go @@ -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) + } +} diff --git a/peer/network/network.go b/peer/network/network.go index 879f8b9b682..d23d1a18c9f 100755 --- a/peer/network/network.go +++ b/peer/network/network.go @@ -19,7 +19,6 @@ package network import ( "fmt" - "github.com/hyperledger/fabric/flogging" "github.com/op/go-logging" "github.com/spf13/cobra" ) @@ -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) - }, } diff --git a/peer/node/node.go b/peer/node/node.go index e5d798857a2..9f544bfd0d9 100755 --- a/peer/node/node.go +++ b/peer/node/node.go @@ -19,7 +19,6 @@ package node import ( "fmt" - "github.com/hyperledger/fabric/flogging" "github.com/op/go-logging" "github.com/spf13/cobra" ) @@ -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) - }, } diff --git a/peer/version/version.go b/peer/version/version.go index c8964156669..5c3190ec892 100755 --- a/peer/version/version.go +++ b/peer/version/version.go @@ -19,7 +19,6 @@ package version import ( "fmt" - "github.com/hyperledger/fabric/flogging" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -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() },