mirror of
https://github.com/appleboy/drone-telegram.git
synced 2026-06-04 18:23:45 +08:00
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:
@@ -1,4 +1,3 @@
|
|||||||
DIST := dist
|
|
||||||
EXECUTABLE := drone-telegram
|
EXECUTABLE := drone-telegram
|
||||||
GOFMT ?= gofumpt -l
|
GOFMT ?= gofumpt -l
|
||||||
DIST := dist
|
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" )
|
HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
|
||||||
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
|
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
|
||||||
XGO_VERSION := go-1.19.x
|
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
|
LINUX_ARCHS ?= linux/amd64,linux/arm64
|
||||||
DARWIN_ARCHS ?= darwin-10.12/amd64,darwin-10.12/arm64
|
DARWIN_ARCHS ?= darwin-10.12/amd64,darwin-10.12/arm64
|
||||||
@@ -116,7 +115,7 @@ coverage:
|
|||||||
.PHONY: deps-backend
|
.PHONY: deps-backend
|
||||||
deps-backend:
|
deps-backend:
|
||||||
$(GO) mod download
|
$(GO) mod download
|
||||||
$(GO) install $(GXZ_PAGAGE)
|
$(GO) install $(GXZ_PACKAGE)
|
||||||
$(GO) install $(XGO_PACKAGE)
|
$(GO) install $(XGO_PACKAGE)
|
||||||
|
|
||||||
.PHONY: release
|
.PHONY: release
|
||||||
@@ -156,7 +155,7 @@ release-check: | $(DIST_DIRS)
|
|||||||
|
|
||||||
.PHONY: release-compress
|
.PHONY: release-compress
|
||||||
release-compress: | $(DIST_DIRS)
|
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:
|
clean:
|
||||||
$(GO) clean -x -i ./...
|
$(GO) clean -x -i ./...
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -183,7 +181,6 @@ func convertLocation(value string) (Location, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(values) > 3 {
|
if len(values) > 3 {
|
||||||
title = values[2]
|
|
||||||
address = values[3]
|
address = values[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,13 +205,7 @@ func convertLocation(value string) (Location, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadTextFromFile(filename string) ([]string, error) {
|
func loadTextFromFile(filename string) ([]string, error) {
|
||||||
f, err := os.Open(filename)
|
content, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
r := bufio.NewReader(f)
|
|
||||||
content, err := io.ReadAll(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -258,12 +249,12 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
|||||||
return ids
|
return ids
|
||||||
}
|
}
|
||||||
|
|
||||||
func templateMessage(t string, plugin Plugin) (string, error) {
|
func templateMessage(t string, plugin *Plugin) (string, error) {
|
||||||
return template.RenderTrim(t, plugin)
|
return template.RenderTrim(t, plugin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec executes the 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 {
|
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
|
||||||
return errors.New("missing telegram token or user list")
|
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:
|
case len(p.Config.MessageFile) > 0:
|
||||||
message, err = loadTextFromFile(p.Config.MessageFile)
|
message, err = loadTextFromFile(p.Config.MessageFile)
|
||||||
if err != nil {
|
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:
|
case len(p.Config.Message) > 0:
|
||||||
message = []string{p.Config.Message}
|
message = []string{p.Config.Message}
|
||||||
@@ -285,18 +276,18 @@ func (p Plugin) Exec() (err error) {
|
|||||||
if p.Config.TemplateVars != "" {
|
if p.Config.TemplateVars != "" {
|
||||||
p.Tpl = make(map[string]string)
|
p.Tpl = make(map[string]string)
|
||||||
if err = json.Unmarshal([]byte(p.Config.TemplateVars), &p.Tpl); err != nil {
|
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 != "" {
|
if p.Config.TemplateVarsFile != "" {
|
||||||
content, err := os.ReadFile(p.Config.TemplateVarsFile)
|
content, err := os.ReadFile(p.Config.TemplateVarsFile)
|
||||||
if err != nil {
|
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)
|
vars := make(map[string]string)
|
||||||
if err = json.Unmarshal(content, &vars); err != nil {
|
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)
|
// Merging templates variables from file to the variables form plugin settings (variables from file takes precedence)
|
||||||
if p.Tpl == nil {
|
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
|
var bot *tgbotapi.BotAPI
|
||||||
if len(p.Config.Socks5) > 0 {
|
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)}}
|
proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
|
||||||
bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, proxyClient)
|
bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, proxyClient)
|
||||||
} else {
|
} else {
|
||||||
@@ -328,12 +318,12 @@ func (p Plugin) Exec() (err error) {
|
|||||||
bot.Debug = p.Config.Debug
|
bot.Debug = p.Config.Debug
|
||||||
|
|
||||||
ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail)
|
ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail)
|
||||||
photos := globList(trimElement(p.Config.Photo))
|
photos := globList(p.Config.Photo)
|
||||||
documents := globList(trimElement(p.Config.Document))
|
documents := globList(p.Config.Document)
|
||||||
stickers := globList(trimElement(p.Config.Sticker))
|
stickers := globList(p.Config.Sticker)
|
||||||
audios := globList(trimElement(p.Config.Audio))
|
audios := globList(p.Config.Audio)
|
||||||
voices := globList(trimElement(p.Config.Voice))
|
voices := globList(p.Config.Voice)
|
||||||
videos := globList(trimElement(p.Config.Video))
|
videos := globList(p.Config.Video)
|
||||||
locations := trimElement(p.Config.Location)
|
locations := trimElement(p.Config.Location)
|
||||||
venues := trimElement(p.Config.Venue)
|
venues := trimElement(p.Config.Venue)
|
||||||
|
|
||||||
@@ -450,7 +440,7 @@ func (p Plugin) Exec() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send bot message.
|
// 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)
|
message, err := bot.Send(msg)
|
||||||
|
|
||||||
if p.Config.Debug {
|
if p.Config.Debug {
|
||||||
@@ -467,7 +457,7 @@ func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Message is plugin default message.
|
// Message is plugin default message.
|
||||||
func (p Plugin) Message() []string {
|
func (p *Plugin) Message() []string {
|
||||||
icon := icons[strings.ToLower(p.Build.Status)]
|
icon := icons[strings.ToLower(p.Build.Status)]
|
||||||
|
|
||||||
if p.Config.GitHub {
|
if p.Config.GitHub {
|
||||||
|
|||||||
Reference in New Issue
Block a user