瀏覽代碼

exit with status 1 if help is called on an invalid command.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
Daniel Nephin 8 年之前
父節點
當前提交
bb7601a3ff
共有 2 個文件被更改,包括 33 次插入11 次删除
  1. 19 0
      cli/cobra.go
  2. 14 11
      cmd/docker/docker_test.go

+ 19 - 0
cli/cobra.go

@@ -2,6 +2,7 @@ package cli
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"strings"
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 )
 )
@@ -17,6 +18,7 @@ func SetupRootCommand(rootCmd *cobra.Command) {
 	rootCmd.SetUsageTemplate(usageTemplate)
 	rootCmd.SetUsageTemplate(usageTemplate)
 	rootCmd.SetHelpTemplate(helpTemplate)
 	rootCmd.SetHelpTemplate(helpTemplate)
 	rootCmd.SetFlagErrorFunc(FlagErrorFunc)
 	rootCmd.SetFlagErrorFunc(FlagErrorFunc)
+	rootCmd.SetHelpCommand(helpCommand)
 
 
 	rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
 	rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
 	rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
 	rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
@@ -39,6 +41,23 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
 	}
 	}
 }
 }
 
 
+var helpCommand = &cobra.Command{
+	Use:               "help [command]",
+	Short:             "Help about the command",
+	PersistentPreRun:  func(cmd *cobra.Command, args []string) {},
+	PersistentPostRun: func(cmd *cobra.Command, args []string) {},
+	RunE: func(c *cobra.Command, args []string) error {
+		cmd, args, e := c.Root().Find(args)
+		if cmd == nil || e != nil || len(args) > 0 {
+			return fmt.Errorf("unknown help topic: %v", strings.Join(args, " "))
+		}
+
+		helpFunc := cmd.HelpFunc()
+		helpFunc(cmd, args)
+		return nil
+	},
+}
+
 func hasSubCommands(cmd *cobra.Command) bool {
 func hasSubCommands(cmd *cobra.Command) bool {
 	return len(operationSubCommands(cmd)) > 0
 	return len(operationSubCommands(cmd)) > 0
 }
 }

+ 14 - 11
cmd/docker/docker_test.go

@@ -1,13 +1,14 @@
 package main
 package main
 
 
 import (
 import (
+	"io/ioutil"
 	"os"
 	"os"
 	"testing"
 	"testing"
 
 
 	"github.com/Sirupsen/logrus"
 	"github.com/Sirupsen/logrus"
-	"github.com/docker/docker/utils"
-
 	"github.com/docker/docker/cli/command"
 	"github.com/docker/docker/cli/command"
+	"github.com/docker/docker/pkg/testutil/assert"
+	"github.com/docker/docker/utils"
 )
 )
 
 
 func TestClientDebugEnabled(t *testing.T) {
 func TestClientDebugEnabled(t *testing.T) {
@@ -16,14 +17,16 @@ func TestClientDebugEnabled(t *testing.T) {
 	cmd := newDockerCommand(&command.DockerCli{})
 	cmd := newDockerCommand(&command.DockerCli{})
 	cmd.Flags().Set("debug", "true")
 	cmd.Flags().Set("debug", "true")
 
 
-	if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil {
-		t.Fatalf("Unexpected error: %s", err.Error())
-	}
+	err := cmd.PersistentPreRunE(cmd, []string{})
+	assert.NilError(t, err)
+	assert.Equal(t, os.Getenv("DEBUG"), "1")
+	assert.Equal(t, logrus.GetLevel(), logrus.DebugLevel)
+}
 
 
-	if os.Getenv("DEBUG") != "1" {
-		t.Fatal("expected debug enabled, got false")
-	}
-	if logrus.GetLevel() != logrus.DebugLevel {
-		t.Fatalf("expected logrus debug level, got %v", logrus.GetLevel())
-	}
+func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) {
+	discard := ioutil.Discard
+	cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard))
+	cmd.SetArgs([]string{"help", "invalid"})
+	err := cmd.Execute()
+	assert.Error(t, err, "unknown help topic: invalid")
 }
 }