From 09fd2dfbc744d756cd7c4197598f9f96d6438444 Mon Sep 17 00:00:00 2001
From: gjahn <gregor.jahn@bht-berlin.de>
Date: Thu, 14 Dec 2023 00:48:56 +0100
Subject: [PATCH] Allow to set version during build time

---
 Makefile                | 1 +
 README.md               | 8 +++++---
 configuration/config.go | 8 ++++++--
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index f43fbb3..8f34902 100644
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,7 @@ $(BIN_DIR)/artifact.bin:
 	cd $(SRC_DIR) \
 	&& go build \
 		-o $(@) \
+		-ldflags "-X webservice/configuration.version=0.0.1" \
 		$(SRC_DIR)/*.go
 
 .PHONY: build-linux
diff --git a/README.md b/README.md
index e8c68c9..74b1ca8 100644
--- a/README.md
+++ b/README.md
@@ -24,9 +24,11 @@ information checkout the [configuration code](./configuration/config.go).
 3. Execute unit tests: `go test -race -v ./...`
 4. Build artifact: `go build -o ./artifact.bin ./*.go`
 
-To build for another platform, set `GOOS` and `GOARCH`. To yield a static binary (fully
-self-contained, no dynamic linking) set `CGO_ENABLED=0`. For more details, please refer
-to the [Makefile](./Makefile).
+To build for another platform, set `GOOS` and `GOARCH`. To yield a static
+binary (fully self-contained, no dynamic linking) set `CGO_ENABLED=0`. 
+To set a version during build time, add the following CLI option
+`-ldflags "-X webservice/configuration.version=${VERSION}"`.
+For more information, please refer to the [Makefile](./Makefile).
 
 
 #### Run:
diff --git a/configuration/config.go b/configuration/config.go
index be25fb6..2286c4b 100644
--- a/configuration/config.go
+++ b/configuration/config.go
@@ -12,9 +12,11 @@ import (
 
 const BODY_SIZE_LIMIT = 32 * 1024 * 1024    // 32 MB, in bytes
 
+var version string = "n/a"
+
 
 type Config struct {
-    Version     string `env:"VERSION"   envDefault:"N/A"`
+    Version     string
 
     LogLevel    string `env:"LOG_LEVE"  envDefault:"error"`
 
@@ -31,7 +33,9 @@ type Config struct {
 
 
 func New() ( *Config, error ){
-    cfg := Config{}
+    cfg := Config{
+        Version: version,
+    }
 
     if err := configParser.Parse( &cfg ); err != nil {
         return nil, err
-- 
GitLab