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 <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2021-04-07 11:39:31 +02:00
parent 80c82415d9
commit 86f5e311d7
5 changed files with 17 additions and 35 deletions

View File

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

View File

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

View File

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

View File

@ -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
},

View File

@ -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 <name>`, 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)