Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
[Feature] Added more information to the version command (#688)
Browse files Browse the repository at this point in the history
* Added more information to the version command
* Added commit hash info
* Added build timestamp info
* Added git branch info
  • Loading branch information
ZeljkoBenovic authored Sep 20, 2022
1 parent 7ada10f commit f982111
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 21 additions & 2 deletions command/version/result.go
Original file line number Diff line number Diff line change
@@ -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()
}
5 changes: 4 additions & 1 deletion command/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
}
7 changes: 6 additions & 1 deletion versioning/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit f982111

Please sign in to comment.