From 831cac31968dbec673cfd0a20558cf6b504d4572 Mon Sep 17 00:00:00 2001 From: Dominic Breuker Date: Thu, 22 Aug 2019 17:56:12 +0200 Subject: [PATCH] add version logging to startup log messages for better troubleshooting --- Makefile | 15 +++++++++------ main.go | 10 +++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 0ab7e06..efbb126 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ DEV_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.development TEST_IMAGE = local/pspy-testing:latest TEST_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.testing +VERSION = `git describe --tags --always || echo "unknown"` +BUILD_SHA = `git rev-parse HEAD || echo "unknown"` + # Run unit test and integration test inside container test: docker build -f $(TEST_DOCKERFILE) -t $(TEST_IMAGE) . @@ -38,7 +41,7 @@ example: # builds one set of static binaries that should work on any system without dependencies, but are huge # builds another set of binaries that are as small as possible, but may not work build: - sh -c "if ! docker image ls | grep '$(BUILD_IMAGE)' | cut -d ':' -f1; then echo 'building build image'; docker build -f $(BUILD_DOCKERFILE) -t $(BUILD_IMAGE) .; fi" + # sh -c "if ! docker image ls | grep '$(BUILD_IMAGE)' | cut -d ':' -f1; then echo 'building build image'; docker build -f $(BUILD_DOCKERFILE) -t $(BUILD_IMAGE) .; fi" mkdir -p $(PROJECT_DIR)/bin docker run -it \ @@ -47,8 +50,8 @@ build: -w "/go/src/github.com/dominicbreuker" \ --env CGO_ENABLED=0 \ --env GOOS=linux \ - --env GOARCH=386 \ - $(BUILD_IMAGE) /bin/sh -c "go build -a -ldflags '-extldflags \"-static\"' -o pspy/bin/pspy32 pspy/main.go" + --env GOARCH=386 \ + $(BUILD_IMAGE) /bin/sh -c "go build -a -ldflags '-s -w -X main.version=${VERSION} -X main.commit=${BUILD_SHA} -extldflags \"-static\"' -o pspy/bin/pspy32 pspy/main.go" docker run -it \ --rm \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ @@ -56,18 +59,18 @@ build: --env CGO_ENABLED=0 \ --env GOOS=linux \ --env GOARCH=amd64 \ - $(BUILD_IMAGE) /bin/sh -c "go build -a -ldflags '-extldflags \"-static\"' -o pspy/bin/pspy64 pspy/main.go" + $(BUILD_IMAGE) /bin/sh -c "go build -a -ldflags '-s -w -X main.version=${VERSION} -X main.commit=${BUILD_SHA} -extldflags \"-static\"' -o pspy/bin/pspy64 pspy/main.go" docker run -it \ --rm \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ -w "/go/src/github.com/dominicbreuker" \ --env GOOS=linux \ --env GOARCH=386 \ - $(BUILD_IMAGE) /bin/sh -c "go build -ldflags '-w -s' -o pspy/bin/pspy32s pspy/main.go && upx pspy/bin/pspy32s" + $(BUILD_IMAGE) /bin/sh -c "go build -ldflags '-w -s -X main.version=${VERSION} -X main.commit=${BUILD_SHA}' -o pspy/bin/pspy32s pspy/main.go && upx pspy/bin/pspy32s" docker run -it \ --rm \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ -w "/go/src/github.com/dominicbreuker" \ --env GOOS=linux \ --env GOARCH=amd64 \ - $(BUILD_IMAGE) /bin/sh -c "go build -ldflags '-w -s' -o pspy/bin/pspy64s pspy/main.go && upx pspy/bin/pspy64s" + $(BUILD_IMAGE) /bin/sh -c "go build -ldflags '-w -s -X main.version=${VERSION} -X main.commit=${BUILD_SHA}' -o pspy/bin/pspy64s pspy/main.go && upx pspy/bin/pspy64s" diff --git a/main.go b/main.go index b826cf8..c49d05d 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,15 @@ package main -import "github.com/dominicbreuker/pspy/cmd" +import ( + "fmt" + + "github.com/dominicbreuker/pspy/cmd" +) + +var version string +var commit string func main() { + fmt.Printf("pspy - version: %s - Commit SHA: %s\n", version, commit) cmd.Execute() }