From 72bae873c5e3c620c53e176b31f2fc92bad93706 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Wed, 10 Jun 2020 17:24:13 +0200 Subject: [PATCH] =?UTF-8?q?Do=20not=20delegate=20to=20old=20cli=20if=20cob?= =?UTF-8?q?ra=20fails=20before=20invoking=20the=20PreRun=20or=20SetHelp=20?= =?UTF-8?q?hook=20that=20call=20isOwnCommand().=20Find=20the=20command=20f?= =?UTF-8?q?rom=20root.Find(args).=20Tried=20to=20use=20=20`cmd,=20err=20:?= =?UTF-8?q?=3D=20root.ExecuteC()`=20but=20I=20can=E2=80=99t=20pass=20the?= =?UTF-8?q?=20context=20like=20with=20`root.ExecuteContext(ctx)`,=20could?= =?UTF-8?q?=20not=20find=20a=20way=20without=20and=20explicit=20call=20to?= =?UTF-8?q?=20root.Find(args)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/main.go | 15 +++++++-------- tests/e2e/e2e_test.go | 8 ++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/cli/main.go b/cli/main.go index ef84a534c..10e50f134 100644 --- a/cli/main.go +++ b/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) } diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index c85858637..2d250c583 100644 --- a/tests/e2e/e2e_test.go +++ b/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"))