diff --git a/libbeat/cmd/version.go b/libbeat/cmd/version.go index 3d9e9438295..cdecb5279db 100644 --- a/libbeat/cmd/version.go +++ b/libbeat/cmd/version.go @@ -22,8 +22,13 @@ func genVersionCmd(name, beatVersion string) *cobra.Command { return fmt.Errorf("error initializing beat: %s", err) } - fmt.Printf("%s version %s (%s), libbeat %s\n", - beat.Info.Beat, beat.Info.Version, runtime.GOARCH, version.GetDefaultVersion()) + buildTime := "unknown" + if bt := version.BuildTime(); !bt.IsZero() { + buildTime = bt.String() + } + fmt.Printf("%s version %s (%s), libbeat %s [%s built %s]\n", + beat.Info.Beat, beat.Info.Version, runtime.GOARCH, version.GetDefaultVersion(), + version.Commit(), buildTime) return nil }), } diff --git a/libbeat/scripts/Makefile b/libbeat/scripts/Makefile index 761c45839a7..18bd14e0f11 100755 --- a/libbeat/scripts/Makefile +++ b/libbeat/scripts/Makefile @@ -36,7 +36,8 @@ COVERAGE_TOOL?=${BEAT_GOPATH}/bin/gotestcover COVERAGE_TOOL_REPO?=github.com/elastic/beats/vendor/github.com/pierrre/gotestcover TESTIFY_TOOL_REPO?=github.com/elastic/beats/vendor/github.com/stretchr/testify LIBCOMPOSE_TOOL_REPO?=github.com/docker/libcompose -GOBUILD_FLAGS?=-i +NOW=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ') +GOBUILD_FLAGS?=-i -ldflags "-X github.com/elastic/beats/libbeat/version.buildTime=$(NOW) -X github.com/elastic/beats/libbeat/version.commit=$(COMMIT_ID)" GOIMPORTS=goimports GOIMPORTS_REPO?=golang.org/x/tools/cmd/goimports GOIMPORTS_LOCAL_PREFIX?=github.com/elastic diff --git a/libbeat/version/version.go b/libbeat/version/version.go index 395518b4f29..38cce5de6d5 100644 --- a/libbeat/version/version.go +++ b/libbeat/version/version.go @@ -1,3 +1,25 @@ package version +import "time" + const defaultBeatVersion = "7.0.0-alpha1" + +var ( + buildTime = "unknown" + commit = "unknown" +) + +// BuildTime exposes the compile-time build time information. +// It will represent the zero time instant if parsing fails. +func BuildTime() time.Time { + t, err := time.Parse(time.RFC3339, buildTime) + if err != nil { + return time.Time{} + } + return t +} + +// Commit exposes the compile-time commit hash. +func Commit() string { + return commit +}