From 833477e3d2981c4cc896fb5709b478386c51b62a Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 2 Sep 2022 17:28:18 -0400 Subject: [PATCH] Provide automatic buildinfo for plugins (#3261) Fixes #3230 Signed-off-by: Marc Khouzam --- cli/runtime/buildinfo/buildinfo.go | 22 +++++++++++++++++++ .../template/plugintemplates/Makefile.tmpl | 4 ++++ .../template/plugintemplates/main.go.tmpl | 5 +++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 cli/runtime/buildinfo/buildinfo.go diff --git a/cli/runtime/buildinfo/buildinfo.go b/cli/runtime/buildinfo/buildinfo.go new file mode 100644 index 0000000000..ddd4a215cb --- /dev/null +++ b/cli/runtime/buildinfo/buildinfo.go @@ -0,0 +1,22 @@ +// Copyright 2021 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package buildinfo holds global variables set at build time to provide information about the plugin build. +package buildinfo + +var ( + // Date is the date the plugin binary was built. + // It should be set using: + // go build --ldflags "-X 'github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo.Date=...'" + Date string + + // SHA is the git commit SHA the plugin binary was built with. + // Is should be set using: + // go build --ldflags "-X 'github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo.SHA=...'" + SHA string + + // Version is the version of the plugin built. + // It should be set using: + // go build --ldflags "-X 'github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo.Version=...'" + Version string +) diff --git a/pkg/v1/builder/template/plugintemplates/Makefile.tmpl b/pkg/v1/builder/template/plugintemplates/Makefile.tmpl index 34ab0b077d..bc0a225a0a 100644 --- a/pkg/v1/builder/template/plugintemplates/Makefile.tmpl +++ b/pkg/v1/builder/template/plugintemplates/Makefile.tmpl @@ -11,6 +11,10 @@ GOARCH ?= $(shell go env GOARCH) GOHOSTOS ?= $(shell go env GOHOSTOS) GOHOSTARCH ?= $(shell go env GOHOSTARCH) +LD_FLAGS = -X 'github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo.Date=$(BUILD_DATE)' +LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo.SHA=$(BUILD_SHA)' +LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo.Version=$(BUILD_VERSION)' + TOOLS_DIR := tools TOOLS_BIN_DIR := $(TOOLS_DIR)/bin GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint diff --git a/pkg/v1/builder/template/plugintemplates/main.go.tmpl b/pkg/v1/builder/template/plugintemplates/main.go.tmpl index e84f028dcc..36d983977a 100644 --- a/pkg/v1/builder/template/plugintemplates/main.go.tmpl +++ b/pkg/v1/builder/template/plugintemplates/main.go.tmpl @@ -6,14 +6,15 @@ import ( "github.com/aunum/log" cliv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/cli/v1alpha1" + "github.com/vmware-tanzu/tanzu-framework/cli/runtime/buildinfo" "github.com/vmware-tanzu/tanzu-framework/cli/runtime/plugin" ) var descriptor = cliv1alpha1.PluginDescriptor{ Name: "{{ .PluginName | ToLower }}", Description: "{{ .Description | ToLower }}", - Version: "v0.0.1", - BuildSHA: "", + Version: buildinfo.Version, + BuildSHA: buildinfo.SHA, Group: cliv1alpha1.ManageCmdGroup, // set group }