-
Notifications
You must be signed in to change notification settings - Fork 63
/
errors.go
58 lines (47 loc) · 1.43 KB
/
errors.go
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
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 GitHub Inc.
// Copyright (c) 2022 Unikraft GmbH.
package cmdfactory
import (
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2/terminal"
)
// FlagErrorf returns a new FlagError that wraps an error produced by
// fmt.Errorf(format, args...).
func FlagErrorf(format string, args ...interface{}) error {
return FlagErrorWrap(fmt.Errorf(format, args...))
}
// FlagError returns a new FlagError that wraps the specified error.
func FlagErrorWrap(err error) error { return &FlagError{err} }
// A *FlagError indicates an error processing command-line flags or other
// arguments. Such errors cause the application to display the usage message.
type FlagError struct {
// Note: not struct{error}: only *FlagError should satisfy error.
err error
}
func (fe *FlagError) Error() string {
return fe.err.Error()
}
func (fe *FlagError) Unwrap() error {
return fe.err
}
// ErrSilent is an error that triggers exit code 1 without any error messaging
var ErrSilent = errors.New("ErrSilent")
// ErrCancel signals user-initiated cancellation
var ErrCancel = errors.New("ErrCancel")
func IsUserCancellation(err error) bool {
return errors.Is(err, ErrCancel) || errors.Is(err, terminal.InterruptErr)
}
func MutuallyExclusive(message string, conditions ...bool) error {
numTrue := 0
for _, ok := range conditions {
if ok {
numTrue++
}
}
if numTrue > 1 {
return FlagErrorf("%s", message)
}
return nil
}