move error decaleration to a central place

Signed-off-by: Moshe Beladev <moshebe123@gmail.com>
This commit is contained in:
Moshe Beladev 2021-01-31 17:14:19 +02:00
Родитель 645ce6c610
Коммит a32b7fd48f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 75E75C0711A042E5
5 изменённых файлов: 18 добавлений и 17 удалений

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

@ -25,6 +25,7 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/credentials"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/login"
"github.com/docker/hub-tool/internal/metrics"
@ -50,7 +51,7 @@ func newLoginCmd(streams command.Streams, store credentials.Store, hubClient *hu
}
err := login.RunLogin(cmd.Context(), streams, hubClient, store, username)
if err != nil {
if err == login.ErrCanceled {
if err == errdef.ErrCanceled {
return nil
}
return err

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

@ -19,8 +19,8 @@ package repo
import (
"bufio"
"context"
"errors"
"fmt"
"github.com/docker/hub-tool/internal/errdef"
"strings"
"github.com/docker/cli/cli"
@ -41,8 +41,6 @@ type rmOptions struct {
force bool
}
var errCanceled = errors.New("canceled")
func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
var opts rmOptions
cmd := &cobra.Command{
@ -55,7 +53,7 @@ func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *co
},
RunE: func(cmd *cobra.Command, args []string) error {
err := runRm(cmd.Context(), streams, hubClient, opts, args[0])
if err == nil || err == errCanceled {
if err == nil || err == errdef.ErrCanceled {
return nil
}
return err
@ -92,7 +90,7 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
input := ""
select {
case <-ctx.Done():
return errCanceled
return errdef.ErrCanceled
case input = <-userIn:
}
if input != namedRef.Name() {

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

@ -25,12 +25,12 @@ import (
"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"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
@ -41,8 +41,6 @@ type rmOptions struct {
force bool
}
var errCanceled = errors.New("canceled")
func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
var opts rmOptions
cmd := &cobra.Command{
@ -55,7 +53,7 @@ func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *co
},
RunE: func(cmd *cobra.Command, args []string) error {
err := runRm(cmd.Context(), streams, hubClient, opts, args[0])
if err == nil || err == errCanceled {
if err == nil || err == errdef.ErrCanceled {
return nil
}
return err
@ -89,7 +87,7 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
input := ""
select {
case <-ctx.Done():
return errCanceled
return errdef.ErrCanceled
case input = <-userIn:
}
if strings.ToLower(input) != "y" {

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

@ -0,0 +1,6 @@
package errdef
import "errors"
// ErrCanceled represents a normally canceled operation
var ErrCanceled = errors.New("canceled")

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

@ -32,12 +32,10 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/credentials"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub"
)
// ErrCanceled represents a normally canceled operation
var ErrCanceled = errors.New("canceled")
// RunLogin logs the user and asks for the 2FA code if needed
func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store, candidateUsername string) error {
username := candidateUsername
@ -87,7 +85,7 @@ func readClearText(ctx context.Context, streams command.Streams, prompt string)
input := ""
select {
case <-ctx.Done():
return "", ErrCanceled
return "", errdef.ErrCanceled
case input = <-userIn:
}
return input, nil