浏览代码

Fix bug not allowing users to run any context command when set to an aws context type.
When users have an “aws" context type, we try to exec the command (will execute context ls, context use, etc.) and when it fails because it doesn’t find the aws backend, then display the custom error for aws context type.

Signed-off-by: Guillaume Tardif <[email protected]>

Guillaume Tardif 5 年之前
父节点
当前提交
adeba9b124
共有 3 个文件被更改,包括 19 次插入20 次删除
  1. 11 12
      cli/main.go
  2. 7 7
      cli/main_test.go
  3. 1 1
      cli/mobycli/exec.go

+ 11 - 12
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 <name>`, 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 <name>`, 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 <name>`, 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)

+ 7 - 7
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()))
 }

+ 1 - 1
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"