File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change 1
1
package caddycmd
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
"github.com/spf13/cobra"
5
7
)
6
8
@@ -123,7 +125,23 @@ func caddyCmdToCobra(caddyCmd Command) *cobra.Command {
123
125
// in a cobra command's RunE field.
124
126
func WrapCommandFuncForCobra (f CommandFunc ) func (cmd * cobra.Command , _ []string ) error {
125
127
return func (cmd * cobra.Command , _ []string ) error {
126
- _ , err := f (Flags {cmd .Flags ()})
128
+ status , err := f (Flags {cmd .Flags ()})
129
+ if status > 1 {
130
+ cmd .SilenceErrors = true
131
+ return & customExitError {ExitCode : status , Err : err }
132
+ }
127
133
return err
128
134
}
129
135
}
136
+
137
+ type customExitError struct {
138
+ ExitCode int
139
+ Err error
140
+ }
141
+
142
+ func (e * customExitError ) Error () string {
143
+ if e .Err == nil {
144
+ return fmt .Sprintf ("exiting with status %d" , e .ExitCode )
145
+ }
146
+ return e .Err .Error ()
147
+ }
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ package caddycmd
17
17
import (
18
18
"bufio"
19
19
"bytes"
20
+ "errors"
20
21
"flag"
21
22
"fmt"
22
23
"io"
@@ -63,6 +64,10 @@ func Main() {
63
64
}
64
65
65
66
if err := rootCmd .Execute (); err != nil {
67
+ var exitError * customExitError
68
+ if errors .As (err , & exitError ) {
69
+ os .Exit (exitError .ExitCode )
70
+ }
66
71
os .Exit (1 )
67
72
}
68
73
}
You can’t perform that action at this time.
0 commit comments