diff --git a/Makefile b/Makefile index f8de63c..67168df 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,12 @@ ifndef VERSION $(error VERSION is not set. Please review and copy config.env.default to config.env and try again) endif +ifndef BUILD +$(error BUILD is not set. Please review and copy config.env.default to config.env and try again) +endif + +LDFLAGS="-s -X=$(GIT)main.commit=$(BUILD) -X=$(GIT)main.version=$(VERSION) -X=$(GIT)main.date=$(shell date +%Y-%m-%d:%H:%M:%S)" + SHELL := /bin/bash GOPATH ?= $(shell echo $${GOPATH:-~/go}) @@ -54,8 +60,8 @@ release: ## goreleaser build $(GOPATH)/bin/goreleaser build --clean --single-target --snapshot .PHONY: build -build: ## goreleaser build - go build --tags=all +build: ## go build + go build -v --tags=all -ldflags=$(LDFLAGS) -o $(NAME) main.go .PHONY: docker container: ## docker build diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..3ffff17 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + version string + commit string + date string + output string +) + +var versionCmd = &cobra.Command{ + Use: "version", + Run: func(cmd *cobra.Command, args []string) { + if cmd.Flag("commit").Value.String() == "true" { + output = commit + if date != "" { + output += " built @ " + date + } + fmt.Println(output) + } else { + fmt.Println(version) + } + }, +} + +func init() { + versionCmd.Flags().Bool("commit", false, "show the version commit") + rootCmd.AddCommand(versionCmd) +} + +func SetVersionInfo(buildVersion string, buildCommit string, buildDate string) { + version = buildVersion + commit = buildCommit + date = buildDate +} diff --git a/config.env b/config.env index e92c519..dc6632d 100644 --- a/config.env +++ b/config.env @@ -2,4 +2,4 @@ NAME=magellan VERSION=$(shell git describe --tags --abbrev=0) - +BUILD=$(shell git rev-parse --short HEAD) diff --git a/main.go b/main.go index ebe2d95..0d248c4 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,13 @@ import ( "github.com/OpenCHAMI/magellan/cmd" ) +var ( + version string + commit string + date string +) + func main() { + cmd.SetVersionInfo(version, commit, date) cmd.Execute() }