From 9f1d073728867eaab03ce799175788d89e129101 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sat, 22 Jun 2019 15:34:00 +0530 Subject: [PATCH] Add Makefile and fix cmdline version flag --- Makefile | 21 +++++++++++++++++++++ main.go | 27 +++++++++++---------------- 2 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5a01522 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +LAST_COMMIT := $(shell git rev-parse --short HEAD) +LAST_COMMIT_DATE := $(shell git show -s --format=%ci ${LAST_COMMIT}) +VERSION := $(shell git describe --abbrev=1) + +BUILDSTR := ${VERSION} (build "\\\#"${LAST_COMMIT} $(shell date '+%Y-%m-%d %H:%M:%S')) + +BIN := sql-jobber + +.PHONY: build +build: + # Compile the main application. + go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}'" + +.PHONY: test +test: + go test + +.PHONY: clean +clean: + go clean + - rm -f ${BIN} diff --git a/main.go b/main.go index c744026..bcf0cae 100644 --- a/main.go +++ b/main.go @@ -30,9 +30,6 @@ import ( const ( dbMySQL = "mysql" dbPostgres = "postgres" - - buildVersion = "unknown" - buildDate = "unknown" ) type constants struct { @@ -62,12 +59,10 @@ type DBConfig struct { } var ( - sysLog = log.New(os.Stdout, "JOBBER: ", log.Ldate|log.Ltime|log.Lshortfile) - - ko = koanf.New(".") - - // Global Jobber container. - jobber = &Jobber{ + buildString = "unknown" + sysLog = log.New(os.Stdout, "JOBBER: ", log.Ldate|log.Ltime|log.Lshortfile) + ko = koanf.New(".") + jobber = &Jobber{ Tasks: make(Tasks), DBs: make(DBs), ResultBackends: make(ResultBackends), @@ -91,12 +86,18 @@ func init() { f.String("worker-name", "sqljobber", "Name of this worker instance") f.Int("worker-concurrency", 10, "Number of concurrent worker threads to run") f.Bool("worker-only", false, "Don't start the HTTP server and run in worker-only mode?") - f.Bool("version", false, "Current version of the build") + f.Bool("version", false, "Current version and build") f.Parse(os.Args[1:]) // Load commandline params. ko.Load(posflag.Provider(f, ".", ko), nil) + // Display version. + if ko.Bool("version") { + fmt.Println(buildString) + os.Exit(0) + } + // Load the config file. sysLog.Printf("reading config: %s", ko.String("config")) if err := ko.Load(file.Provider(ko.String("config")), toml.Parser()); err != nil { @@ -108,12 +109,6 @@ func init() { } func main() { - // Display version. - if ko.Bool("version") { - sysLog.Printf("commit: %v\nBuild: %v", buildVersion, buildDate) - return - } - mode := "default" if ko.Bool("worker-only") { mode = "worker only"