Skip to content

Commit

Permalink
add build info into binary
Browse files Browse the repository at this point in the history
  • Loading branch information
nthienan committed Apr 9, 2023
1 parent fdf1a9c commit 84ca8dd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ jobs:
goarch: ${{ matrix.goarch }}
goversion: https://go.dev/dl/go1.20.3.linux-amd64.tar.gz
extra_files: README.md
build_command: make build
project_path: dists
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build
dists
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
GO ?= go
GOFMT ?= $(GO)fmt

VERSION ?= $(shell cat VERSION)
GIT_BRANCH ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
GIT_COMMIT ?= $(shell git rev-parse HEAD)
USER ?= $(shell echo $USER)
DATE ?= $(shell echo `date`)

APP_BIN ?= dists/aws-dynamodb-cache-lambda-extension
APP_SRC ?= main.go


.PHONY: build
build: go-vet
@echo ">> building binary file"
@go build \
-ldflags "-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.Version=$(VERSION)' \
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.Revision=$(GIT_COMMIT)' \
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.Branch=$(GIT_BRANCH)' \
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.BuildUser=$(USER)' \
-X 'github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version.BuildDate=$(DATE)'" \
-o $(APP_BIN) $(APP_SRC)


.PHONY: go-run
go-run: go-vet
@go run $(APP_SRC)


.PHONY: go-fmt
go-fmt:
@echo ">> checking code style"
@fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
if [ -n "$${fmtRes}" ]; then \
echo "go fmt checking failed!"; echo "$${fmtRes}"; echo; \
exit 1; \
fi


.PHONY: go-vet
go-vet:
@echo ">> vetting code"
$(GO) vet $(GOOPTS) ./...
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.0
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module github.com/nthienan/aws-dynamodb-cache-lambda-extension

go 1.19
go 1.20

require (
github.com/aws/aws-sdk-go v1.44.239
github.com/gorilla/mux v1.8.0
gopkg.in/yaml.v2 v2.4.0
)

require github.com/jmespath/go-jmespath v0.4.0 // indirect
require (
github.com/alecthomas/kingpin/v2 v2.3.2 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
)
11 changes: 10 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
github.com/alecthomas/kingpin/v2 v2.3.2 h1:H0aULhgmSzN8xQ3nX1uxtdlTHYoPLu5AhHxWrKI6ocU=
github.com/alecthomas/kingpin/v2 v2.3.2/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/aws/aws-sdk-go v1.44.239 h1:AenB6byCYGSBb30q99CGYqFbqpLpWrTidzm7MzxtuPo=
github.com/aws/aws-sdk-go v1.44.239/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand All @@ -12,6 +17,9 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down Expand Up @@ -41,6 +49,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
48 changes: 48 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package version

import (
"bytes"
"runtime"
"strings"
"text/template"
)

var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
GoVersion = runtime.Version()
GoOS = runtime.GOOS
GoArch = runtime.GOARCH
)

func Print(program string) string {
var versionTemplate = `
{{.program}}, version: {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
platform: {{.platform}}
`

m := map[string]string{
"program": program,
"version": Version,
"branch": Branch,
"revision": Revision,
"buildUser": BuildUser,
"buildDate": BuildDate,
"goVersion": GoVersion,
"platform": GoOS + "/" + GoArch,
}

t := template.Must(template.New("version").Parse(versionTemplate))
var buffer bytes.Buffer
if err := t.ExecuteTemplate(&buffer, "version", m); err != nil {
panic(err)
}

return strings.TrimSpace(buffer.String())
}
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ import (
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/extension"
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/ipc"
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/plugins"
"github.com/nthienan/aws-dynamodb-cache-lambda-extension/internal/version"
"github.com/alecthomas/kingpin/v2"
)

var (
extensionClient = extension.NewClient(os.Getenv("AWS_LAMBDA_RUNTIME_API"))
)

func main() {
kingpin.Version(version.Print("aws-dynamodb-cache-lambda-extension"))

// parse flags
kingpin.HelpFlag.Short('h')
kingpin.Parse()

ctx, cancel := context.WithCancel(context.Background())

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

0 comments on commit 84ca8dd

Please sign in to comment.