From 3721fc313dd31b7e4d4d5d04b5a8d6c8dec0c521 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sat, 8 Mar 2025 16:56:57 +0800 Subject: [PATCH] feat: validate configuration fields in Exec method - Remove the import of the errors package - Add a validate method to the Config type to check for missing fields - Update the Exec method to use the new validate method for configuration validation Signed-off-by: appleboy --- plugin.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/plugin.go b/plugin.go index 8dd9d3d..19df89d 100644 --- a/plugin.go +++ b/plugin.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "mime/multipart" @@ -134,6 +133,22 @@ type ( } ) +func (c *Config) validate() error { + var missingFields []string + + if c.WebhookID == "" { + missingFields = append(missingFields, "WebhookID") + } + if c.WebhookToken == "" { + missingFields = append(missingFields, "WebhookToken") + } + + if len(missingFields) > 0 { + return fmt.Errorf("missing discord config: %s", strings.Join(missingFields, ", ")) + } + return nil +} + func templateMessage(t string, plugin Plugin) (string, error) { return template.RenderTrim(t, plugin) } @@ -172,8 +187,8 @@ func newfileUploadRequest(uri string, params map[string]string, paramName, path // Exec executes the plugin. func (p *Plugin) Exec(ctx context.Context) error { - if p.Config.WebhookID == "" || p.Config.WebhookToken == "" { - return errors.New("missing discord config") + if err := p.Config.validate(); err != nil { + return fmt.Errorf("failed to validate config: %w", err) } if len(p.Config.Message) == 0 {