DRY ldflags; include build time into version string (#541)

This commit is contained in:
Dan Sosedoff 2021-12-30 15:33:43 -06:00 committed by GitHub
parent 706caa44bf
commit af79994595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -4,6 +4,8 @@ BUILD_TIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" | tr -d '\n')
GO_VERSION = $(shell go version | awk {'print $$3'})
DOCKER_RELEASE_TAG = "sosedoff/pgweb:$(shell git describe --abbrev=0 --tags | sed 's/v//')"
DOCKER_LATEST_TAG = "sosedoff/pgweb:latest"
LDFLAGS = -s -w
PKG = github.com/sosedoff/pgweb
usage:
@echo ""
@ -37,22 +39,21 @@ build:
go build
@echo "You can now execute ./pgweb"
release: LDFLAGS += -X $(PKG)/pkg/command.GitCommit=$(GIT_COMMIT)
release: LDFLAGS += -X $(PKG)/pkg/command.BuildTime=$(BUILD_TIME)
release: LDFLAGS += -X $(PKG)/pkg/command.GoVersion=$(GO_VERSION)
release:
@echo "Building binaries..."
@gox \
-osarch "$(TARGETS)" \
-ldflags "-s -w -X github.com/sosedoff/pgweb/pkg/command.GitCommit=$(GIT_COMMIT) -X github.com/sosedoff/pgweb/pkg/command.BuildTime=$(BUILD_TIME) -X github.com/sosedoff/pgweb/pkg/command.GoVersion=$(GO_VERSION)" \
-ldflags "$(LDFLAGS)" \
-output "./bin/pgweb_{{.OS}}_{{.Arch}}"
@echo "Building ARM binaries..."
GOOS=linux GOARCH=arm GOARM=5 go build \
-ldflags "-s -w -X github.com/sosedoff/pgweb/pkg/command.GitCommit=$(GIT_COMMIT) -X github.com/sosedoff/pgweb/pkg/command.BuildTime=$(BUILD_TIME) -X github.com/sosedoff/pgweb/pkg/command.GoVersion=$(GO_VERSION)" \
-o "./bin/pgweb_linux_arm_v5"
GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "$(LDFLAGS)" -o "./bin/pgweb_linux_arm_v5"
@echo "Building ARM64 binaries..."
GOOS=linux GOARCH=arm64 GOARM=7 go build \
-ldflags "-s -w -X github.com/sosedoff/pgweb/pkg/command.GitCommit=$(GIT_COMMIT) -X github.com/sosedoff/pgweb/pkg/command.BuildTime=$(BUILD_TIME) -X github.com/sosedoff/pgweb/pkg/command.GoVersion=$(GO_VERSION)" \
-o "./bin/pgweb_linux_arm64_v7"
GOOS=linux GOARCH=arm64 GOARM=7 go build -ldflags "$(LDFLAGS)" -o "./bin/pgweb_linux_arm64_v7"
@echo "\nPackaging binaries...\n"
@./script/package.sh

View File

@ -161,12 +161,19 @@ func initOptions() {
func printVersion() {
chunks := []string{fmt.Sprintf("Pgweb v%s", command.Version)}
if command.GitCommit != "" {
chunks = append(chunks, fmt.Sprintf("(git: %s)", command.GitCommit))
}
if command.GoVersion != "" {
chunks = append(chunks, fmt.Sprintf("(go: %s)", command.GoVersion))
}
if command.BuildTime != "" {
chunks = append(chunks, fmt.Sprintf("(build time: %s)", command.BuildTime))
}
fmt.Println(strings.Join(chunks, " "))
}