mirror of https://github.com/docker/compose.git
				
				
				
			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 <guillaume.tardif@docker.com>
This commit is contained in:
		
							parent
							
								
									a0a0e391cb
								
							
						
					
					
						commit
						adeba9b124
					
				
							
								
								
									
										23
									
								
								cli/main.go
								
								
								
								
							
							
						
						
									
										23
									
								
								cli/main.go
								
								
								
								
							| 
						 | 
					@ -61,7 +61,7 @@ var (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	ownCommands = map[string]struct{}{
 | 
						contextAgnosticCommands = map[string]struct{}{
 | 
				
			||||||
		"compose": {},
 | 
							"compose": {},
 | 
				
			||||||
		"context": {},
 | 
							"context": {},
 | 
				
			||||||
		"login":   {},
 | 
							"login":   {},
 | 
				
			||||||
| 
						 | 
					@ -86,14 +86,14 @@ func init() {
 | 
				
			||||||
	rand.Seed(time.Now().UnixNano())
 | 
						rand.Seed(time.Now().UnixNano())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func isOwnCommand(cmd *cobra.Command) bool {
 | 
					func isContextAgnosticCommand(cmd *cobra.Command) bool {
 | 
				
			||||||
	if cmd == nil {
 | 
						if cmd == nil {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if _, ok := ownCommands[cmd.Name()]; ok {
 | 
						if _, ok := contextAgnosticCommands[cmd.Name()]; ok {
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return isOwnCommand(cmd.Parent())
 | 
						return isContextAgnosticCommand(cmd.Parent())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
| 
						 | 
					@ -103,7 +103,7 @@ func main() {
 | 
				
			||||||
		SilenceErrors: true,
 | 
							SilenceErrors: true,
 | 
				
			||||||
		SilenceUsage:  true,
 | 
							SilenceUsage:  true,
 | 
				
			||||||
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
 | 
							PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
 | 
				
			||||||
			if !isOwnCommand(cmd) {
 | 
								if !isContextAgnosticCommand(cmd) {
 | 
				
			||||||
				mobycli.ExecIfDefaultCtxType(cmd.Context())
 | 
									mobycli.ExecIfDefaultCtxType(cmd.Context())
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
| 
						 | 
					@ -136,7 +136,7 @@ func main() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	helpFunc := root.HelpFunc()
 | 
						helpFunc := root.HelpFunc()
 | 
				
			||||||
	root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
 | 
						root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
		if !isOwnCommand(cmd) {
 | 
							if !isContextAgnosticCommand(cmd) {
 | 
				
			||||||
			mobycli.ExecIfDefaultCtxType(cmd.Context())
 | 
								mobycli.ExecIfDefaultCtxType(cmd.Context())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		helpFunc(cmd, args)
 | 
							helpFunc(cmd, args)
 | 
				
			||||||
| 
						 | 
					@ -181,11 +181,6 @@ func main() {
 | 
				
			||||||
		ctype = cc.Type()
 | 
							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())
 | 
						metrics.Track(ctype, os.Args[1:], root.PersistentFlags())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ctx = apicontext.WithCurrentContext(ctx, currentContext)
 | 
						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) {
 | 
							if errors.Is(ctx.Err(), context.Canceled) {
 | 
				
			||||||
			os.Exit(130)
 | 
								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
 | 
							// Context should always be handled by new CLI
 | 
				
			||||||
		requiredCmd, _, _ := root.Find(os.Args[1:])
 | 
							requiredCmd, _, _ := root.Find(os.Args[1:])
 | 
				
			||||||
		if requiredCmd != nil && isOwnCommand(requiredCmd) {
 | 
							if requiredCmd != nil && isContextAgnosticCommand(requiredCmd) {
 | 
				
			||||||
			exit(root, currentContext, err)
 | 
								exit(root, currentContext, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		mobycli.ExecIfDefaultCtxType(ctx)
 | 
							mobycli.ExecIfDefaultCtxType(ctx)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -61,11 +61,11 @@ func TestDetermineCurrentContext(t *testing.T) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestCheckOwnCommand(t *testing.T) {
 | 
					func TestCheckOwnCommand(t *testing.T) {
 | 
				
			||||||
	assert.Assert(t, isOwnCommand(login.Command()))
 | 
						assert.Assert(t, isContextAgnosticCommand(login.Command()))
 | 
				
			||||||
	assert.Assert(t, isOwnCommand(context.Command()))
 | 
						assert.Assert(t, isContextAgnosticCommand(context.Command()))
 | 
				
			||||||
	assert.Assert(t, isOwnCommand(cmd.ServeCommand()))
 | 
						assert.Assert(t, isContextAgnosticCommand(cmd.ServeCommand()))
 | 
				
			||||||
	assert.Assert(t, !isOwnCommand(run.Command()))
 | 
						assert.Assert(t, !isContextAgnosticCommand(run.Command()))
 | 
				
			||||||
	assert.Assert(t, !isOwnCommand(cmd.ExecCommand()))
 | 
						assert.Assert(t, !isContextAgnosticCommand(cmd.ExecCommand()))
 | 
				
			||||||
	assert.Assert(t, !isOwnCommand(cmd.LogsCommand()))
 | 
						assert.Assert(t, !isContextAgnosticCommand(cmd.LogsCommand()))
 | 
				
			||||||
	assert.Assert(t, !isOwnCommand(cmd.PsCommand()))
 | 
						assert.Assert(t, !isContextAgnosticCommand(cmd.PsCommand()))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,7 +28,7 @@ import (
 | 
				
			||||||
	"github.com/docker/compose-cli/context/store"
 | 
						"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
 | 
					// ComDockerCli name of the classic cli binary
 | 
				
			||||||
const ComDockerCli = "com.docker.cli"
 | 
					const ComDockerCli = "com.docker.cli"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue