Skip to content

Commit 43a1c73

Browse files
authored
Add a version command (#10)
* Add a Makefile that injects build info into the binary * Use goreleaser and update circleci to use it * Include a version command that prints injected build information
1 parent ded7245 commit 43a1c73

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

.circleci/config.yml

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1-
version: 2
1+
version: 2.1
2+
workflows:
3+
main:
4+
jobs:
5+
- build
6+
- release:
7+
filters:
8+
branches:
9+
ignore: /.*/
10+
tags:
11+
only: /v[0-9]+(\.[0-9]+)*(-.*)*/
212
jobs:
13+
release:
14+
docker:
15+
- image: circleci/golang:1.13
16+
steps:
17+
- checkout
18+
- run:
19+
name: goreleaser
20+
command: |
21+
export Date=$(date +"%F %X")
22+
export Rev=$(git rev-parse HEAD)
23+
curl -sL https://git.io/goreleaser | bash
24+
325
build:
426
docker:
5-
- image: circleci/golang:1.12
27+
- image: circleci/golang:1.13
628

729
working_directory: /go/src/github.com/tlkamp/mockbob
830
environment:

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
!**/*.md
44
!**/
55
!LICENSE
6+
!Makefile
7+
!.goreleaser.yml

.goreleaser.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This is an example goreleaser.yaml file with some sane defaults.
2+
# Make sure to check the documentation at http://goreleaser.com
3+
before:
4+
hooks:
5+
# You may remove this if you don't use go modules.
6+
- go mod download
7+
builds:
8+
- id: custom-build
9+
env:
10+
- CGO_ENABLED=0
11+
ldflags:
12+
- -X 'github.com/tlkamp/mockbob/cmd.buildDate={{.Env.Date}}'
13+
- -X 'github.com/tlkamp/mockbob/cmd.versionInfo={{.Version}}'
14+
- -X 'github.com/tlkamp/mockbob/cmd.gitRev={{.Env.Rev}}'
15+
16+
archives:
17+
- replacements:
18+
darwin: Darwin
19+
linux: Linux
20+
windows: Windows
21+
386: i386
22+
amd64: x86_64
23+
checksum:
24+
name_template: 'checksums.txt'
25+
snapshot:
26+
name_template: "{{ .Tag }}-next"
27+
changelog:
28+
sort: asc
29+
filters:
30+
exclude:
31+
- '^docs:'
32+
- '^test:'

Makefile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
TAG := $(shell git describe --always --long)
2+
REV := $(shell git rev-parse HEAD)
3+
DATE := $(shell date +"%F %X")
4+
5+
6+
default: build
7+
8+
test: export GO111MODULE = on
9+
test:
10+
@cd .. && go install gotest.tools/gotestsum
11+
@gotestsum --junitfile test-results.xml -- -covermode=count -coverprofile=count.out ./...
12+
@go tool cover -func=count.out
13+
@go tool cover -html=count.out -o coverage.html
14+
@go clean -i gotest.tools/gotestsum
15+
16+
build:
17+
go build
18+
19+
release:
20+
@go build -ldflags="-X 'github.com/tlkamp/mockbob/cmd.versionInfo=$(TAG)' -X 'github.com/tlkamp/mockbob/cmd.gitRev=$(REV)' -X 'github.com/tlkamp/mockbob/cmd.buildDate=$(DATE)'"
21+
22+
safe-clean:
23+
@git clean -Xi
24+
25+
clean:
26+
@git clean -Xf

cmd/root.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,26 @@ import (
88
"github.com/tlkamp/mockbob/internal"
99
)
1010

11-
var startCaps bool
11+
var (
12+
versionInfo string = "No version specified"
13+
gitRev string = "No revision specified"
14+
buildDate string = "No date specified"
15+
16+
versionMessage string = fmt.Sprintf(`Version: %v
17+
Revision: %v
18+
Build Date: %v`, versionInfo, gitRev, buildDate)
19+
20+
startCaps bool
21+
)
22+
23+
var versionCmd = &cobra.Command{
24+
Use: "version",
25+
Short: "Display information about the version of mockbob.",
26+
Long: `Displays version information about mockbob, including information from the SCM, git.`,
27+
Run: func(cmd *cobra.Command, args []string) {
28+
fmt.Println(versionMessage)
29+
},
30+
}
1231

1332
var rootCmd = &cobra.Command{
1433
Use: "mockbob [word or sentence (quoted)]",
@@ -55,4 +74,5 @@ func Execute() {
5574

5675
func init() {
5776
rootCmd.Flags().BoolVarP(&startCaps, "start-caps", "c", false, "start the text with a capital letter")
77+
rootCmd.AddCommand(versionCmd)
5878
}

0 commit comments

Comments
 (0)