-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
90 lines (75 loc) · 2.17 KB
/
plugin.go
File metadata and controls
90 lines (75 loc) · 2.17 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/reglet-dev/reglet-plugin-sdk/application/plugin"
"github.com/reglet-dev/reglet-plugin-sdk/domain/entities"
"github.com/reglet-dev/reglet-plugin-sdk/infrastructure/wasm"
"github.com/reglet-dev/reglet/plugins/command/core"
// Import services to trigger auto-registration
_ "github.com/reglet-dev/reglet/plugins/command/services"
)
type commandPlugin struct{}
func (p *commandPlugin) Manifest(ctx context.Context) (*entities.Manifest, error) {
return core.Plugin.Manifest(), nil
}
func (p *commandPlugin) Check(ctx context.Context, configBytes []byte) (*entities.Result, error) {
// Parse config
var cfgStruct core.CommandConfig
if err := json.Unmarshal(configBytes, &cfgStruct); err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
cfg := &cfgStruct
// Maps to single operation "Execute"
handler, ok := core.Plugin.GetHandler("execution", "execute")
if !ok {
return entities.ResultErrorPtr("configuration", "Unknown operation"), nil
}
req := &plugin.Request{
Client: wasm.NewExecAdapter(),
Config: cfg,
Raw: configBytes,
}
res, err := handler(ctx, req)
if err != nil {
return nil, err
}
// Validation
if res.Status == entities.ResultStatusSuccess && res.Data != nil {
// Exit code check
actualExit := 0
if val, ok := res.Data["exit_code"]; ok {
switch v := val.(type) {
case int:
actualExit = v
case float64:
actualExit = int(v)
}
}
if actualExit != cfg.ExpectedExit {
res.Status = entities.ResultStatusFailure
res.Error = &entities.ErrorDetail{
Message: fmt.Sprintf("Exit code mismatch: expected %d, got %d", cfg.ExpectedExit, actualExit),
Type: "validation",
}
}
// Output check
if cfg.ExpectedOutput != "" {
stdout, _ := res.Data["stdout"].(string)
if !strings.Contains(stdout, cfg.ExpectedOutput) {
res.Status = entities.ResultStatusFailure
res.Error = &entities.ErrorDetail{
Message: fmt.Sprintf("Output mismatch: expected '%s' in stdout", cfg.ExpectedOutput),
Type: "validation",
}
}
}
}
return res, nil
}
func init() {
plugin.Register(&commandPlugin{})
}
func main() {}