Răsfoiți Sursa

Do not delegate to old cli if cobra fails before invoking the PreRun or SetHelp hook that call isOwnCommand(). Find the command from root.Find(args).
Tried to use `cmd, err := root.ExecuteC()` but I can’t pass the context like with `root.ExecuteContext(ctx)`, could not find a way without and explicit call to root.Find(args)

Guillaume Tardif 5 ani în urmă
părinte
comite
72bae873c5
2 a modificat fișierele cu 15 adăugiri și 8 ștergeri
  1. 7 8
      cli/main.go
  2. 8 0
      tests/e2e/e2e_test.go

+ 7 - 8
cli/main.go

@@ -60,8 +60,7 @@ import (
 )
 
 var (
-	runningOwnCommand bool
-	ownCommands       = map[string]struct{}{
+	ownCommands = map[string]struct{}{
 		"context": {},
 		"login":   {},
 		"serve":   {},
@@ -100,8 +99,7 @@ func main() {
 		SilenceErrors: true,
 		SilenceUsage:  true,
 		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
-			runningOwnCommand = isOwnCommand(cmd)
-			if !runningOwnCommand {
+			if !isOwnCommand(cmd) {
 				dockerclassic.Exec(cmd.Context())
 			}
 			return nil
@@ -125,8 +123,7 @@ func main() {
 
 	helpFunc := root.HelpFunc()
 	root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
-		runningOwnCommand = isOwnCommand(cmd)
-		if !runningOwnCommand {
+		if !isOwnCommand(cmd) {
 			dockerclassic.Exec(cmd.Context())
 		}
 		helpFunc(cmd, args)
@@ -163,9 +160,11 @@ func main() {
 	ctx = apicontext.WithCurrentContext(ctx, currentContext)
 	ctx = store.WithContextStore(ctx, s)
 
-	if err = root.ExecuteContext(ctx); err != nil {
+	err = root.ExecuteContext(ctx)
+	if err != nil {
 		// Context should always be handled by new CLI
-		if runningOwnCommand {
+		requiredCmd, _, _ := root.Find(os.Args[1:])
+		if requiredCmd != nil && isOwnCommand(requiredCmd) {
 			fmt.Fprintln(os.Stderr, err)
 			os.Exit(1)
 		}

+ 8 - 0
tests/e2e/e2e_test.go

@@ -76,6 +76,14 @@ func (s *E2eSuite) TestContextLegacy() {
 	})
 }
 
+func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() {
+	It("should dispay new cli error when parsing context create flags", func() {
+		_, err := s.NewDockerCommand("context", "create", "--aci-subscription-id", "titi").Exec()
+		Expect(err.Error()).NotTo(ContainSubstring("unknown flag"))
+		Expect(err.Error()).To(ContainSubstring("accepts 2 arg(s), received 0"))
+	})
+}
+
 func (s *E2eSuite) TestClassicLoginWithparameters() {
 	output, err := s.NewDockerCommand("login", "-u", "nouser", "-p", "wrongpasword").Exec()
 	Expect(output).To(ContainSubstring("Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password"))