Skip to content

Commit 46cf17f

Browse files
committed
[FAB-10163] Detect trailing args for 'peer version'
Change-Id: I9fb340a163ea7e5244025d4f6fc3e4554f60183e Signed-off-by: Zhenguo Niu <Niu.ZGlinux@gmail.com>
1 parent 7437da1 commit 46cf17f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

peer/version/version.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ var cobraCommand = &cobra.Command{
2626
Use: "version",
2727
Short: "Print fabric peer version.",
2828
Long: `Print current version of the fabric peer server.`,
29-
Run: func(cmd *cobra.Command, args []string) {
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
if len(args) != 0 {
31+
return fmt.Errorf("trailing args detected")
32+
}
33+
// Parsing of the command line is done so silence cmd usage
34+
cmd.SilenceUsage = true
3035
fmt.Print(GetInfo())
36+
return nil
3137
},
3238
}
3339

peer/version/version_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ func TestCmd(t *testing.T) {
2626
cmd := Cmd()
2727
assert.NoError(t, cmd.Execute(), "expected version command to succeed")
2828
}
29+
30+
func TestCmdWithTrailingArgs(t *testing.T) {
31+
cmd := Cmd()
32+
args := []string{"trailingargs"}
33+
cmd.SetArgs(args)
34+
assert.EqualError(t, cmd.Execute(), "trailing args detected")
35+
}

0 commit comments

Comments
 (0)