From 43d11516b9ec554573790da91c18d1f557df2237 Mon Sep 17 00:00:00 2001 From: nicksanford Date: Thu, 11 May 2023 14:17:48 -0400 Subject: [PATCH] [RSDK-2519] add version information (#83) --- Makefile | 5 ++++- module/main.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3715cf70..222a5c03 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ BUILD_CHANNEL?=local TOOL_BIN = bin/gotools/$(shell uname -s)-$(shell uname -m) PATH_WITH_TOOLS="`pwd`/$(TOOL_BIN):${PATH}" +GIT_REVISION = $(shell git rev-parse HEAD | tr -d '\n') +TAG_VERSION?=$(shell git tag --points-at | sort -Vr | head -n1) +GO_BUILD_LDFLAGS = -ldflags "-X 'main.Version=${TAG_VERSION}' -X 'main.GitRevision=${GIT_REVISION}'" set-pkg-config-openssl: pkg-config openssl || export PKG_CONFIG_PATH=$$PKG_CONFIG_PATH:`find \`which brew > /dev/null && brew --prefix\` -name openssl.pc | head -n1 | xargs dirname` @@ -81,7 +84,7 @@ else endif build-module: - mkdir -p bin && go build -o bin/cartographer-module module/main.go + mkdir -p bin && go build $(GO_BUILD_LDFLAGS) -o bin/cartographer-module module/main.go install-lua-files: sudo mkdir -p /usr/local/share/cartographer/lua_files/ diff --git a/module/main.go b/module/main.go index da0e19da..9e455a36 100644 --- a/module/main.go +++ b/module/main.go @@ -3,6 +3,8 @@ package main import ( "context" + "fmt" + "strings" "github.com/edaniels/golog" "go.viam.com/rdk/module" @@ -12,11 +14,37 @@ import ( viamcartographer "github.com/viamrobotics/viam-cartographer" ) +// Versioning variables which are replaced by LD flags. +var ( + Version = "development" + GitRevision = "" +) + func main() { utils.ContextualMain(mainWithArgs, golog.NewLogger("cartographerModule")) } func mainWithArgs(ctx context.Context, args []string, logger golog.Logger) error { + var versionFields []interface{} + if len(args) != 2 { + return fmt.Errorf("usage: %s [socket path] start module\nor: %s --version print version & exit", args[0], args[0]) + } + if Version != "" { + versionFields = append(versionFields, "version", Version) + } + if GitRevision != "" { + versionFields = append(versionFields, "git_rev", GitRevision) + } + if len(versionFields) != 0 { + logger.Infow(viamcartographer.Model.String(), versionFields...) + } else { + logger.Info(viamcartographer.Model.String() + " built from source; version unknown") + } + + if strings.HasSuffix(args[1], "-version") { + return nil + } + // Instantiate the module cartoModule, err := module.NewModuleFromArgs(ctx, logger) if err != nil {