-
Notifications
You must be signed in to change notification settings - Fork 471
/
config.go
72 lines (61 loc) · 1.39 KB
/
config.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
package util
import (
"fmt"
"reflect"
"strings"
"github.com/oleiade/reflections"
)
type Config struct {
IgnoreList []string
Timeout int
AllowInsecure bool
NoFollowRedirects bool
Server string
Username string
Password string
}
type OutputConfig struct {
FormatOptions []string
}
type format string
const (
JSON format = "json"
YAML format = "yaml"
)
func ValidateSections(unmarshal func(interface{}) error, i interface{}, whitelist map[string]bool) error {
// Get generic input
var toValidate map[string]map[string]interface{}
if err := unmarshal(&toValidate); err != nil {
return err
}
// Run input through whitelist
typ := reflect.TypeOf(i)
typs := strings.Split(typ.String(), ".")[1]
for id, v := range toValidate {
for k, _ := range v {
if !whitelist[k] {
return fmt.Errorf("Invalid Attribute for %s:%s: %s", typs, id, k)
}
}
}
return nil
}
func WhitelistAttrs(i interface{}, format format) (map[string]bool, error) {
validAttrs := make(map[string]bool)
tags, err := reflections.Tags(i, string(format))
if err != nil {
return nil, err
}
for _, v := range tags {
validAttrs[strings.Split(v, ",")[0]] = true
}
return validAttrs, nil
}
func IsValueInList(value string, list []string) bool {
for _, v := range list {
if strings.ToLower(v) == strings.ToLower(value) {
return true
}
}
return false
}