unify buildinfo in a single file. fix /status page not working

This commit is contained in:
nouryxd 2024-07-19 23:08:28 +02:00
parent a44cab6b3f
commit bfae6bb73f
4 changed files with 51 additions and 73 deletions

View file

@ -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",
))
}

View file

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

38
pkg/common/buildinfo.go Normal file
View file

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

View file

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