k8s: Extract common validators for DRY code
[integration.git] / test / security / k8s / src / check / validators / master / boolean / boolean.go
1 package boolean
2
3 import (
4         "strings"
5 )
6
7 // IsSingleFlagPresent checks presence of selected flag and whether it was used once.
8 func IsSingleFlagPresent(flag string, params []string) bool {
9         found := filterFlags(params, flag)
10         if len(found) != 1 {
11                 return false
12         }
13         return true
14 }
15
16 // IsFlagAbsent checks absence of selected flag in parameters.
17 func IsFlagAbsent(flag string, params []string) bool {
18         found := filterFlags(params, flag)
19         if len(found) != 0 {
20                 return false
21         }
22         return true
23 }
24
25 // filterFlags returns all occurrences of selected flag.
26 func filterFlags(strs []string, flag string) []string {
27         var filtered []string
28         for _, str := range strs {
29                 if strings.HasPrefix(str, flag) {
30                         filtered = append(filtered, str)
31                 }
32         }
33         return filtered
34 }