Merge pull request #116 from docker/respect-sigint

Handle ctrl+C in tag rm prompt
This commit is contained in:
Chris Crone 2020-11-12 18:04:53 +01:00 коммит произвёл GitHub
Родитель 8aa91a94ca 59240c977f
Коммит af35f75404
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 18 добавлений и 6 удалений

1
go.mod
Просмотреть файл

@ -25,6 +25,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.3 // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/opencontainers/image-spec v1.0.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208

Просмотреть файл

@ -18,12 +18,14 @@ package tag
import (
"bufio"
"context"
"fmt"
"strings"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/hub-tool/internal/ansi"
@ -49,15 +51,15 @@ func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *co
PreRun: func(cmd *cobra.Command, args []string) {
metrics.Send(parent, rmName)
},
RunE: func(_ *cobra.Command, args []string) error {
return runRm(streams, hubClient, opts, args[0])
RunE: func(cmd *cobra.Command, args []string) error {
return runRm(cmd.Context(), streams, hubClient, opts, args[0])
},
}
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force deletion of the tag")
return cmd
}
func runRm(streams command.Streams, hubClient *hub.Client, opts rmOptions, image string) error {
func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client, opts rmOptions, image string) error {
ref, err := reference.ParseNormalizedNamed(image)
if err != nil {
return err
@ -70,9 +72,18 @@ func runRm(streams command.Streams, hubClient *hub.Client, opts rmOptions, image
if !opts.force {
fmt.Fprintln(streams.Out(), ansi.Warn("Please type the name of your tag to confirm deletion:"), reference.FamiliarString(namedTaggedRef))
reader := bufio.NewReader(streams.In())
input, _ := reader.ReadString('\n')
input = strings.ToLower(strings.TrimSpace(input))
userIn := make(chan string, 1)
go func() {
reader := bufio.NewReader(streams.In())
input, _ := reader.ReadString('\n')
userIn <- strings.ToLower(strings.TrimSpace(input))
}()
input := ""
select {
case <-ctx.Done():
return errors.New("canceled")
case input = <-userIn:
}
if input != reference.FamiliarString(namedTaggedRef) {
return fmt.Errorf("%q differs from your tag name, deletion aborted", input)
}