Răsfoiți Sursa

Remove duplicated code formatting multi errors

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 5 ani în urmă
părinte
comite
b85eddd09c

+ 2 - 12
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)

+ 3 - 5
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()
 }

+ 2 - 12
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")
-}

+ 4 - 8
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()
 }

+ 4 - 7
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()
 }

+ 2 - 13
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")
-}

+ 38 - 0
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")
+}

+ 4 - 3
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"