-
Notifications
You must be signed in to change notification settings - Fork 257
/
validators.go
193 lines (169 loc) · 5.27 KB
/
validators.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package metaquery
import (
"fmt"
"strings"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/pkg/cmdconfig"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/utils"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// ValidationResult :: response for Validate
type ValidationResult struct {
Err error
Message string
ShouldRun bool
}
type validator func(val []string) ValidationResult
// Validate :: validate a full metaquery along with arguments - we can return err & validationResult
func Validate(query string) ValidationResult {
query = strings.TrimSuffix(query, ";")
// get the meta query
cmd, args := getCmdAndArgs(query)
validatorFunction := metaQueryDefinitions[cmd].validator
if validatorFunction != nil {
return validatorFunction(args)
}
return ValidationResult{Err: fmt.Errorf("'%s' is not a known command", query)}
}
func titleSentenceCase(title string) string {
caser := cases.Title(language.English)
titleSegments := strings.SplitN(title, "-", 2)
if len(titleSegments) == 1 {
return caser.String(title)
}
titleSegments = []string{caser.String(titleSegments[0]), titleSegments[1]}
return strings.Join(titleSegments, "-")
}
func booleanValidator(metaquery string, validators ...validator) validator {
return func(args []string) ValidationResult {
// Error: argument required multi-line mode is off. You can enable it with: .multi on
// headers mode is off. You can enable it with: .headers on
// timing mode is off. You can enable it with: .timing on
title := titleSentenceCase(metaQueryDefinitions[metaquery].title)
numArgs := len(args)
if numArgs == 0 {
// get the current status of this mode (convert metaquery name into arg name)
// NOTE - request second arg from cast even though we donl;t use it - to avoid panic
currentStatus := cmdconfig.Viper().GetBool(constants.ArgFromMetaquery(metaquery))
// what is the new status (the opposite)
newStatus := !currentStatus
// convert current and new status to on/off
currentStatusString := constants.BoolToOnOff(currentStatus)
newStatusString := constants.BoolToOnOff(newStatus)
// what is the action to get to the new status
actionString := constants.BoolToEnableDisable(newStatus)
return ValidationResult{
Message: fmt.Sprintf(`%s mode is %s. You can %s it with: %s.`,
title,
constants.Bold(currentStatusString),
actionString,
constants.Bold(fmt.Sprintf("%s %s", metaquery, newStatusString))),
}
}
if numArgs > 1 {
return ValidationResult{
Err: fmt.Errorf("command needs 1 argument - got %d", numArgs),
}
}
return buildValidationResult(args, validators)
}
}
func composeValidator(validators ...validator) validator {
return func(val []string) ValidationResult {
return buildValidationResult(val, validators)
}
}
func validatorFromArgsOf(cmd string) validator {
return func(val []string) ValidationResult {
metaQueryDefinition := metaQueryDefinitions[cmd]
validArgs := []string{}
for _, validArg := range metaQueryDefinition.args {
validArgs = append(validArgs, validArg.value)
}
return allowedArgValues(false, validArgs...)(val)
}
}
var atLeastNArgs = func(n int) validator {
return func(args []string) ValidationResult {
numArgs := len(args)
if numArgs < n {
return ValidationResult{
Err: fmt.Errorf("command needs at least %d %s - got %d", n, utils.Pluralize("argument", n), numArgs),
}
}
return ValidationResult{ShouldRun: true}
}
}
var atMostNArgs = func(n int) validator {
return func(args []string) ValidationResult {
numArgs := len(args)
if numArgs > n {
return ValidationResult{
Err: fmt.Errorf("command needs at most %d %s - got %d", n, utils.Pluralize("argument", n), numArgs),
}
}
return ValidationResult{ShouldRun: true}
}
}
var exactlyNArgs = func(n int) validator {
return func(args []string) ValidationResult {
numArgs := len(args)
if numArgs != n {
return ValidationResult{
Err: fmt.Errorf("command needs %d %s - got %d", n, utils.Pluralize("argument", n), numArgs),
}
}
return ValidationResult{
ShouldRun: true,
}
}
}
var noArgs = exactlyNArgs(0)
var allowedArgValues = func(caseSensitive bool, allowedValues ...string) validator {
return func(args []string) ValidationResult {
if !caseSensitive {
// convert everything to lower case
for idx, a := range args {
args[idx] = strings.ToLower(a)
}
for idx, av := range allowedValues {
allowedValues[idx] = strings.ToLower(av)
}
}
for _, arg := range args {
if !helpers.StringSliceContains(allowedValues, arg) {
return ValidationResult{
Err: fmt.Errorf("valid values for this command are %v - got %s", allowedValues, arg),
}
}
}
return ValidationResult{ShouldRun: true}
}
}
func buildValidationResult(val []string, validators []validator) ValidationResult {
var messages string
for _, v := range validators {
validate := v(val)
if validate.Message != "" {
messages = fmt.Sprintf("%s\n%s", messages, validate.Message)
}
if validate.Err != nil {
return ValidationResult{
Err: validate.Err,
Message: messages,
}
}
if !validate.ShouldRun {
return ValidationResult{
Message: messages,
ShouldRun: false,
}
}
}
return ValidationResult{
Message: messages,
ShouldRun: true,
}
}