From 22f42d55597464dac4334ebedfb2e66a2062972a Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Thu, 30 Aug 2018 14:41:50 -0400 Subject: [PATCH] include build time in binary --- Makefile | 2 +- cmd/version.go | 6 +++--- meta/meta.go | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 74afbf7..bea3a21 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ VENDOR_DIR = vendor IMPORT_PATH = github.com/lbryio/reflector.go VERSION = $(shell git --git-dir=${DIR}/.git describe --dirty --always --long --abbrev=7) -LDFLAGS = -ldflags "-X ${IMPORT_PATH}/meta.Version=${VERSION}" +LDFLAGS = -ldflags "-X ${IMPORT_PATH}/meta.Version=${VERSION} -X ${IMPORT_PATH}/meta.Time=$(shell date +%s)" .PHONY: build dep clean test diff --git a/cmd/version.go b/cmd/version.go index 269ba3d..03b9d51 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,10 +1,10 @@ package cmd import ( - "log" + "fmt" + "time" "github.com/lbryio/reflector.go/meta" - "github.com/spf13/cobra" ) @@ -18,5 +18,5 @@ func init() { } func versionCmd(cmd *cobra.Command, args []string) { - log.Printf("version %s\n", meta.Version) + fmt.Printf("version %s (%s)\n", meta.Version, meta.BuildTime.Format(time.RFC3339)) } diff --git a/meta/meta.go b/meta/meta.go index 81f17b6..e3129c2 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -1,3 +1,20 @@ package meta +import ( + "strconv" + "time" +) + var Version = "" +var Time = "" +var BuildTime time.Time + +func init() { + if Time != "" { + t, err := strconv.Atoi(Time) + if err != nil { + return + } + BuildTime = time.Unix(int64(t), 0).UTC() + } +}