Skip to content

Commit 48cf985

Browse files
authored
Merge pull request #113 from wallyworld/use-utils-passthrougherror
#113 RcPassthroughError has been moved to utils repo. Delete the one from here and use the utils one instead.
2 parents 7275770 + ffccfac commit 48cf985

File tree

4 files changed

+10
-32
lines changed

4 files changed

+10
-32
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func IsErrSilent(err error) bool
6767
IsErrSilent returns whether the error should be logged from cmd.Main.
6868

6969

70-
## func IsRcPassthroughError
70+
## func utils.IsRcPassthroughError
7171
``` go
7272
func IsRcPassthroughError(err error) bool
7373
```
@@ -92,7 +92,7 @@ by the callers of a command. This way the logged output can also
9292
be displayed otherwise, e.g. on the screen.
9393

9494

95-
## func NewRcPassthroughError
95+
## func utils.NewRcPassthroughError
9696
``` go
9797
func NewRcPassthroughError(code int) error
9898
```
@@ -628,7 +628,7 @@ Write formats and outputs the value as directed by the --format and
628628

629629

630630

631-
## type RcPassthroughError
631+
## type utils.RcPassthroughError
632632
``` go
633633
type RcPassthroughError struct {
634634
Code int

cmd.go

+3-27
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,6 @@ import (
2222
"github.com/juju/utils/v4"
2323
)
2424

25-
// RcPassthroughError indicates that a Juju plugin command exited with a
26-
// non-zero exit code. This error is used to exit with the return code.
27-
type RcPassthroughError struct {
28-
Code int
29-
}
30-
31-
// Error implements error.
32-
func (e *RcPassthroughError) Error() string {
33-
return fmt.Sprintf("subprocess encountered error code %v", e.Code)
34-
}
35-
36-
// IsRcPassthroughError returns whether the error is an RcPassthroughError.
37-
func IsRcPassthroughError(err error) bool {
38-
_, ok := err.(*RcPassthroughError)
39-
return ok
40-
}
41-
42-
// NewRcPassthroughError creates an error that will have the code used at the
43-
// return code from the cmd.Main function rather than the default of 1 if
44-
// there is an error.
45-
func NewRcPassthroughError(code int) error {
46-
return &RcPassthroughError{code}
47-
}
48-
4925
// ErrSilent can be returned from Run to signal that Main should exit with
5026
// code 1 without producing error output.
5127
var ErrSilent = errors.New("cmd: error out silently")
@@ -55,7 +31,7 @@ func IsErrSilent(err error) bool {
5531
if err == ErrSilent {
5632
return true
5733
}
58-
if _, ok := err.(*RcPassthroughError); ok {
34+
if utils.IsRcPassthroughError(err) {
5935
return true
6036
}
6137
return false
@@ -466,8 +442,8 @@ func Main(c Command, ctx *Context, args []string) int {
466442
return rc
467443
}
468444
if err := c.Run(ctx); err != nil {
469-
if IsRcPassthroughError(err) {
470-
return err.(*RcPassthroughError).Code
445+
if utils.IsRcPassthroughError(err) {
446+
return err.(*utils.RcPassthroughError).Code
471447
}
472448
if err != ErrSilent {
473449
WriteError(ctx.Stderr, err)

cmd_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/juju/loggo/v2"
1515
jc "github.com/juju/testing/checkers"
16+
"github.com/juju/utils/v4"
1617
gc "gopkg.in/check.v1"
1718

1819
"github.com/juju/gnuflag"
@@ -214,7 +215,7 @@ func (s *CmdSuite) TestZeroOrOneArgs(c *gc.C) {
214215

215216
func (s *CmdSuite) TestIsErrSilent(c *gc.C) {
216217
c.Assert(cmd.IsErrSilent(cmd.ErrSilent), gc.Equals, true)
217-
c.Assert(cmd.IsErrSilent(cmd.NewRcPassthroughError(99)), gc.Equals, true)
218+
c.Assert(cmd.IsErrSilent(utils.NewRcPassthroughError(99)), gc.Equals, true)
218219
c.Assert(cmd.IsErrSilent(fmt.Errorf("noisy")), gc.Equals, false)
219220
}
220221

supercommand.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/juju/errors"
1313
"github.com/juju/gnuflag"
1414
"github.com/juju/loggo/v2"
15+
"github.com/juju/utils/v4"
1516
)
1617

1718
var logger = loggo.GetLogger("cmd")
@@ -549,7 +550,7 @@ func (c *SuperCommand) Run(ctx *Context) error {
549550
logger.Debugf("error stack: \n%v", errors.ErrorStack(err))
550551

551552
// Err has been logged above, we can make the err silent so it does not log again in cmd/main
552-
if !IsRcPassthroughError(err) {
553+
if !utils.IsRcPassthroughError(err) {
553554
err = ErrSilent
554555
}
555556
} else {

0 commit comments

Comments
 (0)