forked from kubeshop/botkube-plugins-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.93 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
package main
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-plugin"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/executor"
"gopkg.in/yaml.v3"
)
// version is set via ldflags by GoReleaser.
var version = "dev"
// Config holds the executor configuration.
type Config struct {
TransformResponseToUpperCase *bool `yaml:"transformResponseToUpperCase,omitempty"`
}
// EchoExecutor implements the Botkube executor plugin interface.
type EchoExecutor struct{}
// Metadata returns details about the Echo plugin.
func (EchoExecutor) Metadata(context.Context) (api.MetadataOutput, error) {
return api.MetadataOutput{
Version: version,
Description: "Echo sends back the command that was specified.",
}, nil
}
// Execute returns a given command as a response.
func (EchoExecutor) Execute(_ context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
cfg, err := mergeConfigs(in.Configs)
if err != nil {
return executor.ExecuteOutput{}, err
}
response := in.Command
if cfg.TransformResponseToUpperCase != nil && *cfg.TransformResponseToUpperCase {
response = strings.ToUpper(response)
}
return executor.ExecuteOutput{
Data: fmt.Sprintf("Echo: %s", response),
}, nil
}
func main() {
executor.Serve(map[string]plugin.Plugin{
"echo": &executor.Plugin{
Executor: &EchoExecutor{},
},
})
}
// mergeConfigs merges all input configuration. In our case we don't have complex merge strategy,
// the last one that was specified wins :)
func mergeConfigs(configs []*executor.Config) (Config, error) {
finalCfg := Config{}
for _, inputCfg := range configs {
var cfg Config
err := yaml.Unmarshal(inputCfg.RawYAML, &cfg)
if err != nil {
return Config{}, fmt.Errorf("while unmarshalling YAML config: %w", err)
}
if cfg.TransformResponseToUpperCase == nil {
continue
}
finalCfg.TransformResponseToUpperCase = cfg.TransformResponseToUpperCase
}
return finalCfg, nil
}