From b9539d6db90fa0916fa08e60f097ad50d40107bb Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Wed, 2 Sep 2020 18:02:40 +0200 Subject: [PATCH] Integrate version numbering with govvv See: https://oddcode.daveamit.com/2018/08/17/embed-versioning-information-in-golang-binary/ --- build.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++---- go.mod | 1 + main.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/build.sh b/build.sh index 7318b1e..a5a0ac9 100755 --- a/build.sh +++ b/build.sh @@ -2,14 +2,54 @@ TARGET=mqttListener +export PATH=$PATH:~/go/bin + + + + + + +RED='\e[0;31m' +GREEN='\e[0;32m' +LIGHT_YELLOW='\e[93m' +NC='\e[0m' # No Color +BOLD='\e[1m' +NORMAL='\e[21m' + + + +####################################################################################### +# functions + +die() { echo -e "\n${RED}$@${NC}" 1>&2 ; exit 1; } + + + +####################################################################################### + #cd /src +if ! command -v govvv &> /dev/null ; then + die "govv is not installed" +fi -echo "Building for Linux 64-bit" -env GOOS=linux GOARCH=amd64 go build -o ${TARGET}-linux-amd64 -echo "Building for arm" -env GOOS=linux GOARCH=arm go build -o ${TARGET}-linux-arm +####################################################################################### +# build +echo -e "${NC}\n " + +echo -e "\nBuilding for Linux 64-bit:" +env GOOS=linux GOARCH=amd64 go build -a -v -ldflags "$(govvv -flags)" -o ${TARGET}-linux-amd64 +echo -e "\t\t${GREEN}... done${NC}" + + +echo -e "\nBuilding for arm:" +env GOOS=linux GOARCH=arm go build -a -v -ldflags "$(govvv -flags)" -o ${TARGET}-linux-arm +echo -e "\t\t${GREEN}... done${NC}" + +echo -e "\nstipping arm binary:" +strip -o ${TARGET}-linux-arm-stripped ${TARGET}-linux-arm +echo -e "\t\t${GREEN}... done${NC}" diff --git a/go.mod b/go.mod index c247699..fb841fe 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module mqttListener go 1.14 require ( + github.com/ahmetb/govvv v0.3.0 // indirect github.com/eclipse/paho.mqtt.golang v1.2.0 github.com/mattn/go-sqlite3 v1.14.2 github.com/mmcdole/gofeed v1.0.0 diff --git a/main.go b/main.go index 5dfcd80..96f7483 100644 --- a/main.go +++ b/main.go @@ -11,13 +11,50 @@ import ( "os" "os/signal" + "path/filepath" "syscall" "net/http" ) +// --------------------------------------------------------------------------- +// Following variables will be statically linked at the time of compiling + +// GitCommit holds short commit hash of source tree +var GitCommit string + +// GitBranch holds current branch name the code is built off +var GitBranch string + +// GitState shows whether there are uncommitted changes +var GitState string + +// GitSummary holds output of git describe --tags --dirty --always +var GitSummary string + +// BuildDate holds RFC3339 formatted UTC date (build time) +var BuildDate string + +// Version holds contents of ./VERSION file, if exists, or the value passed via the -version option +var Version string + +func PrintVersion() { + var versionString string = GitSummary + fmt.Println("\n--------------------------------------------------------") + fmt.Println(filepath.Base(os.Args[0]), " Version Information: ") + fmt.Println("\tVersion:\t", versionString) + fmt.Println("\tBranch:\t\t", GitBranch) + fmt.Println("\tCommit-ID:\t", GitCommit) + fmt.Println("\tBuilt:\t\t", BuildDate) + fmt.Println("\n--------------------------------------------------------") + fmt.Println("") +} + +// --------------------------------------------------------------------------- func main() { + PrintVersion() + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM)