From bfae6bb73ff1aff6b62790051d64f665e48c4971 Mon Sep 17 00:00:00 2001 From: nouryxd <66651385+nouryxd@users.noreply.github.com> Date: Fri, 19 Jul 2024 23:08:28 +0200 Subject: [PATCH] unify buildinfo in a single file. fix /status page not working --- cmd/nourybot/router.go | 16 ++++++---- pkg/commands/ping.go | 5 ++-- pkg/common/buildinfo.go | 38 ++++++++++++++++++++++++ pkg/common/version.go | 65 ----------------------------------------- 4 files changed, 51 insertions(+), 73 deletions(-) create mode 100644 pkg/common/buildinfo.go delete mode 100644 pkg/common/version.go diff --git a/cmd/nourybot/router.go b/cmd/nourybot/router.go index aca1ab8..215d2e3 100644 --- a/cmd/nourybot/router.go +++ b/cmd/nourybot/router.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "sort" + "strconv" "github.com/julienschmidt/httprouter" "github.com/nicklaw5/helix/v2" @@ -355,15 +356,18 @@ func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Requ } func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { - commit := common.GetVersion() + commit := common.GetCommit() + commandsUsed := strconv.Itoa(common.GetCommandsUsed()) started := common.GetUptime().Format("2006-1-2 15:4:5") - commitLink := fmt.Sprintf("https://github.com/nouryxd/nourybot/commit/%v", common.GetVersionPure()) + commitLink := fmt.Sprintf("https://github.com/nouryxd/nourybot/commit/%v", commit) + goVersion := common.GetGoVersion() - fmt.Print(w, fmt.Sprint( + fmt.Fprint(w, fmt.Sprint( "started: \t"+started+"\n"+ - "environment: \t"+app.Environment+"\n"+ - "commit: \t"+commit+"\n"+ - "github: \t"+commitLink, + "commands used: \t"+commandsUsed+"\n"+ + "go version: \t"+goVersion+"\n"+ + "commit: \t"+commitLink+"\n"+ + "environment: \t"+app.Environment+"\n", )) } diff --git a/pkg/commands/ping.go b/pkg/commands/ping.go index 14103e3..bbb3c6e 100644 --- a/pkg/commands/ping.go +++ b/pkg/commands/ping.go @@ -11,7 +11,8 @@ import ( func Ping(env string) string { botUptime := humanize.Time(common.GetUptime()) commandsUsed := common.GetCommandsUsed() - commit := common.GetVersion() + commit := common.GetCommit() + goVersion := common.GetGoVersion() - return fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v, Running on commit: %v, Env: %v", commandsUsed, botUptime, commit, env) + return fmt.Sprintf("Pong! :) Last restart: %v, Commands used: %v, Go version: %v, Commit: %v, Env: %v", botUptime, commandsUsed, goVersion, commit, env) } diff --git a/pkg/common/buildinfo.go b/pkg/common/buildinfo.go new file mode 100644 index 0000000..5ecfccb --- /dev/null +++ b/pkg/common/buildinfo.go @@ -0,0 +1,38 @@ +package common + +import "runtime/debug" + +// GetVersion returns the current git commit hash. +func GetGoVersion() string { + var version string + + bi, ok := debug.ReadBuildInfo() + if ok { + version = bi.GoVersion + } + + return version +} + +// GetVersion returns the current git commit hash. +func GetCommit() string { + var revision string + var short string + + bi, ok := debug.ReadBuildInfo() + if ok { + for _, s := range bi.Settings { + switch s.Key { + case "vcs.revision": + revision = s.Value + short = revision[:7] + } + } + } + + if revision == "" { + return "unavailable" + } + + return short +} diff --git a/pkg/common/version.go b/pkg/common/version.go deleted file mode 100644 index 049ab2d..0000000 --- a/pkg/common/version.go +++ /dev/null @@ -1,65 +0,0 @@ -// File pretty much completely stolen from a generated -// Autostrada project I used a while ago. They don't -// require attribution but I still want to give them -// credit for their amazing project. -// https://autostrada.dev/ - -package common - -import ( - "runtime/debug" -) - -// GetVersion returns the current git commit hash. -func GetVersion() string { - var revision string - var short string - - bi, ok := debug.ReadBuildInfo() - if ok { - for _, s := range bi.Settings { - switch s.Key { - case "vcs.revision": - revision = s.Value - short = revision[:7] - } - } - } - - if revision == "" { - return "unavailable" - } - - return short -} - -// GetVersion returns the current git commit hash. -// This function does not add the "-dirty" string at the end of the hash. -func GetVersionPure() string { - var revision string - var modified bool - - bi, ok := debug.ReadBuildInfo() - if ok { - for _, s := range bi.Settings { - switch s.Key { - case "vcs.revision": - revision = s.Value - case "vcs.modified": - if s.Value == "true" { - modified = true - } - } - } - } - - if revision == "" { - return "unavailable" - } - - if modified { - return revision - } - - return revision -}