Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build/cmd): include commit/tag and time in 'version' cmd #114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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