From 609a252ee07520446687cc60de6ed64003ddde6f Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Sat, 26 Apr 2025 21:27:53 +0800 Subject: [PATCH] refactor: clean code --- go.mod | 2 +- .../pkg/core/notifier/providers/bark/bark.go | 1 + .../notifier/providers/dingtalk/dingtalk.go | 1 + .../core/notifier/providers/email/email.go | 1 + .../core/notifier/providers/gotify/gotify.go | 34 +++++++--------- .../pkg/core/notifier/providers/lark/lark.go | 1 + .../providers/mattermost/mattermost.go | 1 + .../notifier/providers/pushover/pushover.go | 34 +++++++--------- .../notifier/providers/pushplus/pushplus.go | 39 +++++++------------ .../providers/serverchan/serverchan.go | 1 + .../notifier/providers/telegram/telegram.go | 1 + 11 files changed, 49 insertions(+), 67 deletions(-) diff --git a/go.mod b/go.mod index 18d0a4ee..54651504 100644 --- a/go.mod +++ b/go.mod @@ -187,7 +187,7 @@ require ( github.com/ncruces/go-strftime v0.1.9 // indirect github.com/nrdcg/namesilo v0.2.1 // indirect github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect - github.com/pkg/errors v0.9.1 + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/spf13/cast v1.7.1 // indirect github.com/spf13/cobra v1.9.1 // indirect diff --git a/internal/pkg/core/notifier/providers/bark/bark.go b/internal/pkg/core/notifier/providers/bark/bark.go index c25ae3f9..ccdd5736 100644 --- a/internal/pkg/core/notifier/providers/bark/bark.go +++ b/internal/pkg/core/notifier/providers/bark/bark.go @@ -32,6 +32,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil } diff --git a/internal/pkg/core/notifier/providers/dingtalk/dingtalk.go b/internal/pkg/core/notifier/providers/dingtalk/dingtalk.go index 4f9964e9..cc394eae 100644 --- a/internal/pkg/core/notifier/providers/dingtalk/dingtalk.go +++ b/internal/pkg/core/notifier/providers/dingtalk/dingtalk.go @@ -30,6 +30,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil } diff --git a/internal/pkg/core/notifier/providers/email/email.go b/internal/pkg/core/notifier/providers/email/email.go index 0e78ba04..69d39012 100644 --- a/internal/pkg/core/notifier/providers/email/email.go +++ b/internal/pkg/core/notifier/providers/email/email.go @@ -44,6 +44,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil } diff --git a/internal/pkg/core/notifier/providers/gotify/gotify.go b/internal/pkg/core/notifier/providers/gotify/gotify.go index ad0c515e..aed6e7c8 100644 --- a/internal/pkg/core/notifier/providers/gotify/gotify.go +++ b/internal/pkg/core/notifier/providers/gotify/gotify.go @@ -9,25 +9,21 @@ import ( "log/slog" "net/http" - "github.com/pkg/errors" - "github.com/usual2970/certimate/internal/pkg/core/notifier" ) type NotifierConfig struct { - // Gotify 服务地址 - // 示例:https://gotify.example.com + // Gotify 服务地址。 Url string `json:"url"` - // Gotify Token + // Gotify Token。 Token string `json:"token"` - // Gotify 消息优先级 - Priority int64 `json:"priority"` + // Gotify 消息优先级。 + Priority int64 `json:"priority,omitempty"` } type NotifierProvider struct { - config *NotifierConfig - logger *slog.Logger - // 未来将移除 + config *NotifierConfig + logger *slog.Logger httpClient *http.Client } @@ -40,6 +36,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), httpClient: http.DefaultClient, }, nil } @@ -54,7 +51,6 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier { } func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) { - // Gotify 原生实现, notify 库没有实现, 等待合并 reqBody := &struct { Title string `json:"title"` Message string `json:"message"` @@ -65,10 +61,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s Priority: n.config.Priority, } - // Make request body, err := json.Marshal(reqBody) if err != nil { - return nil, errors.Wrap(err, "encode message body") + return nil, fmt.Errorf("gotify api error: failed to encode message body: %w", err) } req, err := http.NewRequestWithContext( @@ -78,27 +73,24 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s bytes.NewReader(body), ) if err != nil { - return nil, errors.Wrap(err, "create new request") + return nil, fmt.Errorf("gotify api error: failed to create new request: %w", err) } req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", n.config.Token)) req.Header.Set("Content-Type", "application/json; charset=utf-8") - // Send request to gotify service resp, err := n.httpClient.Do(req) if err != nil { - return nil, errors.Wrapf(err, "send request to gotify server") + return nil, fmt.Errorf("gotify api error: failed to send request: %w", err) } defer resp.Body.Close() - // Read response and verify success result, err := io.ReadAll(resp.Body) if err != nil { - return nil, errors.Wrap(err, "read response") + return nil, fmt.Errorf("gotify api error: failed to read response: %w", err) + } else if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("gotify api error: unexpected status code: %d, resp: %s", resp.StatusCode, string(result)) } - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("gotify returned status code %d: %s", resp.StatusCode, string(result)) - } return ¬ifier.NotifyResult{}, nil } diff --git a/internal/pkg/core/notifier/providers/lark/lark.go b/internal/pkg/core/notifier/providers/lark/lark.go index 09cc6444..e8ad7816 100644 --- a/internal/pkg/core/notifier/providers/lark/lark.go +++ b/internal/pkg/core/notifier/providers/lark/lark.go @@ -28,6 +28,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil } diff --git a/internal/pkg/core/notifier/providers/mattermost/mattermost.go b/internal/pkg/core/notifier/providers/mattermost/mattermost.go index 397864e9..ed3a507a 100644 --- a/internal/pkg/core/notifier/providers/mattermost/mattermost.go +++ b/internal/pkg/core/notifier/providers/mattermost/mattermost.go @@ -38,6 +38,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil } diff --git a/internal/pkg/core/notifier/providers/pushover/pushover.go b/internal/pkg/core/notifier/providers/pushover/pushover.go index 8f84dfd2..f306df1f 100644 --- a/internal/pkg/core/notifier/providers/pushover/pushover.go +++ b/internal/pkg/core/notifier/providers/pushover/pushover.go @@ -9,20 +9,19 @@ import ( "log/slog" "net/http" - "github.com/pkg/errors" - "github.com/usual2970/certimate/internal/pkg/core/notifier" ) type NotifierConfig struct { - Token string `json:"token"` // 应用 API Token - User string `json:"user"` // 用户/分组 Key + // Pushover API Token。 + Token string `json:"token"` + // 用户或分组标识。 + User string `json:"user"` } type NotifierProvider struct { - config *NotifierConfig - logger *slog.Logger - // 未来将移除 + config *NotifierConfig + logger *slog.Logger httpClient *http.Client } @@ -35,6 +34,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), httpClient: http.DefaultClient, }, nil } @@ -48,10 +48,8 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier { return n } -// Notify 发送通知 -// 参考文档:https://pushover.net/api func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) { - // 请求体 + // REF: https://pushover.net/api reqBody := &struct { Token string `json:"token"` User string `json:"user"` @@ -64,10 +62,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s Message: message, } - // Make request body, err := json.Marshal(reqBody) if err != nil { - return nil, errors.Wrap(err, "encode message body") + return nil, fmt.Errorf("pushover api error: failed to encode message body: %w", err) } req, err := http.NewRequestWithContext( @@ -77,25 +74,22 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s bytes.NewReader(body), ) if err != nil { - return nil, errors.Wrap(err, "create new request") + return nil, fmt.Errorf("pushover api error: failed to create new request: %w", err) } req.Header.Set("Content-Type", "application/json; charset=utf-8") - // Send request to pushover service resp, err := n.httpClient.Do(req) if err != nil { - return nil, errors.Wrapf(err, "send request to pushover server") + return nil, fmt.Errorf("pushover api error: failed to send request: %w", err) } defer resp.Body.Close() result, err := io.ReadAll(resp.Body) if err != nil { - return nil, errors.Wrap(err, "read response") - } - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("pushover returned status code %d: %s", resp.StatusCode, string(result)) + return nil, fmt.Errorf("pushover api error: failed to read response: %w", err) + } else if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("pushover api error: unexpected status code: %d, resp: %s", resp.StatusCode, string(result)) } return ¬ifier.NotifyResult{}, nil diff --git a/internal/pkg/core/notifier/providers/pushplus/pushplus.go b/internal/pkg/core/notifier/providers/pushplus/pushplus.go index 4edac14e..a0ef4c7f 100644 --- a/internal/pkg/core/notifier/providers/pushplus/pushplus.go +++ b/internal/pkg/core/notifier/providers/pushplus/pushplus.go @@ -9,20 +9,17 @@ import ( "log/slog" "net/http" - "github.com/pkg/errors" - "github.com/usual2970/certimate/internal/pkg/core/notifier" ) type NotifierConfig struct { - // PushPlus Token + // PushPlus Token。 Token string `json:"token"` } type NotifierProvider struct { - config *NotifierConfig - logger *slog.Logger - // 未来将移除 + config *NotifierConfig + logger *slog.Logger httpClient *http.Client } @@ -35,6 +32,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), httpClient: http.DefaultClient, }, nil } @@ -48,10 +46,8 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier { return n } -// Notify 发送通知 -// 参考文档:https://pushplus.plus/doc/guide/api.html func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) { - // 请求体 + // REF: https://pushplus.plus/doc/guide/api.html reqBody := &struct { Token string `json:"token"` Title string `json:"title"` @@ -62,10 +58,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s Content: message, } - // Make request body, err := json.Marshal(reqBody) if err != nil { - return nil, errors.Wrap(err, "encode message body") + return nil, fmt.Errorf("pushplus api error: failed to encode message body: %w", err) } req, err := http.NewRequestWithContext( @@ -75,38 +70,32 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s bytes.NewReader(body), ) if err != nil { - return nil, errors.Wrap(err, "create new request") + return nil, fmt.Errorf("pushplus api error: failed to create new request: %w", err) } req.Header.Set("Content-Type", "application/json; charset=utf-8") - // Send request to pushplus service resp, err := n.httpClient.Do(req) if err != nil { - return nil, errors.Wrapf(err, "send request to pushplus server") + return nil, fmt.Errorf("pushplus api error: failed to send request: %w", err) } defer resp.Body.Close() result, err := io.ReadAll(resp.Body) if err != nil { - return nil, errors.Wrap(err, "read response") + return nil, fmt.Errorf("pushplus api error: failed to read response: %w", err) + } else if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("pushplus api error: unexpected status code: %d, resp: %s", resp.StatusCode, string(result)) } - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("pushplus returned status code %d: %s", resp.StatusCode, string(result)) - } - - // 解析响应 var errorResponse struct { Code int `json:"code"` Msg string `json:"msg"` } if err := json.Unmarshal(result, &errorResponse); err != nil { - return nil, errors.Wrap(err, "decode response") - } - - if errorResponse.Code != 200 { - return nil, fmt.Errorf("pushplus returned error: %s", errorResponse.Msg) + return nil, fmt.Errorf("pushplus api error: failed to decode response: %w", err) + } else if errorResponse.Code != 200 { + return nil, fmt.Errorf("pushplus api error: unexpected response code: %d, msg: %s", errorResponse.Code, errorResponse.Msg) } return ¬ifier.NotifyResult{}, nil diff --git a/internal/pkg/core/notifier/providers/serverchan/serverchan.go b/internal/pkg/core/notifier/providers/serverchan/serverchan.go index 9cf45431..89724b08 100644 --- a/internal/pkg/core/notifier/providers/serverchan/serverchan.go +++ b/internal/pkg/core/notifier/providers/serverchan/serverchan.go @@ -29,6 +29,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil } diff --git a/internal/pkg/core/notifier/providers/telegram/telegram.go b/internal/pkg/core/notifier/providers/telegram/telegram.go index 7bcfe8e6..218f7ee3 100644 --- a/internal/pkg/core/notifier/providers/telegram/telegram.go +++ b/internal/pkg/core/notifier/providers/telegram/telegram.go @@ -30,6 +30,7 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) { return &NotifierProvider{ config: config, + logger: slog.Default(), }, nil }