From 86f5e311d7495ca2cd23fb5664aef41a6045de22 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Wed, 7 Apr 2021 11:39:31 +0200 Subject: [PATCH] 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 --- cli/cmd/compose/run.go | 8 ++++++-- cli/cmd/compose/up.go | 8 ++++++-- cli/cmd/exit.go | 28 ---------------------------- cli/cmd/version.go | 3 ++- cli/main.go | 5 +++-- 5 files changed, 17 insertions(+), 35 deletions(-) delete mode 100644 cli/cmd/exit.go diff --git a/cli/cmd/compose/run.go b/cli/cmd/compose/run.go index 8729316f6..b3aaab2ba 100644 --- a/cli/cmd/compose/run.go +++ b/cli/cmd/compose/run.go @@ -27,10 +27,10 @@ import ( "github.com/mattn/go-shellwords" "github.com/spf13/cobra" + "github.com/docker/cli/cli" "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/progress" - "github.com/docker/compose-cli/cli/cmd" ) type runOptions struct { @@ -196,7 +196,11 @@ func runRun(ctx context.Context, opts runOptions) error { } exitCode, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts) if exitCode != 0 { - return cmd.ExitCodeError{ExitCode: exitCode} + errMsg := "" + if err != nil { + errMsg = err.Error() + } + return cli.StatusError{StatusCode: exitCode, Status: errMsg} } return err } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 306805468..25f427808 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -28,6 +28,7 @@ import ( "time" "github.com/compose-spec/compose-go/types" + "github.com/docker/cli/cli" "github.com/docker/compose-cli/utils" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -37,7 +38,6 @@ import ( "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/api/progress" - "github.com/docker/compose-cli/cli/cmd" "github.com/docker/compose-cli/cli/formatter" ) @@ -323,7 +323,11 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro err = eg.Wait() if exitCode != 0 { - return cmd.ExitCodeError{ExitCode: exitCode} + errMsg := "" + if err != nil { + errMsg = err.Error() + } + return cli.StatusError{StatusCode: exitCode, Status: errMsg} } return err } diff --git a/cli/cmd/exit.go b/cli/cmd/exit.go deleted file mode 100644 index fa45ad889..000000000 --- a/cli/cmd/exit.go +++ /dev/null @@ -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) -} diff --git a/cli/cmd/version.go b/cli/cmd/version.go index 878d0b1e5..aafa40e92 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" + "github.com/docker/cli/cli" "github.com/docker/compose-cli/cli/formatter" "github.com/docker/compose-cli/cli/mobycli" "github.com/docker/compose-cli/internal" @@ -39,7 +40,7 @@ func VersionCommand() *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { err := runVersion(cmd) if err != nil { - return ExitCodeError{ExitCode: 1} + return cli.StatusError{StatusCode: 1, Status: err.Error()} } return nil }, diff --git a/cli/main.go b/cli/main.go index e6bbe0b03..61a23076a 100644 --- a/cli/main.go +++ b/cli/main.go @@ -28,6 +28,7 @@ import ( "syscall" "time" + "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" cliconfig "github.com/docker/cli/cli/config" cliflags "github.com/docker/cli/cli/flags" @@ -286,9 +287,9 @@ $ docker context create %s `, cc.Type(), store.EcsContextType), ctype) } 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) - os.Exit(exit.ExitCode) + os.Exit(exit.StatusCode) } metrics.Track(ctype, os.Args[1:], metrics.FailureStatus)