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 image: golang:1.24-alpine3.20
commands: commands:
- go mod tidy - go mod tidy
- go build . - go build . -ldflags="-X 'main.ConfigBuildHash=$CI_COMMIT_SHA' -X 'main.ConfigBuildPipeline=$CI_PIPELINE_URL'"
- name: deploy - name: deploy
image: alpine:latest image: alpine:latest
commands: commands:

View file

@ -15,9 +15,10 @@ import (
type Callback func(h *Handler, args []string) Error type Callback func(h *Handler, args []string) Error
type Config struct { type Config struct {
Prefix string BotPrefix string
Token string BotToken string
Intents discordgo.Intent BotIntents discordgo.Intent
CommandExtras map[string]string
} }
type App struct { type App struct {
@ -43,7 +44,7 @@ func (a *App) RegisterCommandAlias(alias, cmd string) {
} }
func (a *App) Run() { func (a *App) Run() {
dg, err := discordgo.New("Bot " + a.Config.Token) dg, err := discordgo.New("Bot " + a.Config.BotToken)
if err != nil { if err != nil {
fmt.Println("error creating Discord session,", err) fmt.Println("error creating Discord session,", err)
@ -51,7 +52,7 @@ func (a *App) Run() {
} }
dg.AddHandler(a.handler) dg.AddHandler(a.handler)
dg.Identify.Intents = a.Config.Intents dg.Identify.Intents = a.Config.BotIntents
err = dg.Open() err = dg.Open()
if err != nil { if err != nil {
@ -95,7 +96,7 @@ func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCrea
var args string var args string
cmd = h.Message.Content cmd = h.Message.Content
cmd = strings.TrimPrefix(cmd, a.Config.Prefix) cmd = strings.TrimPrefix(cmd, a.Config.BotPrefix)
cmd, args, _ = strings.Cut(cmd, " ") cmd, args, _ = strings.Cut(cmd, " ")
alias, ok := a.CommandAliases[cmd] alias, ok := a.CommandAliases[cmd]

View file

@ -5,7 +5,6 @@ import (
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time"
"github.com/Fluffy-Bean/lynxie/app" "github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/utils" "github.com/Fluffy-Bean/lynxie/utils"
@ -21,7 +20,8 @@ func registerDebug(a *app.App) app.Callback {
buildTags := "-" buildTags := "-"
goVersion := strings.TrimPrefix(runtime.Version(), "go") goVersion := strings.TrimPrefix(runtime.Version(), "go")
gcCount := runtime.MemStats{}.NumGC 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() latency := h.Session.HeartbeatLatency().Milliseconds()
info, _ := debug.ReadBuildInfo() info, _ := debug.ReadBuildInfo()
@ -57,8 +57,8 @@ func registerDebug(a *app.App) app.Callback {
Inline: false, Inline: false,
}, },
{ {
Name: "Local Time", Name: "Build Hash",
Value: localTime, Value: fmt.Sprintf("[%s](%s)", buildHash, buildPipeline),
Inline: false, Inline: false,
}, },
{ {

View file

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
@ -18,8 +17,6 @@ import (
var client = http.Client{ var client = http.Client{
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
} }
var username = os.Getenv("E621_USERNAME")
var password = os.Getenv("E621_PASSWORD")
type post struct { type post struct {
Id int `json:"id"` Id int `json:"id"`
@ -57,13 +54,18 @@ type post struct {
} }
func RegisterPorbCommands(a *app.App) { func RegisterPorbCommands(a *app.App) {
if username != "" && password != "" { username, _ := a.Config.CommandExtras["e621_username"]
a.RegisterCommand("e621", registerE621(a)) password, _ := a.Config.CommandExtras["e621_password"]
a.RegisterCommandAlias("porb", "e621") if username == "" || password == "" {
} else {
log.Println("Not registering e621 command...") log.Println("Not registering e621 command...")
return
} }
a.RegisterCommand("e621", registerE621(a))
a.RegisterCommandAlias("porb", "e621")
} }
func registerE621(a *app.App) app.Callback { 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("Accept", "application/json")
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", fmt.Sprintf("Lynxie/2.0 (by %s on e621)", username)) 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" "github.com/bwmarrin/discordgo"
) )
var ConfigBuildHash string
var ConfigBuildPipeline string
func main() { func main() {
a := app.NewApp(app.Config{ a := app.NewApp(app.Config{
Prefix: ">", BotPrefix: ">",
Token: os.Getenv("TOKEN"), BotToken: os.Getenv("TOKEN"),
Intents: discordgo.IntentsGuildMessages, 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) debug.RegisterDebugCommands(a)