From a0bb37fc067bfed184c33572d831ee602f53e7fc Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Thu, 24 Apr 2025 19:32:32 +0100 Subject: [PATCH] Set build hash and pipeline link through LDFlags Add custom command values to config --- .woodpecker/pipeline.yaml | 2 +- app/app.go | 13 +++++++------ commands/debug/debug.go | 8 ++++---- commands/porb/porb.go | 19 ++++++++++++------- main.go | 15 ++++++++++++--- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.woodpecker/pipeline.yaml b/.woodpecker/pipeline.yaml index 2617130..8a99d72 100644 --- a/.woodpecker/pipeline.yaml +++ b/.woodpecker/pipeline.yaml @@ -7,7 +7,7 @@ steps: image: golang:1.24-alpine3.20 commands: - go mod tidy - - go build . + - go build . -ldflags="-X 'main.ConfigBuildHash=$CI_COMMIT_SHA' -X 'main.ConfigBuildPipeline=$CI_PIPELINE_URL'" - name: deploy image: alpine:latest commands: diff --git a/app/app.go b/app/app.go index 67fab2d..40b7939 100644 --- a/app/app.go +++ b/app/app.go @@ -15,9 +15,10 @@ import ( type Callback func(h *Handler, args []string) Error type Config struct { - Prefix string - Token string - Intents discordgo.Intent + BotPrefix string + BotToken string + BotIntents discordgo.Intent + CommandExtras map[string]string } type App struct { @@ -43,7 +44,7 @@ func (a *App) RegisterCommandAlias(alias, cmd string) { } func (a *App) Run() { - dg, err := discordgo.New("Bot " + a.Config.Token) + dg, err := discordgo.New("Bot " + a.Config.BotToken) if err != nil { fmt.Println("error creating Discord session,", err) @@ -51,7 +52,7 @@ func (a *App) Run() { } dg.AddHandler(a.handler) - dg.Identify.Intents = a.Config.Intents + dg.Identify.Intents = a.Config.BotIntents err = dg.Open() if err != nil { @@ -95,7 +96,7 @@ func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCrea var args string cmd = h.Message.Content - cmd = strings.TrimPrefix(cmd, a.Config.Prefix) + cmd = strings.TrimPrefix(cmd, a.Config.BotPrefix) cmd, args, _ = strings.Cut(cmd, " ") alias, ok := a.CommandAliases[cmd] diff --git a/commands/debug/debug.go b/commands/debug/debug.go index 27f2096..d273178 100644 --- a/commands/debug/debug.go +++ b/commands/debug/debug.go @@ -5,7 +5,6 @@ import ( "runtime" "runtime/debug" "strings" - "time" "github.com/Fluffy-Bean/lynxie/app" "github.com/Fluffy-Bean/lynxie/utils" @@ -21,7 +20,8 @@ func registerDebug(a *app.App) app.Callback { buildTags := "-" goVersion := strings.TrimPrefix(runtime.Version(), "go") gcCount := runtime.MemStats{}.NumGC - localTime := time.Now().Local().Format("2006-01-02 15:04:05") + buildHash, _ := a.Config.CommandExtras["debug_build-hash"] + buildPipeline, _ := a.Config.CommandExtras["debug_build-pipeline"] latency := h.Session.HeartbeatLatency().Milliseconds() info, _ := debug.ReadBuildInfo() @@ -57,8 +57,8 @@ func registerDebug(a *app.App) app.Callback { Inline: false, }, { - Name: "Local Time", - Value: localTime, + Name: "Build Hash", + Value: fmt.Sprintf("[%s](%s)", buildHash, buildPipeline), Inline: false, }, { diff --git a/commands/porb/porb.go b/commands/porb/porb.go index 05768fa..6f97782 100644 --- a/commands/porb/porb.go +++ b/commands/porb/porb.go @@ -6,7 +6,6 @@ import ( "fmt" "log" "net/http" - "os" "strings" "time" @@ -18,8 +17,6 @@ import ( var client = http.Client{ Timeout: 10 * time.Second, } -var username = os.Getenv("E621_USERNAME") -var password = os.Getenv("E621_PASSWORD") type post struct { Id int `json:"id"` @@ -57,13 +54,18 @@ type post struct { } func RegisterPorbCommands(a *app.App) { - if username != "" && password != "" { - a.RegisterCommand("e621", registerE621(a)) + username, _ := a.Config.CommandExtras["e621_username"] + password, _ := a.Config.CommandExtras["e621_password"] - a.RegisterCommandAlias("porb", "e621") - } else { + if username == "" || password == "" { log.Println("Not registering e621 command...") + + return } + + a.RegisterCommand("e621", registerE621(a)) + + a.RegisterCommandAlias("porb", "e621") } func registerE621(a *app.App) app.Callback { @@ -99,6 +101,9 @@ func registerE621(a *app.App) app.Callback { } } + username, _ := a.Config.CommandExtras["e621_username"] + password, _ := a.Config.CommandExtras["e621_password"] + req.Header.Add("Accept", "application/json") req.Header.Add("Content-Type", "application/json") req.Header.Add("User-Agent", fmt.Sprintf("Lynxie/2.0 (by %s on e621)", username)) diff --git a/main.go b/main.go index a32c6f9..e7dd65d 100644 --- a/main.go +++ b/main.go @@ -11,11 +11,20 @@ import ( "github.com/bwmarrin/discordgo" ) +var ConfigBuildHash string +var ConfigBuildPipeline string + func main() { a := app.NewApp(app.Config{ - Prefix: ">", - Token: os.Getenv("TOKEN"), - Intents: discordgo.IntentsGuildMessages, + BotPrefix: ">", + BotToken: os.Getenv("TOKEN"), + BotIntents: discordgo.IntentsGuildMessages, + CommandExtras: map[string]string{ + "debug_build-hash": ConfigBuildHash, + "debug_build-pipeline": ConfigBuildPipeline, + "e621_username": os.Getenv("E621_USERNAME"), + "e621_password": os.Getenv("E621_PASSWORD"), + }, }) debug.RegisterDebugCommands(a)