diff --git a/Makefile b/Makefile index dfe58783..40d93aca 100644 --- a/Makefile +++ b/Makefile @@ -104,5 +104,13 @@ test: deps generate .PHONY: build ## builds the binary executable from CLI build: $(INSTALL_PREFIX) deps - @echo "building $(BINARY_PATH) ..." - @go build -o $(BINARY_PATH) cmd/libasciidoc/*.go \ No newline at end of file + $(eval BUILD_COMMIT:=$(shell git rev-parse --short HEAD)) + $(eval BUILD_TAG:=$(shell git tag --contains $(BUILD_COMMIT))) + $(eval BUILD_TIME:=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')) + @echo "building $(BINARY_PATH) (commit:$(BUILD_COMMIT) / tag:$(BUILD_TAG) / time:$(BUILD_TIME))" + @go build -ldflags \ + " -X github.com/bytesparadise/libasciidoc.BuildCommit=$(BUILD_COMMIT)\ + -X github.com/bytesparadise/libasciidoc.BuildTag=$(BUILD_TAG) \ + -X github.com/bytesparadise/libasciidoc.BuildTime=$(BUILD_TIME)" \ + -o $(BINARY_PATH) \ + cmd/libasciidoc/*.go \ No newline at end of file diff --git a/cmd/libasciidoc/version.go b/cmd/libasciidoc/version.go index f8cae8dc..b51906d8 100644 --- a/cmd/libasciidoc/version.go +++ b/cmd/libasciidoc/version.go @@ -3,6 +3,7 @@ package main import ( "fmt" + "github.com/bytesparadise/libasciidoc" "github.com/spf13/cobra" ) @@ -10,6 +11,11 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of libasciidoc", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("~HEAD") + if libasciidoc.BuildTag != "" { + fmt.Printf("tag: %s\n", libasciidoc.BuildTag) + } else { + fmt.Printf("commit: %s\n", libasciidoc.BuildCommit) + } + fmt.Printf("build time: %s\n", libasciidoc.BuildTime) }, } diff --git a/libasciidoc.go b/libasciidoc.go index 0cdc574a..0c8ece95 100644 --- a/libasciidoc.go +++ b/libasciidoc.go @@ -12,6 +12,15 @@ import ( log "github.com/sirupsen/logrus" ) +var ( + // BuildCommit lastest build commit (set by Makefile) + BuildCommit = "" + // BuildTag if the `BuildCommit` matches a tag + BuildTag = "" + // BuildTime set by build script (set by Makefile) + BuildTime = "" +) + // ConvertFileToHTML converts the content of the given filename into an HTML document. // The conversion result is written in the given writer `output`, whereas the document metadata (title, etc.) (or an error if a problem occurred) is returned // as the result of the function call.