/
helpers.go
177 lines (157 loc) · 4.71 KB
/
helpers.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package options
import (
"fmt"
"os"
"sort"
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
)
const (
// DefaultErrorExitCode is the default exit code in case of an error
DefaultErrorExitCode = 1
// DefaultSuggestionsMinimumDistance default distance when generating suggestions if an option/arg is wrong
DefaultSuggestionsMinimumDistance = 2
)
var (
fatalErrHandler = fatal
// ErrExit can be used to exit with a non 0 exit code without any error message
ErrExit = fmt.Errorf("exit")
)
func InvalidOption(name string, value string, values []string) error {
suggestions := SuggestionsFor(value, values, DefaultSuggestionsMinimumDistance)
if len(suggestions) > 0 {
if len(suggestions) == 1 {
return InvalidOptionf(name, value, "Did you mean: --%s %s", name, termcolor.ColorInfo(suggestions[0]))
}
return InvalidOptionf(name, value, "Did you mean one of: %s", termcolor.ColorInfo(strings.Join(suggestions, ", ")))
}
sort.Strings(values)
return InvalidOptionf(name, value, "Possible values: %s", strings.Join(values, ", "))
}
func InvalidArg(value string, values []string) error {
suggestions := SuggestionsFor(value, values, DefaultSuggestionsMinimumDistance)
if len(suggestions) > 0 {
if len(suggestions) == 1 {
return InvalidArgf(value, "Did you mean: %s", suggestions[0])
}
return InvalidArgf(value, "Did you mean one of: %s", strings.Join(suggestions, ", "))
}
sort.Strings(values)
return InvalidArgf(value, "Possible values: %s", strings.Join(values, ", "))
}
func InvalidArgError(value string, err error) error {
return InvalidArgf(value, "%s", err)
}
func InvalidArgf(value string, message string, a ...interface{}) error {
text := fmt.Sprintf(message, a...)
return fmt.Errorf("Invalid argument: %s\n%s", termcolor.ColorInfo(value), text)
}
// InvalidOptionf returns an error that shows the invalid option.
func InvalidOptionf(option string, value interface{}, message string, a ...interface{}) error {
text := fmt.Sprintf(message, a...)
return fmt.Errorf("invalid option: --%s %v\n%s", option, value, text)
}
// MissingOption reports a missing command line option using the full name expression.
func MissingOption(name string) error {
return fmt.Errorf("missing option: --%s", name)
}
// CheckErr prints a user friendly error to STDERR and exits with a non-zero exit code.
func CheckErr(err error) {
checkErr(err, fatalErrHandler)
}
// checkErr formats a given error as a string and calls the passed handleErr func.
func checkErr(err error, handleErr func(string, int)) {
switch {
case err == nil:
return
case err == ErrExit:
handleErr("", DefaultErrorExitCode)
return
default:
handleErr(err.Error(), DefaultErrorExitCode)
}
}
func fatal(msg string, code int) {
if len(msg) > 0 {
// add newline if needed
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprint(os.Stderr, msg)
}
os.Exit(code)
}
func SuggestionsFor(typedName string, values []string, suggestionsMinimumDistance int, explicitSuggestions ...string) []string {
suggestions := []string{}
for _, value := range values {
levenshteinDistance := ld(typedName, value, true)
suggestByLevenshtein := levenshteinDistance <= suggestionsMinimumDistance
suggestByPrefix := strings.HasPrefix(strings.ToLower(value), strings.ToLower(typedName))
if suggestByLevenshtein || suggestByPrefix {
suggestions = append(suggestions, value)
}
for _, explicitSuggestion := range explicitSuggestions {
if strings.EqualFold(typedName, explicitSuggestion) {
suggestions = append(suggestions, value)
}
}
}
return suggestions
}
// ld compares two strings and returns the levenshtein distance between them.
//
// this was copied from vendor/github.com/spf13/cobra/command.go as its not public
func ld(s, t string, ignoreCase bool) int {
if ignoreCase {
s = strings.ToLower(s)
t = strings.ToLower(t)
}
d := make([][]int, len(s)+1)
for i := range d {
d[i] = make([]int, len(t)+1)
}
for i := range d {
d[i][0] = i
}
for j := range d[0] {
d[0][j] = j
}
for j := 1; j <= len(t); j++ {
for i := 1; i <= len(s); i++ {
if s[i-1] == t[j-1] {
d[i][j] = d[i-1][j-1]
} else {
min := d[i-1][j]
if d[i][j-1] < min {
min = d[i][j-1]
}
if d[i-1][j-1] < min {
min = d[i-1][j-1]
}
d[i][j] = min + 1
}
}
}
return d[len(s)][len(t)]
}
// ArgumentsOptionValue returns the argument
func ArgumentsOptionValue(args []string, flag string, option string) string {
flags := []string{"--" + option, "-" + flag}
for i := 0; i < len(args); i++ {
arg := args[i]
for _, f := range flags {
feq := f + "="
if strings.HasPrefix(arg, feq) {
return arg[len(feq):]
}
if arg == f {
j := i + 1
if j < len(args) {
return args[j]
}
return ""
}
}
}
return ""
}