From 066194121b6c20b0bfdb71f7ceb6abb12a5d107d Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Mon, 26 Sep 2022 13:16:04 +0200 Subject: [PATCH] Add version and git tag to binary Fixes #47 --- Makefile | 6 +++++- cmd/cc-backend/main.go | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 48fc132..aac0e1e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,10 @@ TARGET = ./cc-backend VAR = ./var FRONTEND = ./web/frontend +VERSION = 0.1 +GIT_HASH := $(shell git rev-parse --short HEAD || echo 'development') +CURRENT_TIME = $(shell date +"%Y-%m-%d:T%H:%M:%S") +LD_FLAGS = '-s -X main.buildTime=${CURRENT_TIME} -X main.version=${VERSION} -X main.hash=${GIT_HASH}' SVELTE_COMPONENTS = status \ analysis \ @@ -25,7 +29,7 @@ SVELTE_SRC = $(wildcard $(FRONTEND)/src/*.svelte) \ $(TARGET): $(VAR) $(SVELTE_TARGETS) $(info ===> BUILD cc-backend) - @go build ./cmd/cc-backend + @go build -ldflags=${LD_FLAGS} ./cmd/cc-backend clean: $(info ===> CLEAN) diff --git a/cmd/cc-backend/main.go b/cmd/cc-backend/main.go index b343066..c7c4b8e 100644 --- a/cmd/cc-backend/main.go +++ b/cmd/cc-backend/main.go @@ -46,14 +46,30 @@ import ( _ "github.com/mattn/go-sqlite3" ) +const logoString = ` + ____ _ _ ____ _ _ _ +/ ___| |_ _ ___| |_ ___ _ __ / ___|___ ___| | ___ __ (_) |_ +| | | | | | / __| __/ _ \ '__| | / _ \ / __| |/ / '_ \| | __| +| |___| | |_| \__ \ || __/ | | |__| (_) | (__| <| |_) | | |_ +\____|_|\__,_|___/\__\___|_| \____\___/ \___|_|\_\ .__/|_|\__| + |_| +` + +var ( + buildTime string + hash string + version string +) + func main() { - var flagReinitDB, flagServer, flagSyncLDAP, flagGops, flagDev bool + var flagReinitDB, flagServer, flagSyncLDAP, flagGops, flagDev, flagVersion bool var flagNewUser, flagDelUser, flagGenJWT, flagConfigFile, flagImportJob string flag.BoolVar(&flagReinitDB, "init-db", false, "Go through job-archive and re-initialize the 'job', 'tag', and 'jobtag' tables (all running jobs will be lost!)") flag.BoolVar(&flagSyncLDAP, "sync-ldap", false, "Sync the 'user' table with ldap") flag.BoolVar(&flagServer, "server", false, "Start a server, continues listening on port after initialization and argument handling") flag.BoolVar(&flagGops, "gops", false, "Listen via github.com/google/gops/agent (for debugging)") flag.BoolVar(&flagDev, "dev", false, "Enable development components: GraphQL Playground and Swagger UI") + flag.BoolVar(&flagVersion, "version", false, "Show version information and exit") flag.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`") flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `:[admin,support,api,user]:`") flag.StringVar(&flagDelUser, "del-user", "", "Remove user by `username`") @@ -61,6 +77,14 @@ func main() { flag.StringVar(&flagImportJob, "import-job", "", "Import a job. Argument format: `:,...`") flag.Parse() + if flagVersion { + fmt.Print(logoString) + fmt.Printf("Version:\t%s\n", version) + fmt.Printf("Git hash:\t%s\n", hash) + fmt.Printf("Build time:\t%s\n", buildTime) + os.Exit(0) + } + // See https://github.com/google/gops (Runtime overhead is almost zero) if flagGops { if err := agent.Listen(agent.Options{}); err != nil {