From 8e784438c7ebb77eb903c964192012d8a0826d02 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 7 Dec 2018 22:20:11 +0000 Subject: [PATCH] Imprint build name and version number if available --- build | 5 +++-- src/yggdrasil/core.go | 21 +++++++++++++++++++++ yggdrasil.go | 5 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/build b/build index c07e5eac..2e7c20cc 100755 --- a/build +++ b/build @@ -14,10 +14,11 @@ go get -d -v yggdrasil for file in *.go ; do echo "Building: $file" #go build $@ $file + IMPRINT="-X yggdrasil.buildName=$(sh contrib/semver/name.sh) -X yggdrasil.buildVersion=$(sh contrib/semver/version.sh)" if [ $DEBUG ]; then - go build -tags debug -v $file + go build -ldflags="$IMPRINT" -tags debug -v $file else - go build -ldflags="-s -w" -v $file + go build -ldflags="$IMPRINT -s -w" -v $file fi if [ $UPX ]; then upx --brute ${file%.go} diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 706b8aa3..54f1820c 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -12,6 +12,9 @@ import ( "yggdrasil/defaults" ) +var buildName string +var buildVersion string + // The Core object represents the Yggdrasil node. You should create a Core // object for each Yggdrasil node you plan to run. type Core struct { @@ -59,6 +62,24 @@ func (c *Core) init(bpub *boxPubKey, c.tun.init(c) } +// Get the current build name. This is usually injected if built from git, +// or returns "unknown" otherwise. +func GetBuildName() string { + if buildName == "" { + return "unknown" + } + return buildName +} + +// Get the current build version. This is usually injected if built from git, +// or returns "unknown" otherwise. +func GetBuildVersion() string { + if buildVersion == "" { + return "unknown" + } + return buildVersion +} + // Starts up Yggdrasil using the provided NodeConfig, and outputs debug logging // through the provided log.Logger. The started stack will include TCP and UDP // sockets, a multicast discovery socket, an admin socket, router, switch and diff --git a/yggdrasil.go b/yggdrasil.go index 5244a8ec..d326d18e 100644 --- a/yggdrasil.go +++ b/yggdrasil.go @@ -100,10 +100,15 @@ func main() { normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised") confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") + version := flag.Bool("version", false, "prints the version of this build") flag.Parse() var cfg *nodeConfig switch { + case *version: + fmt.Println("Build name:", yggdrasil.GetBuildName()) + fmt.Println("Build version:", yggdrasil.GetBuildVersion()) + os.Exit(0) case *autoconf: // Use an autoconf-generated config, this will give us random keys and // port numbers, and will use an automatically selected TUN/TAP interface.