Repository navigation
Expand file tree
/
Copy pathexit.go
More file actions
49 lines (42 loc) · 1.34 KB
/
Copy pathexit.go
File metadata and controls
49 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cli
import (
"context"
"errors"
"github.com/urfave/cli/v3"
"github.com/umatare5/cisco-wnc-cli/internal/show"
)
// Exit codes. A run that reached every controller and rendered its rows exits 0
// even when there were no rows, because an empty result is a normal answer.
const (
ExitOK = 0
ExitFailure = 1
ExitUsage = 2
ExitPartial = 3
ExitSignal = 130
)
// exitCode maps the run's outcome, and the order is load-bearing. A cli.ExitCoder comes first
// because urfave builds one itself and it matches none of this package's sentinels. The signal
// check comes next so an interrupted fan-out is not reported as a partial success.
func exitCode(ctx context.Context, err error) int {
var coder cli.ExitCoder
switch {
case errors.As(err, &coder):
return ExitUsage
case ctx.Err() != nil:
return ExitSignal
case errors.Is(err, ErrUsage):
return ExitUsage
case errors.Is(err, show.ErrPartial):
return ExitPartial
case err != nil:
return ExitFailure
default:
return ExitOK
}
}
// reported tells whether the fan-out has already written this failure to the log,
// which is the case for every outcome it classifies per controller. Printing a
// summary line on top of those would repeat what the operator just read.
func reported(err error) bool {
return errors.Is(err, show.ErrPartial) || errors.Is(err, show.ErrAllFailed)
}