From 28e6dc196a250f9494d06c56181795a33e618b1e Mon Sep 17 00:00:00 2001 From: Ajat Prabha Date: Mon, 13 Jul 2020 10:50:22 +0530 Subject: [PATCH] add version command to print the build version (#51) * add test for version command * implement version command * add test for detailed version command * implement detailed version command * add newVersionCmd to rootCmd * add ldflags for build * remove TDD cycle comments --- .goreleaser.yml | 22 ++++++++++++++ Makefile | 31 ++++++++++++++++---- cmd/root.go | 1 + cmd/version.go | 32 +++++++++++++++++++++ cmd/version_test.go | 57 +++++++++++++++++++++++++++++++++++++ internal/version/version.go | 28 ++++++++++++++++++ 6 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 cmd/version.go create mode 100644 cmd/version_test.go create mode 100644 internal/version/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index a8132be..95b4ca3 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -13,12 +13,22 @@ builds: - CGO_ENABLED=1 ldflags: - -w -s -extldflags "-static" + - -X github.com/gojek/darkroom/internal/version.version={{ .Version }} + - -X github.com/gojek/darkroom/internal/version.gitTag={{ .Tag }} + - -X github.com/gojek/darkroom/internal/version.gitCommit={{ .FullCommit }} + - -X github.com/gojek/darkroom/internal/version.buildDate={{ .Date }} - id: darkroom_windows_32bit main: . goos: - windows goarch: - 386 + ldflags: + - -w -s + - -X github.com/gojek/darkroom/internal/version.version={{ .Version }} + - -X github.com/gojek/darkroom/internal/version.gitTag={{ .Tag }} + - -X github.com/gojek/darkroom/internal/version.gitCommit={{ .FullCommit }} + - -X github.com/gojek/darkroom/internal/version.buildDate={{ .Date }} env: - CGO_ENABLED=1 - CC=i686-w64-mingw32-gcc @@ -29,6 +39,12 @@ builds: - windows goarch: - amd64 + ldflags: + - -w -s + - -X github.com/gojek/darkroom/internal/version.version={{ .Version }} + - -X github.com/gojek/darkroom/internal/version.gitTag={{ .Tag }} + - -X github.com/gojek/darkroom/internal/version.gitCommit={{ .FullCommit }} + - -X github.com/gojek/darkroom/internal/version.buildDate={{ .Date }} env: - CGO_ENABLED=1 - CC=x86_64-w64-mingw32-gcc @@ -40,6 +56,12 @@ builds: goarch: - amd64 - 386 + ldflags: + - -w -s + - -X github.com/gojek/darkroom/internal/version.version={{ .Version }} + - -X github.com/gojek/darkroom/internal/version.gitTag={{ .Tag }} + - -X github.com/gojek/darkroom/internal/version.gitCommit={{ .FullCommit }} + - -X github.com/gojek/darkroom/internal/version.buildDate={{ .Date }} env: - CGO_ENABLED=1 - CC=o64-clang diff --git a/Makefile b/Makefile index 6fc528f..2aea037 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,36 @@ APP=darkroom APP_EXECUTABLE="./out/$(APP)" +BUILD_INFO_GIT_TAG ?= $(shell git describe --tags 2>/dev/null || echo unknown) +BUILD_INFO_GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo unknown) +BUILD_INFO_BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" || echo unknown) +BUILD_INFO_VERSION ?= $(shell prefix=$$(echo $(BUILD_INFO_GIT_TAG) | cut -c 1); if [ "$${prefix}" = "v" ]; then echo $(BUILD_INFO_GIT_TAG) | cut -c 2- ; else echo $(BUILD_INFO_GIT_TAG) ; fi) + +build_info_fields := \ + version=$(BUILD_INFO_VERSION) \ + gitTag=$(BUILD_INFO_GIT_TAG) \ + gitCommit=$(BUILD_INFO_GIT_COMMIT) \ + buildDate=$(BUILD_INFO_BUILD_DATE) +build_info_ld_flags := $(foreach entry,$(build_info_fields),-X github.com/gojek/darkroom/internal/version.$(entry)) + +LD_FLAGS := -ldflags="-s -w $(build_info_ld_flags)" +GOOS := $(shell go env GOOS) +GOARCH := $(shell go env GOARCH) +GO_BUILD := GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=1 go build $(LD_FLAGS) +GO_RUN := CGO_ENABLED=1 go run $(LD_FLAGS) + all: test-ci setup: go get golang.org/x/lint/golint go get github.com/mattn/goveralls +run: copy-config + @$(GO_RUN) main.go server + compile: - mkdir -p out - go build -o $(APP_EXECUTABLE) main.go + @mkdir -p out + @$(GO_BUILD) -o $(APP_EXECUTABLE) main.go lint: @golint ./... | { grep -vwE "exported (var|function|method|type|const) \S+ should have comment" || true; } @@ -27,12 +48,12 @@ coverage: goveralls -coverprofile=profile.cov -service=travis-ci copy-config: - cp config.example.yaml config.yaml + @cp config.example.yaml config.yaml docker-image: - docker build -t ${USER}/darkroom:latest -f build/Dockerfile . + @docker build -t ${USER}/darkroom:latest -f build/Dockerfile . docker-docs: - docker build -t darkroom-docs:latest -f build/Dockerfile.docs . + @docker build -t darkroom-docs:latest -f build/Dockerfile.docs . test-ci: copy-config compile lint format vet test coverage diff --git a/cmd/root.go b/cmd/root.go index 57c6aee..e538381 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,6 +11,7 @@ var rootCmd = &cobra.Command{ func init() { rootCmd.AddCommand(serverCmd) + rootCmd.AddCommand(newVersionCmd()) } // Run function lets you run the commands diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..7a1d281 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,32 @@ +package cmd + +import ( + "fmt" + "github.com/gojek/darkroom/internal/version" + "github.com/spf13/cobra" +) + +func newVersionCmd() *cobra.Command { + args := struct { + detailed bool + }{} + cmd := &cobra.Command{ + Use: "version", + Short: "Print version", + Long: `Print version.`, + RunE: func(cmd *cobra.Command, _ []string) error { + buildInfo := version.Build + if args.detailed { + cmd.Println(fmt.Sprintf("Version: %s", buildInfo.Version)) + cmd.Println(fmt.Sprintf("Git Tag: %s", buildInfo.GitTag)) + cmd.Println(fmt.Sprintf("Git Commit: %s", buildInfo.GitCommit)) + cmd.Println(fmt.Sprintf("Build Date: %s", buildInfo.BuildDate)) + } else { + cmd.Println(buildInfo.Version) + } + return nil + }, + } + cmd.PersistentFlags().BoolVarP(&args.detailed, "detailed", "a", false, "Print detailed version") + return cmd +} diff --git a/cmd/version_test.go b/cmd/version_test.go new file mode 100644 index 0000000..c9731f0 --- /dev/null +++ b/cmd/version_test.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "bytes" + "github.com/gojek/darkroom/internal/version" + "github.com/spf13/cobra" + "github.com/stretchr/testify/suite" + "strings" + "testing" +) + +type VersionCmdSuite struct { + suite.Suite + rootCmd *cobra.Command + buf *bytes.Buffer +} + +func TestVersionCmd(t *testing.T) { + suite.Run(t, new(VersionCmdSuite)) +} + +func (s *VersionCmdSuite) SetupSuite() { + version.Build = version.BuildInfo{ + Version: "0.1.0", + GitTag: "v0.1.0", + GitCommit: "c910e75b573b48961c7dcc1dd1063a543164d963", + BuildDate: "2020-03-03T10:59:06Z", + } +} + +func (s *VersionCmdSuite) SetupTest() { + s.rootCmd = &cobra.Command{ + Use: "app", + } + s.rootCmd.AddCommand(newVersionCmd()) + s.buf = &bytes.Buffer{} + s.rootCmd.SetOut(s.buf) +} + +func (s *VersionCmdSuite) TestVersionOutput() { + s.rootCmd.SetArgs([]string{"version"}) + err := s.rootCmd.Execute() + s.NoError(err) + s.Equal(strings.TrimSpace(`0.1.0`), strings.TrimSpace(s.buf.String())) +} + +func (s *VersionCmdSuite) TestVersionDetailedOutput() { + s.rootCmd.SetArgs([]string{"version", "--detailed"}) + err := s.rootCmd.Execute() + s.NoError(err) + s.Equal(strings.TrimSpace(` +Version: 0.1.0 +Git Tag: v0.1.0 +Git Commit: c910e75b573b48961c7dcc1dd1063a543164d963 +Build Date: 2020-03-03T10:59:06Z +`), strings.TrimSpace(s.buf.String())) +} diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..20d945d --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,28 @@ +package version + +var ( + version = "unknown" + gitTag = "unknown" + gitCommit = "unknown" + buildDate = "unknown" +) + +type BuildInfo struct { + Version string + GitTag string + GitCommit string + BuildDate string +} + +var ( + Build BuildInfo +) + +func init() { + Build = BuildInfo{ + Version: version, + GitTag: gitTag, + GitCommit: gitCommit, + BuildDate: buildDate, + } +}