diff --git a/Makefile b/Makefile index 11f799a..53546f6 100644 --- a/Makefile +++ b/Makefile @@ -7,10 +7,15 @@ WATCHER_NAME=watcher HOST_OS=$(shell go env GOOS) HOST_ARCH=$(shell go env GOARCH) +GO ?= go +BUILD_FLAGS ?= +VERSION ?= $(shell git describe --tags --dirty --always 2>/dev/null || echo unknown) +COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown) +LDFLAGS=-X ${PACKAGE}/version.Version=${VERSION} -X ${PACKAGE}/version.Commit=${COMMIT} .PHONY: build-local build-local: - go build -o ${DIST_DIR}/${BIN_NAME} + $(GO) build $(BUILD_FLAGS) -ldflags "${LDFLAGS}" -o ${DIST_DIR}/${BIN_NAME} .PHONY: clean clean: @@ -25,6 +30,12 @@ build-binaries: make BIN_NAME=${CLI_NAME}-windows-amd64.exe GOOS=windows build-local make BIN_NAME=${CLI_NAME}-windows-386.exe GOOS=windows GOARCH=386 build-local +.PHONY: build-release +build-release: + $(eval RELEASE_VERSION := $(shell git describe --tags --exact-match 2>/dev/null)) + @test -n "${RELEASE_VERSION}" || (echo "build-release must be run from a git tag" && exit 1) + make VERSION=${RELEASE_VERSION} build-binaries + .PHONY: build-watcher build-watcher: - go build -o ${DIST_DIR}/${BIN_NAME}-${WATCHER_NAME} ${PACKAGE}/watcher \ No newline at end of file + $(GO) build $(BUILD_FLAGS) -o ${DIST_DIR}/${BIN_NAME}-${WATCHER_NAME} ${PACKAGE}/watcher diff --git a/cmd/cmd.go b/cmd/cmd.go index 16df6b9..739fce3 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -19,6 +19,7 @@ import ( "github.com/microcks/microcks-cli/pkg/config" "github.com/microcks/microcks-cli/pkg/connectors" "github.com/microcks/microcks-cli/pkg/errors" + "github.com/microcks/microcks-cli/version" "github.com/spf13/cobra" ) @@ -29,6 +30,7 @@ func NewCommad() *cobra.Command { command := &cobra.Command{ Use: "microcks", Short: "A CLI tool for Microcks", + Version: version.Info(), SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { cmd.HelpFunc()(cmd, args) @@ -37,6 +39,7 @@ func NewCommad() *cobra.Command { HiddenDefaultCmd: true, }, } + command.SetVersionTemplate("{{.Version}}\n") command.AddCommand(NewImportCommand(&clientOpts)) command.AddCommand(NewImportDirCommand(&clientOpts)) diff --git a/cmd/version.go b/cmd/version.go index 3989778..d51a5bf 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -25,10 +25,10 @@ import ( func NewVersionCommand() *cobra.Command { var command = &cobra.Command{ Use: "version", - Short: "Print the version number of microcks CLI", - Long: `Print the version number of microcks CLI`, + Short: "Print the version number of Microcks CLI", + Long: `Print the version number of Microcks CLI`, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("Microcks-CLI %s\n", version.Version) + fmt.Println(version.Info()) }, } diff --git a/version/version.go b/version/version.go index 48984dc..d007268 100644 --- a/version/version.go +++ b/version/version.go @@ -16,5 +16,10 @@ package version var ( - Version = "1.0.3" + Version = "unknown" + Commit = "unknown" ) + +func Info() string { + return "Microcks CLI " + Version + "\nCommit: " + Commit +}