Set build hash and pipeline link through LDFlags

Add custom command values to config
This commit is contained in:
Michał Gdula 2025-04-24 19:32:32 +01:00
parent eb50802d1e
commit a0bb37fc06
5 changed files with 36 additions and 21 deletions

View file

@ -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:

View file

@ -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]

View file

@ -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,
},
{

View file

@ -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))

15
main.go
View file

@ -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)