Skip to content

Commit

Permalink
add version command to print the build version (#51)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ajatprabha authored Jul 13, 2020
1 parent c910e75 commit 28e6dc1
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
22 changes: 22 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
31 changes: 26 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var rootCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(serverCmd)
rootCmd.AddCommand(newVersionCmd())
}

// Run function lets you run the commands
Expand Down
32 changes: 32 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -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()))
}
28 changes: 28 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 28e6dc1

Please sign in to comment.