Integrate version numbering with govvv

See: https://oddcode.daveamit.com/2018/08/17/embed-versioning-information-in-golang-binary/
This commit is contained in:
Siegfried Siegert 2020-09-02 18:02:40 +02:00
parent f2066c7de6
commit b9539d6db9
3 changed files with 82 additions and 4 deletions

View File

@ -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}"

1
go.mod
View File

@ -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

37
main.go
View File

@ -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)