Преглед на файлове

Replace custom error type for exit code with existing one from docker/cli.
Will also help to move towards CLI plugin.

Signed-off-by: Guillaume Tardif <[email protected]>

Guillaume Tardif преди 4 години
родител
ревизия
86f5e311d7
променени са 5 файла, в които са добавени 17 реда и са изтрити 35 реда
  1. 6 2
      cli/cmd/compose/run.go
  2. 6 2
      cli/cmd/compose/up.go
  3. 0 28
      cli/cmd/exit.go
  4. 2 1
      cli/cmd/version.go
  5. 3 2
      cli/main.go

+ 6 - 2
cli/cmd/compose/run.go

@@ -27,10 +27,10 @@ import (
 	"github.com/mattn/go-shellwords"
 	"github.com/mattn/go-shellwords"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
+	"github.com/docker/cli/cli"
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/api/progress"
 	"github.com/docker/compose-cli/api/progress"
-	"github.com/docker/compose-cli/cli/cmd"
 )
 )
 
 
 type runOptions struct {
 type runOptions struct {
@@ -196,7 +196,11 @@ func runRun(ctx context.Context, opts runOptions) error {
 	}
 	}
 	exitCode, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
 	exitCode, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
 	if exitCode != 0 {
 	if exitCode != 0 {
-		return cmd.ExitCodeError{ExitCode: exitCode}
+		errMsg := ""
+		if err != nil {
+			errMsg = err.Error()
+		}
+		return cli.StatusError{StatusCode: exitCode, Status: errMsg}
 	}
 	}
 	return err
 	return err
 }
 }

+ 6 - 2
cli/cmd/compose/up.go

@@ -28,6 +28,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/compose-spec/compose-go/types"
 	"github.com/compose-spec/compose-go/types"
+	"github.com/docker/cli/cli"
 	"github.com/docker/compose-cli/utils"
 	"github.com/docker/compose-cli/utils"
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
@@ -37,7 +38,6 @@ import (
 	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/api/context/store"
 	"github.com/docker/compose-cli/api/context/store"
 	"github.com/docker/compose-cli/api/progress"
 	"github.com/docker/compose-cli/api/progress"
-	"github.com/docker/compose-cli/cli/cmd"
 	"github.com/docker/compose-cli/cli/formatter"
 	"github.com/docker/compose-cli/cli/formatter"
 )
 )
 
 
@@ -323,7 +323,11 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
 
 
 	err = eg.Wait()
 	err = eg.Wait()
 	if exitCode != 0 {
 	if exitCode != 0 {
-		return cmd.ExitCodeError{ExitCode: exitCode}
+		errMsg := ""
+		if err != nil {
+			errMsg = err.Error()
+		}
+		return cli.StatusError{StatusCode: exitCode, Status: errMsg}
 	}
 	}
 	return err
 	return err
 }
 }

+ 0 - 28
cli/cmd/exit.go

@@ -1,28 +0,0 @@
-/*
-   Copyright 2020 Docker Compose CLI authors
-
-   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 cmd
-
-import "strconv"
-
-// ExitCodeError reports an exit code set by command.
-type ExitCodeError struct {
-	ExitCode int
-}
-
-func (e ExitCodeError) Error() string {
-	return strconv.Itoa(e.ExitCode)
-}

+ 2 - 1
cli/cmd/version.go

@@ -23,6 +23,7 @@ import (
 
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
+	"github.com/docker/cli/cli"
 	"github.com/docker/compose-cli/cli/formatter"
 	"github.com/docker/compose-cli/cli/formatter"
 	"github.com/docker/compose-cli/cli/mobycli"
 	"github.com/docker/compose-cli/cli/mobycli"
 	"github.com/docker/compose-cli/internal"
 	"github.com/docker/compose-cli/internal"
@@ -39,7 +40,7 @@ func VersionCommand() *cobra.Command {
 		RunE: func(cmd *cobra.Command, _ []string) error {
 		RunE: func(cmd *cobra.Command, _ []string) error {
 			err := runVersion(cmd)
 			err := runVersion(cmd)
 			if err != nil {
 			if err != nil {
-				return ExitCodeError{ExitCode: 1}
+				return cli.StatusError{StatusCode: 1, Status: err.Error()}
 			}
 			}
 			return nil
 			return nil
 		},
 		},

+ 3 - 2
cli/main.go

@@ -28,6 +28,7 @@ import (
 	"syscall"
 	"syscall"
 	"time"
 	"time"
 
 
+	"github.com/docker/cli/cli"
 	"github.com/docker/cli/cli/command"
 	"github.com/docker/cli/cli/command"
 	cliconfig "github.com/docker/cli/cli/config"
 	cliconfig "github.com/docker/cli/cli/config"
 	cliflags "github.com/docker/cli/cli/flags"
 	cliflags "github.com/docker/cli/cli/flags"
@@ -286,9 +287,9 @@ $ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
 }
 }
 
 
 func exit(ctx string, err error, ctype string) {
 func exit(ctx string, err error, ctype string) {
-	if exit, ok := err.(cmd.ExitCodeError); ok {
+	if exit, ok := err.(cli.StatusError); ok {
 		metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
 		metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
-		os.Exit(exit.ExitCode)
+		os.Exit(exit.StatusCode)
 	}
 	}
 
 
 	metrics.Track(ctype, os.Args[1:], metrics.FailureStatus)
 	metrics.Track(ctype, os.Args[1:], metrics.FailureStatus)