From 5ef2803075787e165176ab3fda3176e091817fb3 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 14 Oct 2020 16:14:57 -0500 Subject: [PATCH] build git commit hash into project --- Makefile | 9 +++++++++ main.go | 4 +++- web/templates/master.html | 11 +++++++++++ web/web.go | 17 ++++++++++++----- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1f3bcab --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +GIT_COMMIT = $(shell git rev-list -1 HEAD) +SOURCES = $(shell find . -type f -and -name "*.go" -or -name "*.html") + +all: subscribe-bot + +subscribe-bot: $(SOURCES) + go build -o $@ -ldflags "-X main.GitCommit=$(GIT_COMMIT)" + +.PHONY: all diff --git a/main.go b/main.go index 095a392..e9da624 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,8 @@ import ( var exit_chan = make(chan int) +var GitCommit string + func main() { configPath := flag.String("config", "config.toml", "Path to the config file (defaults to config.toml)") flag.Parse() @@ -40,7 +42,7 @@ func main() { } go scrape.RunScraper(&config, bot, db, api) - go web.RunWeb(&config, api) + go web.RunWeb(&config, api, GitCommit) signal_chan := make(chan os.Signal, 1) signal.Notify(signal_chan, diff --git a/web/templates/master.html b/web/templates/master.html index 91a16de..f9e2ddb 100644 --- a/web/templates/master.html +++ b/web/templates/master.html @@ -27,6 +27,17 @@ {{ template "content" . }} + + diff --git a/web/web.go b/web/web.go index a33dec8..41cd928 100644 --- a/web/web.go +++ b/web/web.go @@ -2,6 +2,7 @@ package web import ( "fmt" + "html/template" "io/ioutil" "net/http" "os" @@ -30,17 +31,18 @@ var ( ) type Web struct { - config *config.Config - api *osuapi.Osuapi - hc *http.Client + config *config.Config + api *osuapi.Osuapi + hc *http.Client + version string } -func RunWeb(config *config.Config, api *osuapi.Osuapi) { +func RunWeb(config *config.Config, api *osuapi.Osuapi, version string) { hc := &http.Client{ Timeout: 10 * time.Second, } - web := Web{config, api, hc} + web := Web{config, api, hc, version} web.Run() } @@ -58,6 +60,11 @@ func (web *Web) Run() { Root: "web/templates", Master: "master.html", DisableCache: web.config.Debug, + Funcs: template.FuncMap{ + "GitCommit": func() string { + return web.version + }, + }, }) r.GET("/logout", web.logout)