From 59240c977fb6c546512a78c577a6a9099291d820 Mon Sep 17 00:00:00 2001 From: Chris Crone Date: Thu, 12 Nov 2020 17:56:26 +0100 Subject: [PATCH] tag.rm: Stop reading user input on signal Signed-off-by: Chris Crone --- go.mod | 1 + internal/commands/tag/rm.go | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index d6b3e89..732041f 100644 --- a/go.mod +++ b/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 diff --git a/internal/commands/tag/rm.go b/internal/commands/tag/rm.go index c770184..fb803c0 100644 --- a/internal/commands/tag/rm.go +++ b/internal/commands/tag/rm.go @@ -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) }