generated from padok-team/yatas-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (116 loc) · 3.15 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
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
package main
import (
"encoding/gob"
"os"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/padok-team/yatas/plugins/commons"
)
type YatasPlugin struct {
logger hclog.Logger
}
type GPTPlugin struct {
apiKey string
model string
prompt string
forEachTest bool
onlyFailed bool
}
// Don't remove this function
func (g *YatasPlugin) Run(c *commons.Config) []commons.Tests {
g.logger.Debug("message from Yatas GPT Plugin")
var err error
if err != nil {
panic(err)
}
var checksAll []commons.Tests
checks, err := runPlugin(g, c, "gpt")
if err != nil {
g.logger.Error("Error running plugins", "error", err)
}
checksAll = append(checksAll, checks...)
return checksAll
}
func UnmarshalGPT(g *YatasPlugin, c *commons.Config) (GPTPlugin, error) {
var gptCredentials GPTPlugin
for _, r := range c.PluginConfig {
gptFound := false
for key, value := range r {
switch key {
case "pluginName":
if value == "gpt" {
gptFound = true
}
case "config":
g.logger.Debug("🔎Found config")
for _, v := range value.([]interface{}) {
g.logger.Debug("🔎")
g.logger.Debug("%v", v)
for keys, values := range v.(map[string]interface{}) {
switch keys {
case "apiKey":
gptCredentials.apiKey = values.(string)
case "model":
gptCredentials.model = values.(string)
case "prompt":
gptCredentials.prompt = values.(string)
case "forEachTest":
gptCredentials.forEachTest = values.(bool)
case "onlyFailed":
gptCredentials.onlyFailed = values.(bool)
}
}
}
}
}
if gptFound {
g.logger.Debug("GPT configuration found")
}
}
g.logger.Debug("✅")
g.logger.Debug("%v", gptCredentials)
return gptCredentials, nil
}
// handshakeConfigs are used to just do a basic handshake between
// a plugin and host. If the handshake fails, a user friendly error is shown.
// This prevents users from executing bad plugins or executing a plugin
// directory. It is a UX feature, not a security feature.
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 2,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
func main() {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Output: os.Stderr,
JSONFormat: true,
})
yatasPlugin := &YatasPlugin{
logger: logger,
}
// pluginMap is the map of plugins we can dispense.
// Name of your plugin
var pluginMap = map[string]plugin.Plugin{
"gpt": &commons.YatasPlugin{Impl: yatasPlugin},
}
logger.Debug("message from plugin", "foo", "bar")
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
})
}
// Function that runs the checks or things to dot
func runPlugin(g *YatasPlugin, c *commons.Config, plugin string) ([]commons.Tests, error) {
var checksAll []commons.Tests
// Run the checks here
var gptCredentials GPTPlugin
gptCredentials, err := UnmarshalGPT(g, c)
if err != nil {
g.logger.Error("Error unmarshaling GPT config", "error", err)
}
generateReportChat(g, gptCredentials, c)
return checksAll, nil
}