Skip to content

Commit

Permalink
Merge pull request #116 from docker/respect-sigint
Browse files Browse the repository at this point in the history
Handle ctrl+C in tag rm prompt
  • Loading branch information
chris-crone authored Nov 12, 2020
2 parents 8aa91a9 + 59240c9 commit af35f75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions internal/commands/tag/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit af35f75

Please sign in to comment.