Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ctrl+C in tag rm prompt #116

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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