Skip to content

Commit d2df8dd

Browse files
committed
feat(core): add option to return specific error codes
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent f92de47 commit d2df8dd

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

internal/core/bootstrap.go

+5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
125125
if _, ok := err.(*interactive.InterruptError); ok {
126126
return 130, nil, err
127127
}
128+
if cliErr, ok := err.(*CliError); ok {
129+
if cliErr.Code != 0 {
130+
return cliErr.Code, nil, nil
131+
}
132+
}
128133
printErr := meta.Printer.Print(err, nil)
129134
if printErr != nil {
130135
_, _ = fmt.Fprintln(os.Stderr, err)

internal/core/bootstrap_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,21 @@ func TestInterruptError(t *testing.T) {
2727
Cmd: "scw test interrupt error",
2828
Check: TestCheckExitCode(130),
2929
}))
30+
t.Run("exit-code", Test(&TestConfig{
31+
Commands: NewCommands(
32+
&Command{
33+
Namespace: "test",
34+
Resource: "code",
35+
Verb: "error",
36+
ArgsType: reflect.TypeOf(args.RawArgs{}),
37+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
38+
return nil, &CliError{Code: 99}
39+
},
40+
},
41+
),
42+
UseE2EClient: true,
43+
DisableParallel: true, // because e2e client is used
44+
Cmd: "scw test code error",
45+
Check: TestCheckExitCode(99),
46+
}))
3047
}

internal/core/error.go

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ type CliError struct {
2323

2424
Details string
2525
Hint string
26+
27+
// Code allows to return an error code from the main binary. If different than 0,
28+
// it does not print the error, only exits with this code.
29+
Code int
2630
}
2731

2832
func (s *CliError) Error() string {

0 commit comments

Comments
 (0)