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/ansi"
"github.com/docker/hub-tool/internal/credentials" "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/hub"
"github.com/docker/hub-tool/internal/login" "github.com/docker/hub-tool/internal/login"
"github.com/docker/hub-tool/internal/metrics" "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) err := login.RunLogin(cmd.Context(), streams, hubClient, store, username)
if err != nil { if err != nil {
if err == login.ErrCanceled { if err == errdef.ErrCanceled {
return nil return nil
} }
return err return err

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

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

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

@ -25,12 +25,12 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/distribution/reference" "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/ansi"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub" "github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics" "github.com/docker/hub-tool/internal/metrics"
"github.com/pkg/errors"
"github.com/spf13/cobra"
) )
const ( const (
@ -41,8 +41,6 @@ type rmOptions struct {
force bool force bool
} }
var errCanceled = errors.New("canceled")
func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command { func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
var opts rmOptions var opts rmOptions
cmd := &cobra.Command{ 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 { RunE: func(cmd *cobra.Command, args []string) error {
err := runRm(cmd.Context(), streams, hubClient, opts, args[0]) err := runRm(cmd.Context(), streams, hubClient, opts, args[0])
if err == nil || err == errCanceled { if err == nil || err == errdef.ErrCanceled {
return nil return nil
} }
return err return err
@ -89,7 +87,7 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
input := "" input := ""
select { select {
case <-ctx.Done(): case <-ctx.Done():
return errCanceled return errdef.ErrCanceled
case input = <-userIn: case input = <-userIn:
} }
if strings.ToLower(input) != "y" { 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/ansi"
"github.com/docker/hub-tool/internal/credentials" "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/hub"
) )
// ErrCanceled represents a normally canceled operation
var ErrCanceled = errors.New("canceled")
// RunLogin logs the user and asks for the 2FA code if needed // 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 { func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store, candidateUsername string) error {
username := candidateUsername username := candidateUsername
@ -87,7 +85,7 @@ func readClearText(ctx context.Context, streams command.Streams, prompt string)
input := "" input := ""
select { select {
case <-ctx.Done(): case <-ctx.Done():
return "", ErrCanceled return "", errdef.ErrCanceled
case input = <-userIn: case input = <-userIn:
} }
return input, nil return input, nil