From adeba9b124d64703d427c9c650f5e69899649d52 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 4 Sep 2020 10:19:40 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20bug=20not=20allowing=20users=20to=20run?= =?UTF-8?q?=20any=20context=20command=20when=20set=20to=20an=20aws=20conte?= =?UTF-8?q?xt=20type.=20When=20users=20have=20an=20=E2=80=9Caws"=20context?= =?UTF-8?q?=20type,=20we=20try=20to=20exec=20the=20command=20(will=20execu?= =?UTF-8?q?te=20context=20ls,=20context=20use,=20etc.)=20and=20when=20it?= =?UTF-8?q?=20fails=20because=20it=20doesn=E2=80=99t=20find=20the=20aws=20?= =?UTF-8?q?backend,=20then=20display=20the=20custom=20error=20for=20aws=20?= =?UTF-8?q?context=20type.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Guillaume Tardif --- cli/main.go | 23 +++++++++++------------ cli/main_test.go | 14 +++++++------- cli/mobycli/exec.go | 2 +- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/cli/main.go b/cli/main.go index 06e23ce7a..b24299f84 100644 --- a/cli/main.go +++ b/cli/main.go @@ -61,7 +61,7 @@ var ( ) var ( - ownCommands = map[string]struct{}{ + contextAgnosticCommands = map[string]struct{}{ "compose": {}, "context": {}, "login": {}, @@ -86,14 +86,14 @@ func init() { rand.Seed(time.Now().UnixNano()) } -func isOwnCommand(cmd *cobra.Command) bool { +func isContextAgnosticCommand(cmd *cobra.Command) bool { if cmd == nil { return false } - if _, ok := ownCommands[cmd.Name()]; ok { + if _, ok := contextAgnosticCommands[cmd.Name()]; ok { return true } - return isOwnCommand(cmd.Parent()) + return isContextAgnosticCommand(cmd.Parent()) } func main() { @@ -103,7 +103,7 @@ func main() { SilenceErrors: true, SilenceUsage: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if !isOwnCommand(cmd) { + if !isContextAgnosticCommand(cmd) { mobycli.ExecIfDefaultCtxType(cmd.Context()) } return nil @@ -136,7 +136,7 @@ func main() { helpFunc := root.HelpFunc() root.SetHelpFunc(func(cmd *cobra.Command, args []string) { - if !isOwnCommand(cmd) { + if !isContextAgnosticCommand(cmd) { mobycli.ExecIfDefaultCtxType(cmd.Context()) } helpFunc(cmd, args) @@ -181,11 +181,6 @@ func main() { ctype = cc.Type() } - if ctype == store.AwsContextType { - exit(root, currentContext, errors.Errorf(`%q context type has been renamed. Recreate the context by running: -$ docker context create %s `, cc.Type(), store.EcsContextType)) - } - metrics.Track(ctype, os.Args[1:], root.PersistentFlags()) ctx = apicontext.WithCurrentContext(ctx, currentContext) @@ -196,10 +191,14 @@ $ docker context create %s `, cc.Type(), store.EcsContextType)) if errors.Is(ctx.Err(), context.Canceled) { os.Exit(130) } + if ctype == store.AwsContextType { + exit(root, currentContext, errors.Errorf(`%q context type has been renamed. Recreate the context by running: +$ docker context create %s `, cc.Type(), store.EcsContextType)) + } // Context should always be handled by new CLI requiredCmd, _, _ := root.Find(os.Args[1:]) - if requiredCmd != nil && isOwnCommand(requiredCmd) { + if requiredCmd != nil && isContextAgnosticCommand(requiredCmd) { exit(root, currentContext, err) } mobycli.ExecIfDefaultCtxType(ctx) diff --git a/cli/main_test.go b/cli/main_test.go index 2db34c012..c116a18cf 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -61,11 +61,11 @@ func TestDetermineCurrentContext(t *testing.T) { } func TestCheckOwnCommand(t *testing.T) { - assert.Assert(t, isOwnCommand(login.Command())) - assert.Assert(t, isOwnCommand(context.Command())) - assert.Assert(t, isOwnCommand(cmd.ServeCommand())) - assert.Assert(t, !isOwnCommand(run.Command())) - assert.Assert(t, !isOwnCommand(cmd.ExecCommand())) - assert.Assert(t, !isOwnCommand(cmd.LogsCommand())) - assert.Assert(t, !isOwnCommand(cmd.PsCommand())) + assert.Assert(t, isContextAgnosticCommand(login.Command())) + assert.Assert(t, isContextAgnosticCommand(context.Command())) + assert.Assert(t, isContextAgnosticCommand(cmd.ServeCommand())) + assert.Assert(t, !isContextAgnosticCommand(run.Command())) + assert.Assert(t, !isContextAgnosticCommand(cmd.ExecCommand())) + assert.Assert(t, !isContextAgnosticCommand(cmd.LogsCommand())) + assert.Assert(t, !isContextAgnosticCommand(cmd.PsCommand())) } diff --git a/cli/mobycli/exec.go b/cli/mobycli/exec.go index 7b227159a..8e6128076 100644 --- a/cli/mobycli/exec.go +++ b/cli/mobycli/exec.go @@ -28,7 +28,7 @@ import ( "github.com/docker/compose-cli/context/store" ) -var delegatedContextTypes = []string{store.DefaultContextType, store.AwsContextType} +var delegatedContextTypes = []string{store.DefaultContextType} // ComDockerCli name of the classic cli binary const ComDockerCli = "com.docker.cli"