-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (83 loc) · 1.82 KB
/
main.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
package main
import (
"regexp"
"strconv"
"strings"
"github.com/hashicorp/go-plugin"
"github.com/mmcquillan/hex-plugin"
)
type HexValidate struct {
}
func (g *HexValidate) Perform(args hexplugin.Arguments) (resp hexplugin.Response) {
// initialize return values
var output = ""
var success = true
// make sure exists
if args.Command == "" {
success = false
}
// match type
if args.Config["type"] != "" {
switch args.Config["type"] {
case "string":
// everything is a string
case "int":
_, err := strconv.ParseInt(args.Command, 10, 32)
if err != nil {
success = false
}
case "int64":
_, err := strconv.ParseInt(args.Command, 10, 64)
if err != nil {
success = false
}
case "float":
_, err := strconv.ParseFloat(args.Command, 64)
if err != nil {
success = false
}
case "bool":
_, err := strconv.ParseBool(args.Command)
if err != nil {
success = false
}
}
}
// match regular expression
if args.Config["match_re"] != "" {
pattern := strings.Replace(args.Config["match_re"], "/", "", -1)
regx := regexp.MustCompile(pattern)
success = regx.MatchString(args.Command)
}
// match list
if args.Config["match_list"] != "" {
match := false
for _, member := range strings.Split(args.Config["match_list"], ",") {
member = strings.TrimSpace(member)
if member == args.Command {
match = true
}
}
success = match
}
// evaluate results
if success {
output = args.Config["success"]
} else {
output = args.Config["failure"]
}
resp = hexplugin.Response{
Output: output,
Success: success,
}
return resp
}
func main() {
var pluginMap = map[string]plugin.Plugin{
"action": &hexplugin.HexPlugin{Impl: &HexValidate{}},
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: hexplugin.GetHandshakeConfig(),
Plugins: pluginMap,
})
}