From a0e4bf133cbac339324e230b2a67dc59700319ca Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Mon, 8 Mar 2021 22:13:05 -0500 Subject: [PATCH] chore: setup build-info package and env variables in Makefile --- Makefile | 25 ++++++++++++++++++++++ internal/build-info/build.go | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 internal/build-info/build.go diff --git a/Makefile b/Makefile index 6209c685f..8def7d69c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,30 @@ #!/usr/bin/env make +# setup variables +NAME := auth0-cli +PKG := github.com/auth0/$(NAME) +BUILDINFOPKG := $(PKG)/internal/build-info + +## setup variables for build-info +BUILDUSER := $(shell whoami) +BUILDTIME := $(shell date -u '+%Y-%m-%d %H:%M:%S') +VERSION := $(GITCOMMIT) +#BUILDVERSION := $(shell git describe --exact-match --abbrev=0) +GITCOMMIT := $(shell git rev-parse --short HEAD) + +GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) +ifneq ($(GITUNTRACKEDCHANGES),) + GITCOMMIT := $(GITCOMMIT)-dirty +endif + +GITBRANCH ?= $(shell git rev-parse --verify --abbrev-ref HEAD) +CTIMEVAR = -X '$(BUILDINFOPKG).Version=$(VERSION)' \ + -X '$(BUILDINFOPKG).Revision=$(GITCOMMIT)' \ + -X '$(BUILDINFOPKG).Branch=$(GITBRANCH)' \ + -X '$(BUILDINFOPKG).BuildUser=$(BUILDUSER)' \ + -X '$(BUILDINFOPKG).BuildDate=$(BUILDTIME)' + + generate: go generate ./... .PHONY: generate diff --git a/internal/build-info/build.go b/internal/build-info/build.go new file mode 100644 index 000000000..2a31f38ba --- /dev/null +++ b/internal/build-info/build.go @@ -0,0 +1,40 @@ +package buildinfo + +import ( + "runtime" +) + +var ( + Version string + Revision string + Branch string + BuildUser string + BuildDate string + GoVersion = runtime.Version() +) + +type BuildInfo struct { + Version string + Revision string + Branch string + BuildUser string + BuildDate string + GoVersion string +} + +// NewDefaultBuildInfo returns the build information obtained from ldflags +func NewDefaultBuildInfo() BuildInfo { + return NewBuildInfo(Version, Branch, BuildDate, BuildUser, GoVersion, Revision) +} + +// NewBuildInfo returns an object with the build information +func NewBuildInfo(version, branch, buildDate, buildUser, goVersion, revision string) BuildInfo { + return BuildInfo{ + Version: version, + Branch: branch, + BuildDate: buildDate, + BuildUser: buildUser, + GoVersion: goVersion, + Revision: revision, + } +}