Remove duplicated code formatting multi errors

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-09-11 16:08:06 +02:00
parent 57ded74aee
commit b85eddd09c
8 changed files with 59 additions and 60 deletions

View File

@ -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)

View File

@ -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()
}

View File

@ -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")
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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"