From a131f829f3c4fee9357dac5b102cb186388c628d Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Fri, 22 Feb 2019 01:05:44 +0530 Subject: [PATCH] [#22] Improve Makefile, Add better versions cmd --- Makefile | 29 +++++++++++++++++++---------- cmd/version.go | 3 ++- main.go | 4 ++++ pkg/global/global.go | 5 +++++ 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 pkg/global/global.go diff --git a/Makefile b/Makefile index 84223f6..041a287 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,30 @@ -#Go parameters +.PHONY: all install test build clean + +SHA=$(shell git rev-list HEAD --max-count=1 --abbrev-commit) +TAG?=$(shell git tag -l --contains HEAD) +VERSION=$(TAG) + +ifeq ($(VERSION),) +VERSION := latest +endif + +#Go parameters GOCMD=go GOINSTALL=$(GOCMD) install GOTEST=$(GOCMD) test -DEP=dep -.PHONY : all install test - -all : def +DEP=dep -def : - @$(GOINSTALL) -ldflags '-s' +all: build -install: +install: @$(DEP) ensure -test: +test: install @$(GOTEST) -v ./... +build: install + @$(GOINSTALL) -ldflags "-X main.version=$(VERSION)-$(SHA) -s" + clean: - rm -rf * \ No newline at end of file + rm -rf * diff --git a/cmd/version.go b/cmd/version.go index 65a63b2..d65c46d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "github.com/spf13/cobra" + G "github.com/leopardslab/Dunner/pkg/global" ) func init() { @@ -14,6 +15,6 @@ var versionCmd = &cobra.Command{ Short: "Print the version number of Dunner", Long: `All software has versions. This is Dunners's`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("v0.1") + fmt.Println(G.VERSION) }, } diff --git a/main.go b/main.go index 225014b..0469e03 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,12 @@ package main import ( "github.com/leopardslab/Dunner/cmd" + G "github.com/leopardslab/Dunner/pkg/global" ) +var version string = "" + func main() { + G.VERSION = version cmd.Execute() } diff --git a/pkg/global/global.go b/pkg/global/global.go new file mode 100644 index 0000000..6cfa267 --- /dev/null +++ b/pkg/global/global.go @@ -0,0 +1,5 @@ +package global + +var ( + VERSION string +)