diff --git a/Makefile b/Makefile index 190aa3b9f6..3ee276f86f 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,15 @@ protoc: .PHONY: build build: $(eval LATEST_VERSION = $(shell git describe --tags --abbrev=0)) - $(eval COMMIT_HASH = $(shell git rev-parse --short HEAD)) - go build -ldflags="-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)+$(COMMIT_HASH)'" main.go + $(eval COMMIT_HASH = $(shell git rev-parse HEAD)) + $(eval BRANCH = $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n')) + $(eval TIME = $(shell date)) + go build -o polygon-edge -ldflags="\ + -X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)' \ + -X 'github.com/0xPolygon/polygon-edge/versioning.Commit=$(COMMIT_HASH)'\ + -X 'github.com/0xPolygon/polygon-edge/versioning.Branch=$(BRANCH)'\ + -X 'github.com/0xPolygon/polygon-edge/versioning.BuildTime=$(TIME)'" \ + main.go .PHONY: lint lint: diff --git a/command/version/result.go b/command/version/result.go index 4da1e21334..146dbee32c 100644 --- a/command/version/result.go +++ b/command/version/result.go @@ -1,9 +1,28 @@ package version +import ( + "bytes" + "fmt" + "github.com/0xPolygon/polygon-edge/command/helper" +) + type VersionResult struct { - Version string `json:"version"` + Version string `json:"version"` + Commit string `json:"commit"` + Branch string `json:"branch"` + BuildTime string `json:"buildTime"` } func (r *VersionResult) GetOutput() string { - return r.Version + var buffer bytes.Buffer + + buffer.WriteString("\n[VERSION INFO]\n") + buffer.WriteString(helper.FormatKV([]string{ + fmt.Sprintf("Release version|%s", r.Version), + fmt.Sprintf("Git branch|%s", r.Branch), + fmt.Sprintf("Commit hash|%s", r.Commit), + fmt.Sprintf("Build time|%s", r.BuildTime), + })) + + return buffer.String() } diff --git a/command/version/version.go b/command/version/version.go index b8f5e30854..b71e889247 100644 --- a/command/version/version.go +++ b/command/version/version.go @@ -21,7 +21,10 @@ func runCommand(cmd *cobra.Command, _ []string) { outputter.SetCommandResult( &VersionResult{ - Version: versioning.Version, + Version: versioning.Version, + Commit: versioning.Commit, + Branch: versioning.Branch, + BuildTime: versioning.BuildTime, }, ) } diff --git a/versioning/versioning.go b/versioning/versioning.go index 354640ddab..05bfa70ed6 100644 --- a/versioning/versioning.go +++ b/versioning/versioning.go @@ -2,8 +2,13 @@ package versioning var ( // Version is the main version at the moment. + // Commit is the git commit that the binary was built on + // BuildTime is the timestamp of the build // Embedded by --ldflags on build time // Versioning should follow the SemVer guidelines // https://semver.org/ - Version = "v0.1.0" + Version string + Branch string + Commit string + BuildTime string )