From b85eddd09c1f0b01af3d68bcc409b1b827188000 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 11 Sep 2020 16:08:06 +0200 Subject: [PATCH] Remove duplicated code formatting multi errors Signed-off-by: Guillaume Tardif --- cli/cmd/context/rm.go | 14 ++---------- cli/cmd/kill.go | 8 +++---- cli/cmd/rm.go | 14 ++---------- cli/cmd/start.go | 12 ++++------- cli/cmd/stop.go | 11 ++++------ cli/cmd/volume/acivolume.go | 15 ++----------- cli/formatter/multierrformat.go | 38 +++++++++++++++++++++++++++++++++ cli/main.go | 7 +++--- 8 files changed, 59 insertions(+), 60 deletions(-) create mode 100644 cli/formatter/multierrformat.go diff --git a/cli/cmd/context/rm.go b/cli/cmd/context/rm.go index 9cc085151..9a699996e 100644 --- a/cli/cmd/context/rm.go +++ b/cli/cmd/context/rm.go @@ -20,11 +20,11 @@ import ( "context" "errors" "fmt" - "strings" "github.com/hashicorp/go-multierror" "github.com/spf13/cobra" + "github.com/docker/compose-cli/cli/formatter" apicontext "github.com/docker/compose-cli/context" "github.com/docker/compose-cli/context/store" ) @@ -69,20 +69,10 @@ func runRemove(ctx context.Context, args []string, force bool) error { errs = removeContext(s, contextName, errs) } } - if errs != nil { - errs.ErrorFormat = formatErrors - } + formatter.SetMultiErrorFormat(errs) return errs.ErrorOrNil() } -func formatErrors(errs []error) string { - messages := make([]string, len(errs)) - for i, err := range errs { - messages[i] = "Error: " + err.Error() - } - return strings.Join(messages, "\n") -} - func removeContext(s store.Store, n string, errs *multierror.Error) *multierror.Error { if err := s.Remove(n); err != nil { errs = multierror.Append(errs, err) diff --git a/cli/cmd/kill.go b/cli/cmd/kill.go index 1eff1aabb..c63a9cdac 100644 --- a/cli/cmd/kill.go +++ b/cli/cmd/kill.go @@ -20,12 +20,12 @@ import ( "context" "fmt" + "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/spf13/cobra" - "github.com/hashicorp/go-multierror" - "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/errdefs" ) @@ -69,8 +69,6 @@ func runKill(ctx context.Context, args []string, opts killOpts) error { } fmt.Println(id) } - if errs != nil { - errs.ErrorFormat = formatErrors - } + formatter.SetMultiErrorFormat(errs) return errs.ErrorOrNil() } diff --git a/cli/cmd/rm.go b/cli/cmd/rm.go index f61f86dda..500ffc9ac 100644 --- a/cli/cmd/rm.go +++ b/cli/cmd/rm.go @@ -19,7 +19,6 @@ package cmd import ( "context" "fmt" - "strings" "github.com/hashicorp/go-multierror" "github.com/pkg/errors" @@ -27,6 +26,7 @@ import ( "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/containers" + "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/errdefs" ) @@ -75,16 +75,6 @@ func runRm(ctx context.Context, args []string, opts rmOpts) error { fmt.Println(id) } - if errs != nil { - errs.ErrorFormat = formatErrors - } + formatter.SetMultiErrorFormat(errs) return errs.ErrorOrNil() } - -func formatErrors(errs []error) string { - messages := make([]string, len(errs)) - for i, err := range errs { - messages[i] = "Error: " + err.Error() - } - return strings.Join(messages, "\n") -} diff --git a/cli/cmd/start.go b/cli/cmd/start.go index f65dd1d54..e973c817d 100644 --- a/cli/cmd/start.go +++ b/cli/cmd/start.go @@ -20,14 +20,13 @@ import ( "context" "fmt" - "github.com/docker/compose-cli/errdefs" - + "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/spf13/cobra" - "github.com/hashicorp/go-multierror" - "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/cli/formatter" + "github.com/docker/compose-cli/errdefs" ) // StartCommand starts containers @@ -63,9 +62,6 @@ func runStart(ctx context.Context, args []string) error { } fmt.Println(id) } - if errs != nil { - errs.ErrorFormat = formatErrors - } - + formatter.SetMultiErrorFormat(errs) return errs.ErrorOrNil() } diff --git a/cli/cmd/stop.go b/cli/cmd/stop.go index cbfeb3856..9c3cd122e 100644 --- a/cli/cmd/stop.go +++ b/cli/cmd/stop.go @@ -20,14 +20,13 @@ import ( "context" "fmt" - "github.com/docker/compose-cli/errdefs" - + "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/spf13/cobra" - "github.com/hashicorp/go-multierror" - "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/cli/formatter" + "github.com/docker/compose-cli/errdefs" ) type stopOpts struct { @@ -70,8 +69,6 @@ func runStop(ctx context.Context, args []string, opts stopOpts) error { } fmt.Println(id) } - if errs != nil { - errs.ErrorFormat = formatErrors - } + formatter.SetMultiErrorFormat(errs) return errs.ErrorOrNil() } diff --git a/cli/cmd/volume/acivolume.go b/cli/cmd/volume/acivolume.go index 4216c36d2..4e7fc6d32 100644 --- a/cli/cmd/volume/acivolume.go +++ b/cli/cmd/volume/acivolume.go @@ -19,14 +19,13 @@ package volume import ( "context" "fmt" - "strings" "github.com/hashicorp/go-multierror" - "github.com/spf13/cobra" "github.com/docker/compose-cli/aci" "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/progress" ) @@ -97,19 +96,9 @@ func rmVolume() *cobra.Command { } fmt.Println(id) } - if errs != nil { - errs.ErrorFormat = formatErrors - } + formatter.SetMultiErrorFormat(errs) return errs.ErrorOrNil() }, } return cmd } - -func formatErrors(errs []error) string { - messages := make([]string, len(errs)) - for i, err := range errs { - messages[i] = "Error: " + err.Error() - } - return strings.Join(messages, "\n") -} diff --git a/cli/formatter/multierrformat.go b/cli/formatter/multierrformat.go new file mode 100644 index 000000000..a91f1cc3b --- /dev/null +++ b/cli/formatter/multierrformat.go @@ -0,0 +1,38 @@ +/* + Copyright 2020 Docker, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package formatter + +import ( + "strings" + + "github.com/hashicorp/go-multierror" +) + +// SetMultiErrorFormat set cli default format for multi-errors +func SetMultiErrorFormat(errs *multierror.Error) { + if errs != nil { + errs.ErrorFormat = formatErrors + } +} + +func formatErrors(errs []error) string { + messages := make([]string, len(errs)) + for i, err := range errs { + messages[i] = "Error: " + err.Error() + } + return strings.Join(messages, "\n") +} diff --git a/cli/main.go b/cli/main.go index a4eadd1bb..a495d53e5 100644 --- a/cli/main.go +++ b/cli/main.go @@ -27,13 +27,14 @@ import ( "syscall" "time" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/docker/compose-cli/cli/cmd/compose" "github.com/docker/compose-cli/cli/cmd/logout" volume "github.com/docker/compose-cli/cli/cmd/volume" "github.com/docker/compose-cli/errdefs" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" // Backend registrations _ "github.com/docker/compose-cli/aci"