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 16a8c31
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 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
1 change: 1 addition & 0 deletions cmd/libasciidoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func main() {
rootCmd := NewRootCmd()
versionCmd := NewVersionCmd()
rootCmd.AddCommand(versionCmd)
rootCmd.SetHelpCommand(helpCommand)
// rootCmd.SetHelpTemplate(helpTemplate)
Expand Down
15 changes: 0 additions & 15 deletions cmd/libasciidoc/version.go

This file was deleted.

24 changes: 24 additions & 0 deletions cmd/libasciidoc/version_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"

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

// NewVersionCmd returns the root command
func NewVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number of libasciidoc",
Run: func(cmd *cobra.Command, args []string) {
if libasciidoc.BuildTag != "" {
fmt.Fprintf(cmd.OutOrStdout(), "tag: %s\n", libasciidoc.BuildTag)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "commit: %s\n", libasciidoc.BuildCommit)
}
fmt.Fprintf(cmd.OutOrStdout(), "build time: %s\n", libasciidoc.BuildTime)
},
}
}
26 changes: 26 additions & 0 deletions cmd/libasciidoc/version_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main_test

import (
"bytes"

main "github.com/bytesparadise/libasciidoc/cmd/libasciidoc"
. "github.com/onsi/ginkgo"
"github.com/stretchr/testify/require"
)

var _ = Describe("version cmd", func() {

It("ok", func() {
// given
versionCmd := main.NewVersionCmd()
buf := new(bytes.Buffer)
versionCmd.SetOutput(buf)
versionCmd.SetArgs([]string{})
// when
err := versionCmd.Execute()
// then
require.NoError(GinkgoT(), err)
require.NotEmpty(GinkgoT(), buf.String())
})

})
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 16a8c31

Please sign in to comment.