From 65627945a98a5e46b87f081c19be8e4eedde5ce4 Mon Sep 17 00:00:00 2001 From: robrotheram Date: Wed, 28 Feb 2024 20:17:33 +0000 Subject: [PATCH] Added version info to build --- .gitignore | 2 +- Dockerfile | 1 + main.go | 2 ++ pkg/controllers/controller.go | 1 + pkg/discord/commands/infoCmd.go | 22 ++++++++++++++------ pkg/utils/version.go | 31 +++++++++++++++++++++++++++++ pkg/utils/version.txt | 0 ui/src/pages/app/components/nav.jsx | 8 ++++++-- ui/vite.config.js | 9 +++++++++ 9 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 pkg/utils/version.go create mode 100644 pkg/utils/version.txt diff --git a/.gitignore b/.gitignore index d5535ff..afdd7b9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ __debug_bin* *.db notes .kube - +w2g \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 798f4d8..6b80567 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ FROM golang:1.21.4 as GO_BUILDER ARG VER WORKDIR /server ADD . . +RUN go generate pkg/utils/version.go RUN CGO_ENABLED=0 GOOS=linux go build FROM alpine diff --git a/main.go b/main.go index 36017a7..a8e1a03 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,8 @@ func main() { log.Fatalf("Database Error: %v", err) } + log.Infof("Version: %s", utils.Version) + hub := controllers.NewHub(db) bot, _ := discord.NewDiscordBot(utils.Configuration, hub) diff --git a/pkg/controllers/controller.go b/pkg/controllers/controller.go index 02846f4..00e1f8d 100644 --- a/pkg/controllers/controller.go +++ b/pkg/controllers/controller.go @@ -37,6 +37,7 @@ func NewController(id string, db *bolt.DB) *Controller { contoller.load(id) contoller.AddListner(uuid.NewString(), &Auditing{}) if utils.Configuration.BetterStackToken != "" { + log.Info("enabling better stack logging") contoller.AddListner(uuid.NewString(), &BetterStack{Token: utils.Configuration.BetterStackToken}) } diff --git a/pkg/discord/commands/infoCmd.go b/pkg/discord/commands/infoCmd.go index 79f0951..f1bd3b1 100644 --- a/pkg/discord/commands/infoCmd.go +++ b/pkg/discord/commands/infoCmd.go @@ -1,7 +1,9 @@ package commands import ( + "time" "w2g/pkg/discord/components" + "w2g/pkg/utils" "github.com/bwmarrin/discordgo" ) @@ -12,7 +14,6 @@ func init() { Name: "player", ApplicationCommand: []discordgo.ApplicationCommand{ { - Type: discordgo.UserApplicationCommand, }, { @@ -31,14 +32,19 @@ func init() { }, }, Function: listcmd, + }, + Command{ + Name: "version", + ApplicationCommand: []discordgo.ApplicationCommand{ + { + Description: "return the version of this bot", + Type: discordgo.ChatApplicationCommand, + }, + }, + Function: versioncmd, }) } -func nowplayingCmd(ctx CommandCtx) *discordgo.InteractionResponse { - current := ctx.Controller.State().Current - return ctx.CmdReplyEmbed(components.MediaEmbed(current, "Now Playing")) -} - func listcmd(ctx CommandCtx) *discordgo.InteractionResponse { return ctx.CmdReplyData(components.QueueCompontent(ctx.Controller.State().Queue, 0)) } @@ -46,3 +52,7 @@ func listcmd(ctx CommandCtx) *discordgo.InteractionResponse { func controlscmd(ctx CommandCtx) *discordgo.InteractionResponse { return ctx.CmdReplyData(components.ControlCompontent(ctx.Controller.State())) } + +func versioncmd(ctx CommandCtx) *discordgo.InteractionResponse { + return ctx.Replyf("Version: %s, \n commit: %s \n time: %s", utils.Version, utils.Revision, utils.LastCommit.Format(time.RFC822)) +} diff --git a/pkg/utils/version.go b/pkg/utils/version.go new file mode 100644 index 0000000..37e26ec --- /dev/null +++ b/pkg/utils/version.go @@ -0,0 +1,31 @@ +package utils + +import ( + _ "embed" + "runtime/debug" + "time" +) + +//go:generate sh -c "git describe --tags --abbrev=0 > version.txt" +//go:embed version.txt +var Version string +var LastCommit time.Time +var Revision string + +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + for _, kv := range info.Settings { + if kv.Value == "" { + continue + } + switch kv.Key { + case "vcs.revision": + Revision = kv.Value + case "vcs.time": + LastCommit, _ = time.Parse(time.RFC3339, kv.Value) + } + } +} diff --git a/pkg/utils/version.txt b/pkg/utils/version.txt new file mode 100644 index 0000000..e69de29 diff --git a/ui/src/pages/app/components/nav.jsx b/ui/src/pages/app/components/nav.jsx index df63a39..7fb77b1 100644 --- a/ui/src/pages/app/components/nav.jsx +++ b/ui/src/pages/app/components/nav.jsx @@ -41,8 +41,8 @@ export const Nav = ({user, bot}) => { - {isModalOpen &&
-
diff --git a/ui/vite.config.js b/ui/vite.config.js index fc9da72..306f5db 100644 --- a/ui/vite.config.js +++ b/ui/vite.config.js @@ -1,5 +1,6 @@ import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' +import * as child from "child_process"; import { VitePWA } from "vite-plugin-pwa"; const manifestForPlugin = { @@ -47,7 +48,15 @@ const manifestForPlugin = { }, }; + + +const commitHash = child.execSync("git rev-parse --short HEAD").toString() +const gitTag = child.execSync("git describe --tags --abbrev=0").toString() // https://vitejs.dev/config/ export default defineConfig({ + define: { + 'import.meta.env.VITE_APP_VERSION': JSON.stringify(gitTag), + 'import.meta.env.VITE_APP_COMMIT': JSON.stringify(commitHash) + }, plugins: [react()], })