refactor: simplify plugin code and fix minor issues

- Replace os.Open/bufio/io.ReadAll with os.ReadFile in loadTextFromFile
- Guard proxy URL parsing inside socks5 check to avoid false errors
- Remove redundant trimElement wrapping around globList calls
- Remove dead title assignment in convertLocation
- Switch Plugin methods to pointer receivers to avoid struct copying
- Use %w for error wrapping in fmt.Errorf calls
- Fix "unmarshall" typos to "unmarshal" in error messages
- Fix duplicate DIST variable and GXZ_PAGAGE typo in Makefile
This commit is contained in:
Bo-Yi Wu
2026-04-08 22:40:06 +08:00
parent 893f3c30bf
commit 8587a97ab1
2 changed files with 22 additions and 33 deletions
+3 -4
View File
@@ -1,4 +1,3 @@
DIST := dist
EXECUTABLE := drone-telegram
GOFMT ?= gofumpt -l
DIST := dist
@@ -9,7 +8,7 @@ GOFILES := $(shell find . -name "*.go" -type f)
HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
XGO_VERSION := go-1.19.x
GXZ_PAGAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.11
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.11
LINUX_ARCHS ?= linux/amd64,linux/arm64
DARWIN_ARCHS ?= darwin-10.12/amd64,darwin-10.12/arm64
@@ -116,7 +115,7 @@ coverage:
.PHONY: deps-backend
deps-backend:
$(GO) mod download
$(GO) install $(GXZ_PAGAGE)
$(GO) install $(GXZ_PACKAGE)
$(GO) install $(XGO_PACKAGE)
.PHONY: release
@@ -156,7 +155,7 @@ release-check: | $(DIST_DIRS)
.PHONY: release-compress
release-compress: | $(DIST_DIRS)
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && $(GO) run $(GXZ_PAGAGE) -k -9 $${file}; done;
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && $(GO) run $(GXZ_PACKAGE) -k -9 $${file}; done;
clean:
$(GO) clean -x -i ./...
+19 -29
View File
@@ -1,12 +1,10 @@
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"html"
"io"
"log"
"net/http"
"net/url"
@@ -183,7 +181,6 @@ func convertLocation(value string) (Location, bool) {
}
if len(values) > 3 {
title = values[2]
address = values[3]
}
@@ -208,13 +205,7 @@ func convertLocation(value string) (Location, bool) {
}
func loadTextFromFile(filename string) ([]string, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
r := bufio.NewReader(f)
content, err := io.ReadAll(r)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
@@ -258,12 +249,12 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
return ids
}
func templateMessage(t string, plugin Plugin) (string, error) {
func templateMessage(t string, plugin *Plugin) (string, error) {
return template.RenderTrim(t, plugin)
}
// Exec executes the plugin.
func (p Plugin) Exec() (err error) {
func (p *Plugin) Exec() (err error) {
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
return errors.New("missing telegram token or user list")
}
@@ -273,7 +264,7 @@ func (p Plugin) Exec() (err error) {
case len(p.Config.MessageFile) > 0:
message, err = loadTextFromFile(p.Config.MessageFile)
if err != nil {
return fmt.Errorf("error loading message file '%s': %v", p.Config.MessageFile, err)
return fmt.Errorf("error loading message file '%s': %w", p.Config.MessageFile, err)
}
case len(p.Config.Message) > 0:
message = []string{p.Config.Message}
@@ -285,18 +276,18 @@ func (p Plugin) Exec() (err error) {
if p.Config.TemplateVars != "" {
p.Tpl = make(map[string]string)
if err = json.Unmarshal([]byte(p.Config.TemplateVars), &p.Tpl); err != nil {
return fmt.Errorf("unable to unmarshall template vars from JSON string '%s': %v", p.Config.TemplateVars, err)
return fmt.Errorf("unable to unmarshal template vars from JSON string '%s': %w", p.Config.TemplateVars, err)
}
}
if p.Config.TemplateVarsFile != "" {
content, err := os.ReadFile(p.Config.TemplateVarsFile)
if err != nil {
return fmt.Errorf("unable to read file with template vars '%s': %v", p.Config.TemplateVarsFile, err)
return fmt.Errorf("unable to read file with template vars '%s': %w", p.Config.TemplateVarsFile, err)
}
vars := make(map[string]string)
if err = json.Unmarshal(content, &vars); err != nil {
return fmt.Errorf("unable to unmarshall template vars from JSON file '%s': %v", p.Config.TemplateVarsFile, err)
return fmt.Errorf("unable to unmarshal template vars from JSON file '%s': %w", p.Config.TemplateVarsFile, err)
}
// Merging templates variables from file to the variables form plugin settings (variables from file takes precedence)
if p.Tpl == nil {
@@ -308,13 +299,12 @@ func (p Plugin) Exec() (err error) {
}
}
var proxyURL *url.URL
if proxyURL, err = url.Parse(p.Config.Socks5); err != nil {
return fmt.Errorf("unable to unmarshall socks5 proxy url from string '%s': %v", p.Config.Socks5, err)
}
var bot *tgbotapi.BotAPI
if len(p.Config.Socks5) > 0 {
proxyURL, err := url.Parse(p.Config.Socks5)
if err != nil {
return fmt.Errorf("unable to parse socks5 proxy URL '%s': %w", p.Config.Socks5, err)
}
proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, proxyClient)
} else {
@@ -328,12 +318,12 @@ func (p Plugin) Exec() (err error) {
bot.Debug = p.Config.Debug
ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail)
photos := globList(trimElement(p.Config.Photo))
documents := globList(trimElement(p.Config.Document))
stickers := globList(trimElement(p.Config.Sticker))
audios := globList(trimElement(p.Config.Audio))
voices := globList(trimElement(p.Config.Voice))
videos := globList(trimElement(p.Config.Video))
photos := globList(p.Config.Photo)
documents := globList(p.Config.Document)
stickers := globList(p.Config.Sticker)
audios := globList(p.Config.Audio)
voices := globList(p.Config.Voice)
videos := globList(p.Config.Video)
locations := trimElement(p.Config.Location)
venues := trimElement(p.Config.Venue)
@@ -450,7 +440,7 @@ func (p Plugin) Exec() (err error) {
}
// Send bot message.
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
func (p *Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
message, err := bot.Send(msg)
if p.Config.Debug {
@@ -467,7 +457,7 @@ func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
}
// Message is plugin default message.
func (p Plugin) Message() []string {
func (p *Plugin) Message() []string {
icon := icons[strings.ToLower(p.Build.Status)]
if p.Config.GitHub {