mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 18:13:42 +08:00 
			
		
		
		
	commands: handle ctrl-c on active prompt
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
		@@ -49,9 +49,13 @@ func runPrune(ctx context.Context, dockerCli command.Cli, opts pruneOptions) err
 | 
				
			|||||||
		warning = allCacheWarning
 | 
							warning = allCacheWarning
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
 | 
						if !opts.force {
 | 
				
			||||||
 | 
							if ok, err := prompt(ctx, dockerCli.In(), dockerCli.Out(), warning); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							} else if !ok {
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	b, err := builder.New(dockerCli, builder.WithName(opts.builder))
 | 
						b, err := builder.New(dockerCli, builder.WithName(opts.builder))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,9 +28,13 @@ const (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error {
 | 
					func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error {
 | 
				
			||||||
	if in.allInactive && !in.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), rmInactiveWarning) {
 | 
						if in.allInactive && !in.force {
 | 
				
			||||||
 | 
							if ok, err := prompt(ctx, dockerCli.In(), dockerCli.Out(), rmInactiveWarning); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							} else if !ok {
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	txn, release, err := storeutil.GetStore(dockerCli)
 | 
						txn, release, err := storeutil.GetStore(dockerCli)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								commands/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								commands/util.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					package commands
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/docker/cli/cli/command"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func prompt(ctx context.Context, ins io.Reader, out io.Writer, msg string) (bool, error) {
 | 
				
			||||||
 | 
						done := make(chan struct{})
 | 
				
			||||||
 | 
						var ok bool
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							ok = command.PromptForConfirmation(ins, out, msg)
 | 
				
			||||||
 | 
							close(done)
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						select {
 | 
				
			||||||
 | 
						case <-ctx.Done():
 | 
				
			||||||
 | 
							return false, context.Cause(ctx)
 | 
				
			||||||
 | 
						case <-done:
 | 
				
			||||||
 | 
							return ok, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user