From c58e6ef7e7569e0eb69011c7209a0ae6e45de762 Mon Sep 17 00:00:00 2001 From: hirenko-v <132065511+hirenko-v@users.noreply.github.com> Date: Thu, 19 Sep 2024 13:22:34 +0300 Subject: [PATCH] fx --- cmd/echo/README.md | 11 ----- cmd/echo/main.go | 97 ----------------------------------------- cmd/forwarder/README.md | 5 --- cmd/forwarder/main.go | 64 --------------------------- cmd/msg/main.go | 10 +---- cmd/ticker/README.md | 11 ----- cmd/ticker/main.go | 87 ------------------------------------ 7 files changed, 2 insertions(+), 283 deletions(-) delete mode 100644 cmd/echo/README.md delete mode 100644 cmd/echo/main.go delete mode 100644 cmd/forwarder/README.md delete mode 100644 cmd/forwarder/main.go delete mode 100644 cmd/ticker/README.md delete mode 100644 cmd/ticker/main.go diff --git a/cmd/echo/README.md b/cmd/echo/README.md deleted file mode 100644 index a0e3143..0000000 --- a/cmd/echo/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Echo executor - -Echo is an example Botkube executor plugin written in Go. It's not meant for production usage. - -## Configuration parameters - -The configuration should be specified in the YAML format. Such parameters are supported: - -```yaml -transformResponseToUpperCase: true # Default is 'false'. -``` diff --git a/cmd/echo/main.go b/cmd/echo/main.go deleted file mode 100644 index 3222c2c..0000000 --- a/cmd/echo/main.go +++ /dev/null @@ -1,97 +0,0 @@ -package main - -import ( - "context" - "strings" - - "github.com/MakeNowJust/heredoc" - "github.com/hashicorp/go-plugin" - "github.com/kubeshop/botkube/pkg/api" - "github.com/kubeshop/botkube/pkg/api/executor" - pluginx "github.com/kubeshop/botkube/pkg/plugin" -) - -const description = "Echo sends back the command that was specified." - -// 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: description, - JSONSchema: api.JSONSchema{ - Value: heredoc.Docf(`{ - "$schema": "http://json-schema.org/draft-04/schema#", - "title": "echo", - "description": "%s", - "type": "object", - "properties": { - "formatOptions": { - "description": "Options to format echoed string", - "type": "array", - "items": { - "type": "string", - "enum": [ "bold", "italic" ] - } - } - }, - "additionalProperties": false - }`, description), - }, - }, nil -} - -// Execute returns a given command as a response. -// -//nolint:gocritic //hugeParam: in is heavy (80 bytes); consider passing it by pointer -func (EchoExecutor) Execute(_ context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) { - var cfg Config - err := pluginx.MergeExecutorConfigs(in.Configs, &cfg) - if err != nil { - return executor.ExecuteOutput{}, err - } - - response := in.Command - if cfg.TransformResponseToUpperCase != nil && *cfg.TransformResponseToUpperCase { - response = strings.ToUpper(response) - } - - return executor.ExecuteOutput{ - Message: api.NewCodeBlockMessage(response, true), - }, nil -} - -func (EchoExecutor) Help(context.Context) (api.Message, error) { - btnBuilder := api.NewMessageButtonBuilder() - return api.Message{ - Sections: []api.Section{ - { - Base: api.Base{ - Header: "Run `echo` commands", - Description: description, - }, - Buttons: []api.Button{ - btnBuilder.ForCommandWithDescCmd("Run", "echo 'hello world'"), - }, - }, - }, - }, nil -} - -func main() { - executor.Serve(map[string]plugin.Plugin{ - "echo": &executor.Plugin{ - Executor: &EchoExecutor{}, - }, - }) -} diff --git a/cmd/forwarder/README.md b/cmd/forwarder/README.md deleted file mode 100644 index 2664c7c..0000000 --- a/cmd/forwarder/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Forwarder source - -Forwarder is an example Botkube source plugin written in Go. It's not meant for production usage. - -It simply emits an event every time a message is sent as an incoming webhook request. The message is extracted from the `message` payload property. diff --git a/cmd/forwarder/main.go b/cmd/forwarder/main.go deleted file mode 100644 index 87136a6..0000000 --- a/cmd/forwarder/main.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - - "github.com/hashicorp/go-plugin" - "github.com/kubeshop/botkube/pkg/api" - "github.com/kubeshop/botkube/pkg/api/source" -) - -var _ source.Source = (*Forwarder)(nil) - -// version is set via ldflags by GoReleaser. -var version = "dev" - -// Forwarder implements the Botkube executor plugin interface. -type Forwarder struct { - // specify that the source doesn't handle streaming events - source.StreamUnimplemented -} - -// Metadata returns details about the Forwarder plugin. -func (Forwarder) Metadata(_ context.Context) (api.MetadataOutput, error) { - return api.MetadataOutput{ - Version: version, - Description: "Emits an event every time a message is sent as an incoming webhook request", - }, nil -} - -// payload is the incoming webhook payload. -type payload struct { - Message string `json:"message"` -} - -// HandleExternalRequest handles incoming payload and returns an event based on it. -// -//nolint:gocritic // hugeParam: in is heavy (104 bytes); consider passing it by pointer -func (Forwarder) HandleExternalRequest(_ context.Context, in source.ExternalRequestInput) (source.ExternalRequestOutput, error) { - var p payload - err := json.Unmarshal(in.Payload, &p) - if err != nil { - return source.ExternalRequestOutput{}, fmt.Errorf("while unmarshaling payload: %w", err) - } - - if p.Message == "" { - return source.ExternalRequestOutput{}, fmt.Errorf("message cannot be empty") - } - - msg := fmt.Sprintf("*Incoming webhook event:* %s", p.Message) - return source.ExternalRequestOutput{ - Event: source.Event{ - Message: api.NewPlaintextMessage(msg, true), - }, - }, nil -} -func main() { - source.Serve(map[string]plugin.Plugin{ - "forwarder": &source.Plugin{ - Source: &Forwarder{}, - }, - }) -} diff --git a/cmd/msg/main.go b/cmd/msg/main.go index 83dad28..81d5695 100644 --- a/cmd/msg/main.go +++ b/cmd/msg/main.go @@ -55,11 +55,6 @@ func initialMessages() executor.ExecuteOutput { return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd) } - return executor.ExecuteOutput{ - Message: api.Message{ - BaseBody: api.Body{ - Plaintext: "Showcases interactive message capabilities", - }, // Define a list of jobs jobs := []api.OptionItem{ {Name: "Job1", Value: "job1"}, @@ -89,7 +84,7 @@ func initialMessages() executor.ExecuteOutput { Command: cmdPrefix("select job"), OptionGroups: []api.OptionGroup{ { - Name: "Jobs", + Name: "Jobs", Options: jobs, }, }, @@ -107,7 +102,7 @@ func initialMessages() executor.ExecuteOutput { Command: cmdPrefix("select param"), OptionGroups: []api.OptionGroup{ { - Name: "Parameters", + Name: "Parameters", Options: params, }, }, @@ -126,7 +121,6 @@ func initialMessages() executor.ExecuteOutput { }, }, }, - OnlyVisibleForYou: true, ReplaceOriginal: false, }, diff --git a/cmd/ticker/README.md b/cmd/ticker/README.md deleted file mode 100644 index e72dba1..0000000 --- a/cmd/ticker/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Ticker source - -Ticker is an example Botkube source plugin written in Go. It's not meant for production usage. - -## Configuration parameters - -The configuration should be specified in the YAML format. Such parameters are supported: - -```yaml -interval: 1m # Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". -``` diff --git a/cmd/ticker/main.go b/cmd/ticker/main.go deleted file mode 100644 index 2fba97f..0000000 --- a/cmd/ticker/main.go +++ /dev/null @@ -1,87 +0,0 @@ -package main - -import ( - "context" - "fmt" - "time" - - "github.com/hashicorp/go-plugin" - "github.com/kubeshop/botkube/pkg/api" - "github.com/kubeshop/botkube/pkg/api/source" - pluginx "github.com/kubeshop/botkube/pkg/plugin" -) - -var _ source.Source = (*Ticker)(nil) - -// version is set via ldflags by GoReleaser. -var version = "dev" - -// Config holds the source configuration. -type Config struct { - Interval time.Duration `yaml:"interval,omitempty"` -} - -// Ticker implements the Botkube executor plugin interface. -type Ticker struct { - // specify that the source doesn't handle external requests - source.HandleExternalRequestUnimplemented -} - -// Metadata returns details about the Ticker plugin. -func (Ticker) Metadata(_ context.Context) (api.MetadataOutput, error) { - return api.MetadataOutput{ - Version: version, - Description: "Emits an event at a specified interval", - }, nil -} - -// Stream sends an event after configured time duration. -// -//nolint:gocritic // hugeParam: in is heavy (120 bytes); consider passing it by pointer -func (Ticker) Stream(ctx context.Context, in source.StreamInput) (source.StreamOutput, error) { - cfg, err := mergeConfigs(in.Configs) - if err != nil { - return source.StreamOutput{}, err - } - - ticker := time.NewTicker(cfg.Interval) - out := source.StreamOutput{ - Event: make(chan source.Event), - } - - go func() { - for { - select { - case <-ctx.Done(): - ticker.Stop() - case <-ticker.C: - out.Event <- source.Event{ - Message: api.NewPlaintextMessage("Ticker Event", true), - } - } - } - }() - - return out, nil -} - -func main() { - source.Serve(map[string]plugin.Plugin{ - "ticker": &source.Plugin{ - Source: &Ticker{}, - }, - }) -} - -func mergeConfigs(configs []*source.Config) (Config, error) { - defaults := Config{ - Interval: time.Minute, - } - - var cfg Config - err := pluginx.MergeSourceConfigsWithDefaults(defaults, configs, &cfg) - if err != nil { - return Config{}, fmt.Errorf("while parsing input configuration: %w", err) - } - return cfg, nil -}