From 16a8c318643e2eb1245ccb7ad1a74b263671b0b2 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Thu, 24 May 2018 23:14:45 +0200 Subject: [PATCH] feat(build/cmd): include commit/tag and time in 'version' cmd uses tag if it matches the current commit, or the latest commit otherwise. fixes #113 Signed-off-by: Xavier Coulon --- Makefile | 12 ++++++++++-- cmd/libasciidoc/main.go | 1 + cmd/libasciidoc/version.go | 15 --------------- cmd/libasciidoc/version_cmd.go | 24 ++++++++++++++++++++++++ cmd/libasciidoc/version_cmd_test.go | 26 ++++++++++++++++++++++++++ libasciidoc.go | 9 +++++++++ 6 files changed, 70 insertions(+), 17 deletions(-) delete mode 100644 cmd/libasciidoc/version.go create mode 100644 cmd/libasciidoc/version_cmd.go create mode 100644 cmd/libasciidoc/version_cmd_test.go 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/main.go b/cmd/libasciidoc/main.go index 73c5078b..ba636ae0 100644 --- a/cmd/libasciidoc/main.go +++ b/cmd/libasciidoc/main.go @@ -11,6 +11,7 @@ import ( func main() { rootCmd := NewRootCmd() + versionCmd := NewVersionCmd() rootCmd.AddCommand(versionCmd) rootCmd.SetHelpCommand(helpCommand) // rootCmd.SetHelpTemplate(helpTemplate) diff --git a/cmd/libasciidoc/version.go b/cmd/libasciidoc/version.go deleted file mode 100644 index f8cae8dc..00000000 --- a/cmd/libasciidoc/version.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "fmt" - - "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") - }, -} diff --git a/cmd/libasciidoc/version_cmd.go b/cmd/libasciidoc/version_cmd.go new file mode 100644 index 00000000..d282439a --- /dev/null +++ b/cmd/libasciidoc/version_cmd.go @@ -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) + }, + } +} diff --git a/cmd/libasciidoc/version_cmd_test.go b/cmd/libasciidoc/version_cmd_test.go new file mode 100644 index 00000000..e934ed99 --- /dev/null +++ b/cmd/libasciidoc/version_cmd_test.go @@ -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()) + }) + +}) 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.