Skip to content

Commit 84ca8dd

Browse files
committed
add build info into binary
1 parent fdf1a9c commit 84ca8dd

File tree

8 files changed

+122
-4
lines changed

8 files changed

+122
-4
lines changed

.github/workflows/release.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ jobs:
2323
goarch: ${{ matrix.goarch }}
2424
goversion: https://go.dev/dl/go1.20.3.linux-amd64.tar.gz
2525
extra_files: README.md
26+
build_command: make build
27+
project_path: dists

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
build
1+
dists

Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
GO ?= go
2+
GOFMT ?= $(GO)fmt
3+
4+
VERSION ?= $(shell cat VERSION)
5+
GIT_BRANCH ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
6+
GIT_COMMIT ?= $(shell git rev-parse HEAD)
7+
USER ?= $(shell echo $USER)
8+
DATE ?= $(shell echo `date`)
9+
10+
APP_BIN ?= dists/aws-dynamodb-cache-lambda-extension
11+
APP_SRC ?= main.go
12+
13+
14+
.PHONY: build
15+
build: go-vet
16+
@echo ">> building binary file"
17+
@go build \
18+
-ldflags "-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.Version=$(VERSION)' \
19+
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.Revision=$(GIT_COMMIT)' \
20+
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.Branch=$(GIT_BRANCH)' \
21+
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.BuildUser=$(USER)' \
22+
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.BuildDate=$(DATE)'" \
23+
-o $(APP_BIN) $(APP_SRC)
24+
25+
26+
.PHONY: go-run
27+
go-run: go-vet
28+
@go run $(APP_SRC)
29+
30+
31+
.PHONY: go-fmt
32+
go-fmt:
33+
@echo ">> checking code style"
34+
@fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
35+
if [ -n "$${fmtRes}" ]; then \
36+
echo "go fmt checking failed!"; echo "$${fmtRes}"; echo; \
37+
exit 1; \
38+
fi
39+
40+
41+
.PHONY: go-vet
42+
go-vet:
43+
@echo ">> vetting code"
44+
$(GO) vet $(GOOPTS) ./...

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.0

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
module github.com/nthienan/aws-dynamodb-cache-lambda-extension
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/aws/aws-sdk-go v1.44.239
77
github.com/gorilla/mux v1.8.0
88
gopkg.in/yaml.v2 v2.4.0
99
)
1010

11-
require github.com/jmespath/go-jmespath v0.4.0 // indirect
11+
require (
12+
github.com/alecthomas/kingpin/v2 v2.3.2 // indirect
13+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/jmespath/go-jmespath v0.4.0 // indirect
16+
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
17+
)

go.sum

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
github.com/alecthomas/kingpin/v2 v2.3.2 h1:H0aULhgmSzN8xQ3nX1uxtdlTHYoPLu5AhHxWrKI6ocU=
2+
github.com/alecthomas/kingpin/v2 v2.3.2/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
3+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
4+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
15
github.com/aws/aws-sdk-go v1.44.239 h1:AenB6byCYGSBb30q99CGYqFbqpLpWrTidzm7MzxtuPo=
26
github.com/aws/aws-sdk-go v1.44.239/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
3-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
47
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
510
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
611
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
712
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
@@ -12,6 +17,9 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
1217
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1318
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1419
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
21+
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
22+
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
1523
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1624
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1725
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -41,6 +49,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
4149
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4250
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4351
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
52+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4453
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4554
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
4655
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

internal/version/version.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package version
2+
3+
import (
4+
"bytes"
5+
"runtime"
6+
"strings"
7+
"text/template"
8+
)
9+
10+
var (
11+
Version string
12+
Revision string
13+
Branch string
14+
BuildUser string
15+
BuildDate string
16+
GoVersion = runtime.Version()
17+
GoOS = runtime.GOOS
18+
GoArch = runtime.GOARCH
19+
)
20+
21+
func Print(program string) string {
22+
var versionTemplate = `
23+
{{.program}}, version: {{.version}} (branch: {{.branch}}, revision: {{.revision}})
24+
build user: {{.buildUser}}
25+
build date: {{.buildDate}}
26+
go version: {{.goVersion}}
27+
platform: {{.platform}}
28+
`
29+
30+
m := map[string]string{
31+
"program": program,
32+
"version": Version,
33+
"branch": Branch,
34+
"revision": Revision,
35+
"buildUser": BuildUser,
36+
"buildDate": BuildDate,
37+
"goVersion": GoVersion,
38+
"platform": GoOS + "/" + GoArch,
39+
}
40+
41+
t := template.Must(template.New("version").Parse(versionTemplate))
42+
var buffer bytes.Buffer
43+
if err := t.ExecuteTemplate(&buffer, "version", m); err != nil {
44+
panic(err)
45+
}
46+
47+
return strings.TrimSpace(buffer.String())
48+
}

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ import (
99
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/extension"
1010
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/ipc"
1111
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/plugins"
12+
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version"
13+
"github.com/alecthomas/kingpin/v2"
1214
)
1315

1416
var (
1517
extensionClient = extension.NewClient(os.Getenv("AWS_LAMBDA_RUNTIME_API"))
1618
)
1719

1820
func main() {
21+
kingpin.Version(version.Print("aws-dynamodb-cache-lambda-extension"))
22+
23+
// parse flags
24+
kingpin.HelpFlag.Short('h')
25+
kingpin.Parse()
26+
1927
ctx, cancel := context.WithCancel(context.Background())
2028

2129
sigs := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)