Skip to content

Commit

Permalink
feat(build/cmd): include commit/tag and time in 'version' cmd
Browse files Browse the repository at this point in the history
uses tag if it matches the current commit, or the latest commit
otherwise.

fixes #113

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed May 24, 2018
1 parent 8715b44 commit 6ba212b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
$(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
8 changes: 7 additions & 1 deletion cmd/libasciidoc/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package main
import (
"fmt"

"github.com/bytesparadise/libasciidoc"
"github.com/spf13/cobra"
)

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)
},
}
9 changes: 9 additions & 0 deletions libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6ba212b

Please sign in to comment.