From ffccfacf0796422a89815d62d1b679a79a559e27 Mon Sep 17 00:00:00 2001 From: Ian Booth Date: Thu, 8 Feb 2024 19:35:26 +1000 Subject: [PATCH] Delete the RpcPassThroughError from here and use the one in utils --- README.md | 6 +++--- cmd.go | 30 +++--------------------------- cmd_test.go | 3 ++- supercommand.go | 3 ++- 4 files changed, 10 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index f05934c4..e931cb50 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ func IsErrSilent(err error) bool IsErrSilent returns whether the error should be logged from cmd.Main. -## func IsRcPassthroughError +## func utils.IsRcPassthroughError ``` go func IsRcPassthroughError(err error) bool ``` @@ -92,7 +92,7 @@ by the callers of a command. This way the logged output can also be displayed otherwise, e.g. on the screen. -## func NewRcPassthroughError +## func utils.NewRcPassthroughError ``` go func NewRcPassthroughError(code int) error ``` @@ -628,7 +628,7 @@ Write formats and outputs the value as directed by the --format and -## type RcPassthroughError +## type utils.RcPassthroughError ``` go type RcPassthroughError struct { Code int diff --git a/cmd.go b/cmd.go index ead01bbb..b748baf9 100644 --- a/cmd.go +++ b/cmd.go @@ -22,30 +22,6 @@ import ( "github.com/juju/utils/v4" ) -// RcPassthroughError indicates that a Juju plugin command exited with a -// non-zero exit code. This error is used to exit with the return code. -type RcPassthroughError struct { - Code int -} - -// Error implements error. -func (e *RcPassthroughError) Error() string { - return fmt.Sprintf("subprocess encountered error code %v", e.Code) -} - -// IsRcPassthroughError returns whether the error is an RcPassthroughError. -func IsRcPassthroughError(err error) bool { - _, ok := err.(*RcPassthroughError) - return ok -} - -// NewRcPassthroughError creates an error that will have the code used at the -// return code from the cmd.Main function rather than the default of 1 if -// there is an error. -func NewRcPassthroughError(code int) error { - return &RcPassthroughError{code} -} - // ErrSilent can be returned from Run to signal that Main should exit with // code 1 without producing error output. var ErrSilent = errors.New("cmd: error out silently") @@ -55,7 +31,7 @@ func IsErrSilent(err error) bool { if err == ErrSilent { return true } - if _, ok := err.(*RcPassthroughError); ok { + if utils.IsRcPassthroughError(err) { return true } return false @@ -466,8 +442,8 @@ func Main(c Command, ctx *Context, args []string) int { return rc } if err := c.Run(ctx); err != nil { - if IsRcPassthroughError(err) { - return err.(*RcPassthroughError).Code + if utils.IsRcPassthroughError(err) { + return err.(*utils.RcPassthroughError).Code } if err != ErrSilent { WriteError(ctx.Stderr, err) diff --git a/cmd_test.go b/cmd_test.go index ea693b96..c3002681 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -13,6 +13,7 @@ import ( "github.com/juju/loggo/v2" jc "github.com/juju/testing/checkers" + "github.com/juju/utils/v4" gc "gopkg.in/check.v1" "github.com/juju/gnuflag" @@ -214,7 +215,7 @@ func (s *CmdSuite) TestZeroOrOneArgs(c *gc.C) { func (s *CmdSuite) TestIsErrSilent(c *gc.C) { c.Assert(cmd.IsErrSilent(cmd.ErrSilent), gc.Equals, true) - c.Assert(cmd.IsErrSilent(cmd.NewRcPassthroughError(99)), gc.Equals, true) + c.Assert(cmd.IsErrSilent(utils.NewRcPassthroughError(99)), gc.Equals, true) c.Assert(cmd.IsErrSilent(fmt.Errorf("noisy")), gc.Equals, false) } diff --git a/supercommand.go b/supercommand.go index ded668ef..3317d2aa 100644 --- a/supercommand.go +++ b/supercommand.go @@ -12,6 +12,7 @@ import ( "github.com/juju/errors" "github.com/juju/gnuflag" "github.com/juju/loggo/v2" + "github.com/juju/utils/v4" ) var logger = loggo.GetLogger("cmd") @@ -549,7 +550,7 @@ func (c *SuperCommand) Run(ctx *Context) error { logger.Debugf("error stack: \n%v", errors.ErrorStack(err)) // Err has been logged above, we can make the err silent so it does not log again in cmd/main - if !IsRcPassthroughError(err) { + if !utils.IsRcPassthroughError(err) { err = ErrSilent } } else {