Skip to content

Commit

Permalink
[RSDK-2519] add version information (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksanford authored May 11, 2023
1 parent c8e8376 commit 43d1151
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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/
Expand Down
28 changes: 28 additions & 0 deletions module/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main

import (
"context"
"fmt"
"strings"

"github.com/edaniels/golog"
"go.viam.com/rdk/module"
Expand All @@ -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 {
Expand Down

0 comments on commit 43d1151

Please sign in to comment.