Skip to content

Commit 3f5b2fa

Browse files
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 <[email protected]>
1 parent ab56c33 commit 3f5b2fa

File tree

6 files changed

+117
-22
lines changed

6 files changed

+117
-22
lines changed

peer/chaincode/chaincode.go

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package chaincode
1919
import (
2020
"fmt"
2121

22-
"github.com/hyperledger/fabric/flogging"
2322
"github.com/hyperledger/fabric/peer/common"
2423
"github.com/op/go-logging"
2524
"github.com/spf13/cobra"
@@ -74,7 +73,4 @@ var chaincodeCmd = &cobra.Command{
7473
Use: chainFuncName,
7574
Short: fmt.Sprintf("%s specific commands.", chainFuncName),
7675
Long: fmt.Sprintf("%s specific commands.", chainFuncName),
77-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
78-
flogging.LoggingInit(chainFuncName)
79-
},
8076
}

peer/main.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,18 @@ const cmdRoot = "core"
4949
var mainCmd = &cobra.Command{
5050
Use: "peer",
5151
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
52+
peerCommand := getPeerCommandFromCobraCommand(cmd)
53+
flogging.LoggingInit(peerCommand)
54+
5255
return core.CacheConfiguration()
5356
},
54-
PreRun: func(cmd *cobra.Command, args []string) {
55-
flogging.LoggingInit("peer")
56-
},
5757
Run: func(cmd *cobra.Command, args []string) {
5858
if versionFlag {
5959
version.Print()
6060
} else {
6161
cmd.HelpFunc()(cmd, args)
6262
}
6363
},
64-
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
65-
return nil
66-
},
6764
}
6865

6966
// Peer command version flag
@@ -127,3 +124,35 @@ func main() {
127124
}
128125
logger.Info("Exiting.....")
129126
}
127+
128+
// getPeerCommandFromCobraCommand retreives the peer command from the cobra command struct.
129+
// i.e. for a command of `peer node start`, this should return "node"
130+
// For the main/root command this will return the root name (i.e. peer)
131+
// For invalid commands (i.e. nil commands) this will return an empty string
132+
func getPeerCommandFromCobraCommand(command *cobra.Command) string {
133+
var commandName string
134+
135+
if command == nil {
136+
return commandName
137+
}
138+
139+
if peerCommand, ok := findChildOfRootCommand(command); ok {
140+
commandName = peerCommand.Name()
141+
} else {
142+
commandName = command.Name()
143+
}
144+
145+
return commandName
146+
}
147+
148+
func findChildOfRootCommand(command *cobra.Command) (*cobra.Command, bool) {
149+
for command.HasParent() {
150+
if !command.Parent().HasParent() {
151+
return command, true
152+
}
153+
154+
command = command.Parent()
155+
}
156+
157+
return nil, false
158+
}

peer/main_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func TestBuildcommandNameOutputSingleCommand(t *testing.T) {
26+
command := &cobra.Command{Use: "command"}
27+
28+
commandNameOutput := getPeerCommandFromCobraCommand(command)
29+
30+
assertEqual(t, "command", commandNameOutput)
31+
}
32+
33+
func TestBuildcommandNameOutputNilCommand(t *testing.T) {
34+
var command *cobra.Command
35+
36+
commandNameOutput := getPeerCommandFromCobraCommand(command)
37+
38+
assertEqual(t, "", commandNameOutput)
39+
}
40+
41+
func TestBuildcommandNameOutputTwoCommands(t *testing.T) {
42+
rootCommand := &cobra.Command{Use: "rootcommand"}
43+
childCommand := &cobra.Command{Use: "childcommand"}
44+
rootCommand.AddCommand(childCommand)
45+
46+
commandNameOutput := getPeerCommandFromCobraCommand(childCommand)
47+
48+
assertEqual(t, "childcommand", commandNameOutput)
49+
}
50+
51+
func TestBuildcommandNameOutputThreeCommands(t *testing.T) {
52+
rootCommand := &cobra.Command{Use: "rootcommand"}
53+
childCommand := &cobra.Command{Use: "childcommand"}
54+
leafCommand := &cobra.Command{Use: "leafCommand"}
55+
rootCommand.AddCommand(childCommand)
56+
childCommand.AddCommand(leafCommand)
57+
58+
commandNameOutput := getPeerCommandFromCobraCommand(leafCommand)
59+
60+
assertEqual(t, "childcommand", commandNameOutput)
61+
}
62+
63+
func TestBuildcommandNameOutputFourCommands(t *testing.T) {
64+
rootCommand := &cobra.Command{Use: "rootcommand"}
65+
childCommand := &cobra.Command{Use: "childcommand"}
66+
secondChildCommand := &cobra.Command{Use: "secondChildCommand"}
67+
leafCommand := &cobra.Command{Use: "leafCommand"}
68+
69+
rootCommand.AddCommand(childCommand)
70+
childCommand.AddCommand(secondChildCommand)
71+
secondChildCommand.AddCommand(leafCommand)
72+
73+
commandNameOutput := getPeerCommandFromCobraCommand(leafCommand)
74+
75+
assertEqual(t, "childcommand", commandNameOutput)
76+
}
77+
78+
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
79+
if expected != actual {
80+
t.Errorf("Expected %v, got %v", expected, actual)
81+
}
82+
}

peer/network/network.go

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package network
1919
import (
2020
"fmt"
2121

22-
"github.com/hyperledger/fabric/flogging"
2322
"github.com/op/go-logging"
2423
"github.com/spf13/cobra"
2524
)
@@ -40,7 +39,4 @@ var networkCmd = &cobra.Command{
4039
Use: networkFuncName,
4140
Short: fmt.Sprintf("%s specific commands.", networkFuncName),
4241
Long: fmt.Sprintf("%s specific commands.", networkFuncName),
43-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
44-
flogging.LoggingInit(networkFuncName)
45-
},
4642
}

peer/node/node.go

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package node
1919
import (
2020
"fmt"
2121

22-
"github.com/hyperledger/fabric/flogging"
2322
"github.com/op/go-logging"
2423
"github.com/spf13/cobra"
2524
)
@@ -44,7 +43,4 @@ var nodeCmd = &cobra.Command{
4443
Use: nodeFuncName,
4544
Short: fmt.Sprintf("%s specific commands.", nodeFuncName),
4645
Long: fmt.Sprintf("%s specific commands.", nodeFuncName),
47-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
48-
flogging.LoggingInit(nodeFuncName)
49-
},
5046
}

peer/version/version.go

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package version
1919
import (
2020
"fmt"
2121

22-
"github.com/hyperledger/fabric/flogging"
2322
"github.com/spf13/cobra"
2423
"github.com/spf13/viper"
2524
)
@@ -33,9 +32,6 @@ var cobraCommand = &cobra.Command{
3332
Use: "version",
3433
Short: "Print fabric peer version.",
3534
Long: `Print current version of fabric peer server.`,
36-
PreRun: func(cmd *cobra.Command, args []string) {
37-
flogging.LoggingInit("version")
38-
},
3935
Run: func(cmd *cobra.Command, args []string) {
4036
Print()
4137
},

0 commit comments

Comments
 (0)