forked from aboutcode-org/vulnerablecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag.go
More file actions
30 lines (24 loc) · 957 Bytes
/
flag.go
File metadata and controls
30 lines (24 loc) · 957 Bytes
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
package validator
import (
"fmt"
"reflect"
"strings"
"golang.org/x/exp/slices"
"github.com/orcasecurity/shiftleft-cli/lib/utils"
)
type FlagDependencyValidator[T any] struct {
}
func (v FlagDependencyValidator[T]) Validate(cmdOptions T, dependentField string, dependencyField string, allowedValues []string) error {
cmd := reflect.ValueOf(cmdOptions)
dependentFieldValue := reflect.Indirect(cmd).FieldByName(dependentField)
dependencyFieldValue := reflect.Indirect(cmd).FieldByName(dependencyField)
if slices.Contains(allowedValues, "") && dependencyFieldValue.IsZero() {
return nil
}
allowedValues = utils.RemoveFromSlice(allowedValues, "")
if !dependentFieldValue.IsZero() && !slices.Contains(allowedValues, dependencyFieldValue.String()) {
allowedValues := strings.Join(allowedValues, ",")
return fmt.Errorf("input error - '%s' option can be used only with %s=%s", dependentField, dependencyField, allowedValues)
}
return nil
}